示例#1
0
        public void LoadFile(AudioData audioData, IFileFormat fileFormat, bool ClearPlaylist = false)
        {
            if (ClearPlaylist)
            {
                audioListView.Items.Clear();
            }

            AudioFileFormats.Add(fileFormat);

            //Load Channel Info
            AudioFile file = new AudioFile();

            file.Title = fileFormat.FileName;
            if (fileFormat is VGAdudioFile)
            {
                file.vgAdudioFile = (VGAdudioFile)fileFormat;
            }

            //Loop through each channel and set it's own
            var format = audioData.GetAllFormats().ToArray()[0];

            for (int c = 0; c < format.ChannelCount; c++)
            {
                using (var memWav = new MemoryStream())
                {
                    AudioChannel audioChannel = new AudioChannel();
                    audioChannel.Name = $"Channel [{c}]";
                    file.Channels.Add(audioChannel);

                    //Load data and write to stream
                    var audio  = format.GetChannels(c).ToPcm16();
                    var writer = new WaveWriter();
                    writer.WriteToStream(audio, memWav);
                    audioChannel.Data = memWav.ToArray();

                    memWav.Position = 0;

                    //Load the player
                    audioChannel.audioPlayer.Open(new MemoryStream(audioChannel.Data), "test.wav", activeDevice);

                    /*     OpenFileDialog openFileDialog = new OpenFileDialog();
                     *   if (openFileDialog.ShowDialog() == DialogResult.OK)
                     *   {
                     *       audioChannel.audioPlayer.Open(openFileDialog.FileName, activeDevice);
                     *   }*/


                    audioChannel.audioPlayer.PlaybackStopped += (s, args) =>
                    {
                        //WasapiOut uses SynchronizationContext.Post to raise the event
                        //There might be already a new WasapiOut-instance in the background when the async Post method brings the PlaybackStopped-Event to us.
                        if (audioChannel.audioPlayer.PlaybackState != PlaybackState.Stopped)
                        {
                        }
                    };
                }
            }

            audioListView.AddObject(file);

            if (audioListView.Items.Count != 0)
            {
                audioListView.SelectedIndex = 0;
            }
        }