Exemplo n.º 1
0
    public ActionResult Get(string name, [FromServices] AppState appState)
    {
        if (appState is null || appState.Audio is null)
        {
            return(new EmptyResult());
        }

        var audioIO = new AudioInputOutput();
        var data    = new AudioData(
            appState.Audio.Settings.SampleRate,
            appState.Audio.GetOutputArray(ChannelType.Left),
            appState.Audio.GetOutputArray(ChannelType.Right));

        var(success, stream) = audioIO.SaveToMemoryStream(data);

        if (!success)
        {
            return(new EmptyResult());
        }

        // Create a new stream as WaveFileWriter closed memoryStream
        var outStream = new MemoryStream(stream.ToArray());

        var result = new FileStreamResult(outStream, "audio/wave")
        {
            FileDownloadName = name + ".wav"
        };

        return(result);
    }
Exemplo n.º 2
0
        public async Task AudioInputOutputSaveLoadTest()
        {
            var taskProgress = new Progress <double>();
            var taskStatus   = new Progress <string>();

            var audioInputOutput =
                new AudioInputOutput();

            await audioInputOutput.Init(taskProgress);

            var audioLength = 44100;
            var inputAudio  = new float[audioLength];

            for (var i = 0; i < inputAudio.Length; i++)
            {
                inputAudio[i] = (float)Math.Sin(2 * Math.PI * i /
                                                ((float)audioLength / 5));
            }

            var audioData =
                new AudioDataMono(inputAudio);

            for (var index = 0; index < audioData.LengthSamples(); index++)
            {
                audioData.SetOutputSample(
                    index,
                    audioData.GetInputSample(index));
            }

            audioInputOutput.SetAudioData(audioData);

            var testFolder      = KnownFolders.MusicLibrary;
            var audioOutputFile = await testFolder.GetFileAsync("test.wav");

            if (audioOutputFile != null)
            {
                await audioInputOutput.SaveAudioToFile(audioOutputFile, taskStatus);
            }
        }