public async Task StartAudioInput(IAudioClient audioClient) { StartMicConnectWatcher(); if (_AudioOutStream != null) { _AudioOutStream.Dispose(); _AudioOutStream = null; } if (InputDeviceState != InputDeviceState.Avairable) { if (!await ResetAudioInput()) { return; } } _AudioOutStream = audioClient.CreatePCMStream(AudioApplication.Voice, 1920, 100); _FrameOutputNode.Stop(); _AudioGraph.QuantumStarted += AudioGraph_QuantumStarted; _FrameOutputNode.Start(); _AudioGraph.Start(); }
/// <summary> /// Starts when reading of samples from input audio file finished /// </summary> private void FileInput_FileCompleted(AudioFileInputNode sender, object args) { _audioGraph.Stop(); _frameOutputNode?.Stop(); _audioGraph.Dispose(); _audioGraph = null; _finished = true; _ioProgress?.Report(0); }
private async Task StartAudioGraph() { if (_audioGraph == null) { await InitAudioGraph(); var success = await CreateMicrophoneInputNode(); if (!success) { IsMuted = true; return; } CreateToxOutputNode(); CreateToxInputNode(); await CreateSpeakerOutputNode(); } else { if (IsMuted) { _microphoneInputNode.Stop(); _toxOutputNode.Stop(); } else { _microphoneInputNode.Start(); _toxOutputNode.Start(); } } _audioGraph.Start(); }
/// <summary> /// Stops the audio stream. /// </summary> public Task Stop() { if (Active) { Active = false; outputNode?.Stop(); audioGraph?.Stop(); OnActiveChanged?.Invoke(this, false); } outputNode?.Dispose(); outputNode = null; if (audioGraph != null) { audioGraph.QuantumStarted -= Graph_QuantumStarted; audioGraph.UnrecoverableErrorOccurred -= Graph_UnrecoverableErrorOccurred; audioGraph.Dispose(); audioGraph = null; } return(Task.CompletedTask); }
/// <summary> /// Stops the mircrophone input by stopping the audio graph and resetting the nodes. /// </summary> private static async Task StopInput() { //Caculates the CompletedQuantumCount of the current session and can delay the stop to prevent errors. ulong completedQuantumCount = graph.CompletedQuantumCount - lastCompletedQuantumCount; if (completedQuantumCount < 500) { await Task.Delay(100); } lastCompletedQuantumCount = graph.CompletedQuantumCount; //Runs the code in the UI thread. This is used to prevent error, too. await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { deviceInputNode.Stop(); frameOutputNode.Stop(); graph.Stop(); isListing = false; }); }
LoadAudioFromFile( StorageFile file, IProgress <string> status) { _finished = false; status.Report("Reading audio file"); // Initialize FileInputNode var inputNodeCreationResult = await _audioGraph.CreateFileInputNodeAsync(file); if (inputNodeCreationResult.Status != AudioFileNodeCreationStatus.Success) { return(inputNodeCreationResult); } _fileInputNode = inputNodeCreationResult.FileInputNode; // Read audio file encoding properties to pass them //to FrameOutputNode creator var audioEncodingProperties = _fileInputNode.EncodingProperties; // Initialize FrameOutputNode and connect it to fileInputNode _frameOutputNode = _audioGraph.CreateFrameOutputNode( audioEncodingProperties ); _frameOutputNode.Stop(); _fileInputNode.AddOutgoingConnection(_frameOutputNode); // Add a handler for achiving the end of a file _fileInputNode.FileCompleted += FileInput_FileCompleted; // Add a handler which will transfer every audio frame into audioData _audioGraph.QuantumStarted += FileInput_QuantumStarted; // Initialize audioData var numOfSamples = (int)Math.Ceiling( (decimal)0.0000001 * _fileInputNode.Duration.Ticks * _fileInputNode.EncodingProperties.SampleRate ); if (audioEncodingProperties.ChannelCount == 1) { SetAudioData(new AudioDataMono(new float[numOfSamples])); } else { SetAudioData(new AudioDataStereo(new float[numOfSamples], new float[numOfSamples])); } _audioDataCurrentPosition = 0; // Start process which will read audio file frame by frame // and will generated events QuantumStarted when a frame is in memory _audioGraph.Start(); // didn't find a better way to wait for data while (!_finished) { await Task.Delay(50); } // crear status line status.Report(""); return(inputNodeCreationResult); }