private void Recover() { App.Log("Recover MediaPlayer"); graph.Stop(); try { mainInputNode.Dispose(); } catch (Exception) { } try { subInputNode.Dispose(); } catch (Exception) { } try { outputNode.Dispose(); } catch (Exception) { } mainInputNode = null; subInputNode = null; outputNode = null; mainSong = null; subSong = null; try { graph.Dispose(); } catch (Exception) { } graph = null; Init(); }
public static async Task PlayAudio(StorageFile file) { await AudioDevices(); _isAudioPlaying = true; CreateAudioFileInputNodeResult fileInputResult = await audioflow.CreateFileInputNodeAsync(file); if (AudioFileNodeCreationStatus.Success != fileInputResult.Status) { // Cannot read input file Debug.WriteLine(String.Format("Cannot read input file because {0}", fileInputResult.Status.ToString())); _isAudioPlaying = false; return; } if (!_isAudioPlaying) { Debug.WriteLine("Error detected!"); return; } fileInput = fileInputResult.FileInputNode; fileInput.FileCompleted += FileInput_FileCompleted; try { fileInput.AddOutgoingConnection(deviceOuput); } catch (Exception e) { Debug.WriteLine(e.ToString()); } fileInput.StartTime = TimeSpan.FromSeconds(0); audioflow.Start(); _isAudioPlaying = false; }
private static async Task <AudioFileInputNode[, ]> LoadAudioFileInputNodesAsync(int rowsCount, int columnsCount, AudioGraph audioGraph) { var audioDeviceOutputNode = await CreateAudioDeviceOutputNodeAsync(audioGraph); var storageFiles = await LoadStorageFiles(rowsCount, columnsCount); var result = new AudioFileInputNode[rowsCount, columnsCount]; // initialize an input node for each cell for (var y = 0; y < rowsCount; y++) { for (var x = 0; x < columnsCount; x++) { var inputResult = await audioGraph.CreateFileInputNodeAsync(storageFiles[y, x]); if (inputResult.Status != AudioFileNodeCreationStatus.Success) { continue; } var audioFileInputNode = inputResult.FileInputNode; // it shouldn't start when we add it to audioGraph audioFileInputNode.Stop(); // link it to the output node audioFileInputNode.AddOutgoingConnection(audioDeviceOutputNode); // add to the array for easier access to playback result[y, x] = audioFileInputNode; } } return(result); }
private static async Task<AudioFileInputNode[,]> LoadAudioFileInputNodesAsync(int rowsCount, int columnsCount, AudioGraph audioGraph) { var audioDeviceOutputNode = await CreateAudioDeviceOutputNodeAsync(audioGraph); var storageFiles = await LoadStorageFiles(rowsCount, columnsCount); var result = new AudioFileInputNode[rowsCount, columnsCount]; // initialize an input node for each cell for (var y = 0; y < rowsCount; y++) { for (var x = 0; x < columnsCount; x++) { var inputResult = await audioGraph.CreateFileInputNodeAsync(storageFiles[y, x]); if (inputResult.Status != AudioFileNodeCreationStatus.Success) continue; var audioFileInputNode = inputResult.FileInputNode; // it shouldn't start when we add it to audioGraph audioFileInputNode.Stop(); // link it to the output node audioFileInputNode.AddOutgoingConnection(audioDeviceOutputNode); // add to the array for easier access to playback result[y, x] = audioFileInputNode; } } return result; }
/// <summary> /// Ask user to pick a file and use the chosen file to create an AudioFileInputNode /// </summary> private async Task CreateFileInputNode() { FileOpenPicker filePicker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.MusicLibrary, FileTypeFilter = { ".mp3", ".wav" } }; StorageFile file = await filePicker.PickSingleFileAsync(); // file null check code omitted CreateAudioFileInputNodeResult result = await _graph.CreateFileInputNodeAsync(file); if (result.Status != AudioFileNodeCreationStatus.Success) { throw new Exception(result.Status.ToString()); } _fileInputNode = result.FileInputNode; // Set infinite loop _fileInputNode.LoopCount = null; // When file finishes playing the first time, // turn it's output gain down to zero. _fileInputNode.FileCompleted += _fileInputNode_FileCompleted; }
// Event handler for file completion event private void FileInput_FileCompleted(AudioFileInputNode sender, object args) { // File playback is done. Stop the graph _graph.Stop(); // Reset the file input node so starting the graph will resume playback from beginning of the file sender.Reset(); }
private async Task SelectInputFile() { StorageFile file = await GetPackagedFile(null, "audio.mp3"); // File can be null if cancel is hit in the file picker if (file == null) { return; } CreateAudioFileInputNodeResult fileInputNodeResult = await graph.CreateFileInputNodeAsync(file); if (fileInputNodeResult.Status != AudioFileNodeCreationStatus.Success) { // Cannot read file return; } fileInputNode = fileInputNodeResult.FileInputNode; fileInputNode.AddOutgoingConnection(deviceOutputNode); // Event Handler for file completion fileInputNode.FileCompleted += FileInput_FileCompleted; // Enable the button to start the graph // Create the custom effect and apply to the FileInput node AddCustomEffect(); }
/// <summary> /// 6. Called when the current AudioFileInputNode completed playing. /// </summary> private async void CurrentAudioFileInputNode_FileCompleted(AudioFileInputNode sender, object args) { await Task.Delay(new TimeSpan(0, 0, 1)); _currentAudioFileInputNode.FileCompleted -= CurrentAudioFileInputNode_FileCompleted; UpdateEmitter(); }
//<SnippetCreateFileInputNode> private async Task CreateFileInputNode() { if (audioGraph == null) { return; } FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; filePicker.FileTypeFilter.Add(".mp3"); filePicker.FileTypeFilter.Add(".wav"); filePicker.FileTypeFilter.Add(".wma"); filePicker.FileTypeFilter.Add(".m4a"); filePicker.ViewMode = PickerViewMode.Thumbnail; StorageFile file = await filePicker.PickSingleFileAsync(); // File can be null if cancel is hit in the file picker if (file == null) { return; } CreateAudioFileInputNodeResult result = await audioGraph.CreateFileInputNodeAsync(file); if (result.Status != AudioFileNodeCreationStatus.Success) { ShowErrorMessage(result.Status.ToString()); } fileInputNode = result.FileInputNode; }
async void fileInput_FileCompleted(AudioFileInputNode fileInput, object args) { try { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { try { fileInput.Reset(); // Reset the file input node so starting the graph will resume playback from beginning of the file _fileInputs.Remove(fileInput); notifyUser($"Disposing: {fileInput.SourceFile.Name} "); fileInput.Dispose(); /**/ if (_fileInputs.Count() == 0) { _graph.Stop(); notifyUser("All Done!!! "); } } catch (Exception ex) { notifyUser(ex.Message); } }); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { notifyUser("End of file reached"); }); } catch (Exception ex) { notifyUser(ex.Message); } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); var mediaSource = MediaSource.CreateFromUri(new Uri("ms-appx:///Test/GirlishLover.m4a")); await mediaSource.OpenAsync(); this.mpe.Source = mediaSource; this.mpe.MediaPlayer.MediaOpened += this.MediaPlayer_MediaOpened; var settings = new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Other) { QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency }; var result = await AudioGraph.CreateAsync(settings); this.audioGraph = result.Graph; this.outNode = this.audioGraph.CreateFrameOutputNode(); this.fileNode = (await this.audioGraph.CreateFileInputNodeAsync(await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Test/GirlishLover.m4a")))).FileInputNode; this.fileNode.LoopCount = 0; this.fileNode.AddOutgoingConnection(this.outNode); this.fileNode.FileCompleted += this.FileNode_FileCompleted; this.audioGraph.QuantumStarted += this.AudioGraph_QuantumStarted; this.audioGraph.Start(); }
private async void initGraph() { AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Media); CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings); graph = result.Graph; // Create a device output node CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync(); deviceOutput = deviceOutputNodeResult.DeviceOutputNode; FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; filePicker.FileTypeFilter.Add(".mp3"); filePicker.FileTypeFilter.Add(".wav"); StorageFile file = await GetPackagedFile(null, "audio.mp3"); CreateAudioFileInputNodeResult fileInputResult = await graph.CreateFileInputNodeAsync(file); fileInput = fileInputResult.FileInputNode; fileInput.AddOutgoingConnection(deviceOutput); graph.Start(); }
private async void File_Click(object sender, RoutedEventArgs e) { // If another file is already loaded into the FileInput node if (fileInput != null) { // Release the file and dispose the contents of the node fileInput.Dispose(); // Stop playback since a new file is being loaded. Also reset the button UI if (graphButton.Content.Equals("Stop Graph")) { TogglePlay(); } } FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; filePicker.FileTypeFilter.Add(".mp3"); filePicker.FileTypeFilter.Add(".wav"); filePicker.FileTypeFilter.Add(".wma"); filePicker.FileTypeFilter.Add(".m4a"); filePicker.ViewMode = PickerViewMode.Thumbnail; StorageFile file = await filePicker.PickSingleFileAsync(); // File can be null if cancel is hit in the file picker if (file == null) { return; } CreateAudioFileInputNodeResult fileInputResult = await graph.CreateFileInputNodeAsync(file); if (AudioFileNodeCreationStatus.Success != fileInputResult.Status) { // Cannot read input file rootPage.NotifyUser(String.Format("Cannot read input file because {0}", fileInputResult.Status.ToString()), NotifyType.ErrorMessage); return; } fileInput = fileInputResult.FileInputNode; fileInput.AddOutgoingConnection(deviceOutput); fileButton.Background = new SolidColorBrush(Colors.Green); if (fileInput.Duration.TotalSeconds < 3) { // Imported file is too short rootPage.NotifyUser(String.Format("This scenario requires a sound file which is longer than 3 seconds"), NotifyType.ErrorMessage); return; } // Trim the file: set the start time to 3 seconds from the beginning // fileInput.EndTime can be used to trim from the end of file fileInput.StartTime = TimeSpan.FromSeconds(3); // Enable buttons in UI to start graph, loop and change playback speed factor graphButton.IsEnabled = true; loopToggle.IsEnabled = true; playSpeedSlider.IsEnabled = true; }
// will be called when speaking has finished private async void _fileInputNode_FileCompleted(AudioFileInputNode sender, object args) { if (_fileInputNode.LoopCount > 0) { return; // track the loop count down and wait until the end -murks MS will get here twice with 0 when the loopCount is used } _fileInputNode.Stop( ); await EndOfSound( ); }
/// <summary> /// Closes and disposes sound file node /// </summary> /// <param name="sender">Audio file input node</param> /// <param name="args">Arguments</param> private async void FileInputNodeOnFileCompleted(AudioFileInputNode sender, object args) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { sender.RemoveOutgoingConnection(_outputNode); sender.FileCompleted -= FileInputNodeOnFileCompleted; sender.Dispose(); }); }
private AudioMatrix(AudioGraph audioGraph, AudioFileInputNode[,] audioFileInputNodes) { _audioGraph = audioGraph; _audioFileInputNodes = audioFileInputNodes; // we have to start audioGraph one time but we can start/stop individual //input nodes as many times as needed _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); }
static void playNode(AudioFileInputNode node) { if (node == null) { return; } node.Reset(); node.Start(); }
private async void File_Click(object sender, RoutedEventArgs e) { // If another file is already loaded into the FileInput node if (fileInputNode != null) { // Release the file and dispose the contents of the node fileInputNode.Dispose(); // Stop playback since a new file is being loaded. Also reset the button UI if (graphButton.Content.Equals("Stop Graph")) { TogglePlay(); } } FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; filePicker.FileTypeFilter.Add(".mp3"); filePicker.FileTypeFilter.Add(".wma"); filePicker.FileTypeFilter.Add(".wav"); filePicker.ViewMode = PickerViewMode.Thumbnail; StorageFile file = await filePicker.PickSingleFileAsync(); // File can be null if cancel is hit in the file picker if (file == null) { return; } CreateAudioFileInputNodeResult fileInputResult = await graph.CreateFileInputNodeAsync(file); if (fileInputResult.Status != AudioFileNodeCreationStatus.Success) { // Error reading the input file rootPage.NotifyUser(String.Format("Can't read input file because {0}", fileInputResult.Status.ToString()), NotifyType.ErrorMessage); return; } // File loaded successfully. Enable these buttons in the UI graphButton.IsEnabled = true; echoEffectToggle.IsEnabled = true; reverbEffectToggle.IsEnabled = true; limiterEffectToggle.IsEnabled = true; eqToggle.IsEnabled = true; fileInputNode = fileInputResult.FileInputNode; fileInputNode.AddOutgoingConnection(deviceOutputNode); rootPage.NotifyUser("Successfully loaded input file", NotifyType.StatusMessage); fileButton.Background = new SolidColorBrush(Colors.Green); // Create the four inbox effects CreateEchoEffect(); CreateReverbEffect(); CreateLimiterEffect(); CreateEqEffect(); }
private async void MainGraph_QuantumProcessed(AudioGraph sender, object args) { try { this.Position = mainInputNode.Position; if (NextJump != null) { if (NextJump != null && Position >= NextJump.Origin - NextJump.CrossFade && Position <= NextJump.Origin && !IsFading && NextJump.Song == mainSong) { IsFading = true; subInputNode = mainInputNode; subSong = mainSong; mainSong = NextJump.TargetSong ?? NextJump.Song; mainInputNode = await mainSong.Song.CreateNode(graph); mainInputNode.AddOutgoingConnection(outputNode); mainInputNode.StartTime = NextJump.TargetTime - (NextJump.Origin - subInputNode.Position); mainInputNode.OutgoingGain = 0; mainInputNode.Start(); } if (IsFading && subInputNode != null) { var fadePosition = (NextJump.Origin - subInputNode.Position); var fadeTime = NextJump.CrossFade; var percentage = Math.Min(1.0, Math.Max(0.0, fadePosition.TotalSeconds / fadeTime.TotalSeconds)); subInputNode.OutgoingGain = percentage; mainInputNode.OutgoingGain = 1.0 - percentage; } if (Position > NextJump.Origin && IsFading && subInputNode != null) { mainInputNode.OutgoingGain = 1.0; subInputNode.Stop(); subInputNode.RemoveOutgoingConnection(outputNode); subInputNode.Dispose(); subInputNode = null; subSong = null; NextJump = NextJump.NextDefaultJump; IsFading = false; } } } catch (Exception e) { App.Log(e); Recover(); } }
private async void FileInputNodeOnFileCompleted(AudioFileInputNode sender, object args) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { _audioGraph.Stop(); Position = TimeSpan.Zero; }); }
private async void File1_Click(object sender, RoutedEventArgs e) { // If another file is already loaded into the FileInput node if (fileInputNode1 != null) { // Release the file and dispose the contents of the node fileInputNode1.Dispose(); // Stop playback since a new file is being loaded. Also reset the button UI if (graphButton.Content.Equals("Stop Graph")) { TogglePlay(); } } FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; filePicker.FileTypeFilter.Add(".mp3"); filePicker.FileTypeFilter.Add(".wma"); filePicker.FileTypeFilter.Add(".wav"); filePicker.ViewMode = PickerViewMode.Thumbnail; StorageFile file1 = await filePicker.PickSingleFileAsync(); // File can be null if cancel is hit in the file picker if (file1 == null) { return; } CreateAudioFileInputNodeResult fileInputNodeResult = await graph.CreateFileInputNodeAsync(file1); if (fileInputNodeResult.Status != AudioFileNodeCreationStatus.Success) { // Cannot read input file rootPage.NotifyUser(String.Format("Can't read input file1 because {0}", fileInputNodeResult.Status.ToString()), NotifyType.ErrorMessage); return; } fileInputNode1 = fileInputNodeResult.FileInputNode; // Since we are going to play two files simultaneously, set outgoing gain to 0.5 to prevent clipping fileInputNode1.AddOutgoingConnection(submixNode, 0.5); // The graph might already be playing, so set up UI accordingly if (graphButton.Content.Equals("Stop Graph")) { audioPipe1.Fill = new SolidColorBrush(Colors.Blue); } // UI tasks: enable buttons, show status message and change color of the input node box graphButton.IsEnabled = true; echoEffectToggle.IsEnabled = true; rootPage.NotifyUser("Loaded File 1", NotifyType.StatusMessage); fileButton1.Background = new SolidColorBrush(Colors.Green); }
public void Dispose() { _audioGraph?.Dispose(); _outputNode?.Dispose(); _inputNode?.Dispose(); _timer?.Dispose(); _timer = null; _audioGraph = null; _outputNode = null; _inputNode = null; }
public async Task <bool> SetSourceAsync(StorageFile file) { Stop(); _inputNode?.Dispose(); _inputNode = null; var result = await _audioGraph.CreateFileInputNodeAsync(file); LastStatus = result.Status.ToString(); if (result.Status != AudioFileNodeCreationStatus.Success) { return(false); } lock (this) { try { _inputNode = result.FileInputNode; var invalidDuration = TimeSpan.FromMilliseconds(50); if (_inputNode.Duration < invalidDuration) { return(false); } _inputNode.AddOutgoingConnection(_outputNode); ThresoldDuration = _inputNode.Duration - invalidDuration; _inputNode.EffectDefinitions.Add(CreateEchoEffect()); _inputNode.EffectDefinitions.Add(CreateLimiterEffect()); _inputNode.EffectDefinitions.Add(CreateReverbEffect()); _inputNode.EffectDefinitions.Add(CreateEqualizerEffect()); IsEchoEffectEnabled = IsEchoEffectEnabled; IsReverbEffectEnabled = IsReverbEffectEnabled; IsLimiterEffectEnabled = IsLimiterEffectEnabled; IsEQEffectEnabled = IsEQEffectEnabled; } catch (Exception) { return(false); } } if (AutoPlay) { Play(); } return(true); }
private async void PlayAudioGraph_Click(object sender, RoutedEventArgs e) { // If another file is already loaded into the FileInput node if (fileInput != null) { // Release the file and dispose the contents of the node fileInput.Dispose(); } FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; filePicker.FileTypeFilter.Add(".mp3"); filePicker.FileTypeFilter.Add(".wav"); filePicker.FileTypeFilter.Add(".wma"); filePicker.FileTypeFilter.Add(".m4a"); filePicker.ViewMode = PickerViewMode.Thumbnail; StorageFile file = await filePicker.PickSingleFileAsync(); // File can be null if cancel is hit in the file picker if (file == null) { return; } CreateAudioFileInputNodeResult fileInputResult = await graph.CreateFileInputNodeAsync(file); if (AudioFileNodeCreationStatus.Success != fileInputResult.Status) { // Cannot read input file Logging.SingleInstance.LogMessage("Cannot read input file because " + fileInputResult.Status); return; } fileInput = fileInputResult.FileInputNode; if (fileInput.Duration <= TimeSpan.FromSeconds(3)) { // Imported file is too short Logging.SingleInstance.LogMessage("Please pick an audio file which is longer than 3 seconds " + fileInputResult.Status); fileInput.Dispose(); fileInput = null; return; } fileInput.AddOutgoingConnection(deviceOutput); // Trim the file: set the start time to 3 seconds from the beginning // fileInput.EndTime can be used to trim from the end of file fileInput.StartTime = TimeSpan.FromSeconds(3); }
private async void Button_Click(object sender, RoutedEventArgs e) { destination = await ApplicationData.Current.LocalFolder.CreateFileAsync("myfile1", CreationCollisionOption.ReplaceExisting); var openPicker = new Windows.Storage.Pickers.FileOpenPicker(); openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary; openPicker.FileTypeFilter.Add(".wmv"); openPicker.FileTypeFilter.Add(".mp4"); openPicker.FileTypeFilter.Add(".flv"); openPicker.FileTypeFilter.Add(".3gp"); openPicker.FileTypeFilter.Add(".avi"); openPicker.FileTypeFilter.Add(".mkv"); source = await openPicker.PickSingleFileAsync(); //获取视频信息 if (source != null) { var clip = await MediaClip.CreateFromFileAsync(source); property = clip.GetVideoEncodingProperties(); string encodeInfo = property.Subtype; Source_height = property.Height; Source_width = property.Width; Source_frameRate = property.FrameRate.Numerator; dataRate = property.Bitrate; } //获取声频信息 if (source != null) { AudioGraphSettings settings = new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media); CreateAudioGraphResult createResult = await AudioGraph.CreateAsync(settings); if (createResult.Status != AudioGraphCreationStatus.Success) { return; } AudioGraph audioGraph = createResult.Graph; CreateAudioFileInputNodeResult result = await audioGraph.CreateFileInputNodeAsync(source); if (result.Status != AudioFileNodeCreationStatus.Success) { return; } AudioFileInputNode fileInputNode = result.FileInputNode; AudioEncodingProperties property = fileInputNode.EncodingProperties; //Get encode property string subTitles = property.Subtype; SampleRate = property.SampleRate; } }
public async Task Play(SongViewModel song) { if (mainInputNode == null) { mainInputNode = await song.Song.CreateNode(graph); mainSong = song; NextJump = mainSong.Jumps.FirstOrDefault(); mainInputNode.AddOutgoingConnection(outputNode); graph.Start(); IsPlaying = true; } else if (IsPlaying) { var jump = new JumpViewModel(mainSong) { Origin = mainInputNode.Position + TimeSpan.FromSeconds(5), TargetSong = song, TargetTime = TimeSpan.FromSeconds(5), CrossFade = TimeSpan.FromSeconds(5) }; if (mainInputNode.Duration < jump.Origin) { jump.Origin = mainInputNode.Duration; jump.CrossFade = mainInputNode.Duration - mainInputNode.Position; jump.TargetTime = jump.CrossFade; } NextJump = jump; } else { if (mainInputNode != null) { mainInputNode.RemoveOutgoingConnection(outputNode); mainInputNode.Dispose(); mainInputNode = null; mainSong = null; } if (subInputNode != null) { subInputNode.RemoveOutgoingConnection(outputNode); subInputNode.Dispose(); subInputNode = null; subSong = null; } IsFading = false; await Play(song); } }
public async Task LoadFileIntoGraph(StorageFile audioFile) { CreateAudioFileInputNodeResult audioFileInputResult = await this._audioGraph.CreateFileInputNodeAsync(audioFile); if (audioFileInputResult.Status != AudioFileNodeCreationStatus.Success) { throw new Exception("File failed to load into graph."); } _fileInputNode = audioFileInputResult.FileInputNode; _fileInputNode.AddOutgoingConnection(_deviceOutputNode); CreateAndAddEchoEffect(); }
async Task createFileInputNodes() { fixNode = await createFileInputNode("fix.wav"); errorNode = await createFileInputNode("error.wav"); typingNodes = await createFileInputNodesFromFolder("typing"); spaceNodes = await createFileInputNodesFromFolder("space"); backspaceNode = await createFileInputNode("backspace.wav"); finishedNode = await createFileInputNode("finished.wav", 0.65); }
// Event handler for file completion event private async void FileInput_FileCompleted(AudioFileInputNode sender, object args) { // File playback is done. Stop the graph graph.Stop(); // Reset the file input node so starting the graph will resume playback from beginning of the file sender.Reset(); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { rootPage.NotifyUser("End of file reached", NotifyType.StatusMessage); graphButton.Content = "Start Graph"; }); }
private async Task SelectInputFile() { // If another file is already loaded into the FileInput node if (fileInputNode != null) { // Release the file and dispose the contents of the node fileInputNode.Dispose(); // Stop playback since a new file is being loaded. Also reset the button UI if (graphButton.Content.Equals("Stop Graph")) { TogglePlay(); } } FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; filePicker.FileTypeFilter.Add(".mp3"); filePicker.ViewMode = PickerViewMode.Thumbnail; StorageFile file = await filePicker.PickSingleFileAsync(); // File can be null if cancel is hit in the file picker if (file == null) { return; } CreateAudioFileInputNodeResult fileInputNodeResult = await graph.CreateFileInputNodeAsync(file); if (fileInputNodeResult.Status != AudioFileNodeCreationStatus.Success) { // Cannot read file rootPage.NotifyUser(String.Format("Cannot read input file because {0}", fileInputNodeResult.Status.ToString()), NotifyType.ErrorMessage); return; } fileInputNode = fileInputNodeResult.FileInputNode; fileInputNode.AddOutgoingConnection(deviceOutputNode); fileButton.Background = new SolidColorBrush(Colors.Green); // Event Handler for file completion fileInputNode.FileCompleted += FileInput_FileCompleted; // Enable the button to start the graph graphButton.IsEnabled = true; // Create the custom effect and apply to the FileInput node AddCustomEffect(); }
private async Task LoadAudioFromFile(StorageFile file) { // We initialize an instance of AudioGraph AudioGraphSettings settings = new AudioGraphSettings( Windows.Media.Render.AudioRenderCategory.Media ); CreateAudioGraphResult result1 = await AudioGraph.CreateAsync(settings); if (result1.Status != AudioGraphCreationStatus.Success) { ShowMessage("AudioGraph creation error: " + result1.Status.ToString()); } audioGraph = result1.Graph; if (audioGraph == null) return; // We initialize FileInputNode CreateAudioFileInputNodeResult result2 = await audioGraph.CreateFileInputNodeAsync(file); if (result2.Status != AudioFileNodeCreationStatus.Success) { ShowMessage("FileInputNode creation error: " + result2.Status.ToString()); } fileInputNode = result2.FileInputNode; if (fileInputNode == null) return; // We read audio file encoding properties to pass them to FrameOutputNode creator AudioEncodingProperties audioEncodingProperties = fileInputNode.EncodingProperties; // We initialize FrameOutputNode and connect it to fileInputNode frameOutputNode = audioGraph.CreateFrameOutputNode(audioEncodingProperties); fileInputNode.AddOutgoingConnection(frameOutputNode); // We add a handler achiving the end of a file fileInputNode.FileCompleted += FileInput_FileCompleted; // We add a handler which will transfer every audio frame into audioData audioGraph.QuantumStarted += AudioGraph_QuantumStarted; // We initialize audioData int numOfSamples = (int)Math.Ceiling( (decimal)0.0000001 * fileInputNode.Duration.Ticks * fileInputNode.EncodingProperties.SampleRate ); audioData = new float[numOfSamples]; audioDataCurrentPosition = 0; // We start process which will read audio file frame by frame // and will generated events QuantumStarted when a frame is in memory audioGraph.Start(); }
public async Task LoadFileAsync(IStorageFile file) { if (_deviceOutput == null) { await CreateAudioGraph(); } else { _graph.Start(); currentSound.RemoveOutgoingConnection(_deviceOutput); currentSound.Dispose(); } var fileInputResult = await _graph.CreateFileInputNodeAsync(file); currentSound = fileInputResult.FileInputNode; fileInputResult.FileInputNode.Stop(); fileInputResult.FileInputNode.AddOutgoingConnection(_deviceOutput); }
public AudioFileInputNodeEvents(AudioFileInputNode This) { this.This = This; }
private async void FileInput_FileCompleted(AudioFileInputNode sender, object args) { // File playback is done. Stop the graph graph.Stop(); // Reset the file input node so starting the graph will resume playback from beginning of the file sender.Reset(); }
private async void File_Click(object sender, RoutedEventArgs e) { // If another file is already loaded into the FileInput node if (fileInput != null) { // Release the file and dispose the contents of the node fileInput.Dispose(); // Stop playback since a new file is being loaded. Also reset the button UI if (graphButton.Content.Equals("Stop Graph")) { TogglePlay(); } } FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary; filePicker.FileTypeFilter.Add(".mp3"); filePicker.FileTypeFilter.Add(".wav"); filePicker.FileTypeFilter.Add(".wma"); filePicker.FileTypeFilter.Add(".m4a"); filePicker.ViewMode = PickerViewMode.Thumbnail; StorageFile file = await filePicker.PickSingleFileAsync(); // File can be null if cancel is hit in the file picker if (file == null) { return; } CreateAudioFileInputNodeResult fileInputResult = await graph.CreateFileInputNodeAsync(file); if (AudioFileNodeCreationStatus.Success != fileInputResult.Status) { // Cannot read input file await ShowMessage(String.Format("Cannot read input file because {0}", fileInputResult.Status.ToString())); return; } fileInput = fileInputResult.FileInputNode; fileInput.AddOutgoingConnection(deviceOutput); fileButton.Background = new SolidColorBrush(Colors.Green); // Trim the file: set the start time to 3 seconds from the beginning // fileInput.EndTime can be used to trim from the end of file fileInput.StartTime = TimeSpan.FromSeconds(3); // Enable buttons in UI to start graph, loop and change playback speed factor graphButton.IsEnabled = true; }
private void MainPage_FileCompleted(AudioFileInputNode sender, object args) { sender.Stop(); }
private void SelectInputFile() { StorageFile file = this.GetPackagedFile(null, "audio.mp3").Result; // File can be null if cancel is hit in the file picker if (file == null) { return; } CreateAudioFileInputNodeResult fileInputNodeResult = graph.CreateFileInputNodeAsync(file).GetResults(); if (fileInputNodeResult.Status != AudioFileNodeCreationStatus.Success) { // Cannot read file return; } fileInputNode = fileInputNodeResult.FileInputNode; fileInputNode.AddOutgoingConnection(deviceOutputNode); // Event Handler for file completion fileInputNode.FileCompleted += FileInput_FileCompleted; }