public void Play(string path, IMainWindowServices mainWindow) { Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.BufferSize); AudioFileReader reader = null; try { reader = new WAVReader(stream); } catch (FormatException) { try { stream.Seek(0, SeekOrigin.Begin); reader = new AIFFReader(stream); } catch (FormatException) { MessageBox.Show("File is not a recognized AIFF or WAV file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } const double BufferDuration = 2; #if true // prevents "Add New Data Source..." from working state = AudioFilePlayGeneratorParams <OutputDeviceDestination, OutputDeviceArguments> .Do( mainWindow.DisplayName, OutputDeviceEnumerator.OutputDeviceGetDestination, OutputDeviceEnumerator.CreateOutputDeviceDestinationHandler, new OutputDeviceArguments(BufferDuration), AudioFilePlayGeneratorParams <OutputDeviceDestination, OutputDeviceArguments> .MainLoop, generatorParams = new AudioFilePlayGeneratorParams <OutputDeviceDestination, OutputDeviceArguments>( reader), AudioFilePlayGeneratorParams <OutputDeviceDestination, OutputDeviceArguments> .Completion, mainWindow, reader.NumChannels, reader.NumBits, reader.SamplingRate, 1 /*oversampling*/, true /*showProgressWindow*/, true /*modal*/); #endif }
private void buttonPlay_Click(object sender, EventArgs e) { MainWindow.DoAutosaveGlobally(); if (!mainWindow.MakeUpToDate()) { return; } Synthesizer.EffectSpecListRec effectSpec; if (!BuildThis(out effectSpec)) { return; } const double BufferDuration = 2f; #if false if (state != null) { state.stopper.Stop(); } state = null; #endif Stream stream = new FileStream(textBoxAudioFilePath.Text, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.BufferSize); AudioFileReader reader = null; try { reader = new WAVReader(stream); } catch (FormatException) { try { stream.Seek(0, SeekOrigin.Begin); reader = new AIFFReader(stream); } catch (FormatException) { MessageBox.Show("File is not a recognized AIFF or WAV file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } #if true // prevents "Add New Data Source..." from working #if false state = #endif PlayFileWithEffectsGeneratorParams <OutputDeviceDestination, OutputDeviceArguments> .Do( Path.GetFileNameWithoutExtension(textBoxAudioFilePath.Text), OutputDeviceEnumerator.OutputDeviceGetDestination, OutputDeviceEnumerator.CreateOutputDeviceDestinationHandler, new OutputDeviceArguments(BufferDuration), PlayFileWithEffectsGeneratorParams <OutputDeviceDestination, OutputDeviceArguments> .MainLoop, #if false generatorParams = #endif new PlayFileWithEffectsGeneratorParams <OutputDeviceDestination, OutputDeviceArguments>( stream, reader, effectSpec, mainWindow), PlayFileWithEffectsGeneratorParams <OutputDeviceDestination, OutputDeviceArguments> .Completion, mainWindow, reader.NumChannels, reader.NumBits, reader.SamplingRate, 1 /*oversampling*/, true /*showProgressWindow*/, true /*modal*/); #endif }
/* this routine asks for a file and tries to import the contents of that */ /* file as a WAV sample. it reports any errors to the user. */ public static bool ImportWAVSample( Registration registration, IMainWindowServices mainWindow, out int index) { index = -1; string path; using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Title = "Import WAV Sample"; dialog.Filter = "WAV Audio File (.wav)|*.wav|Any File Type (*)|*"; DialogResult result = dialog.ShowDialog(); if (result != DialogResult.OK) { return(false); } path = dialog.FileName; } using (Stream input = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.BufferSize)) { try { using (AudioFileReader reader = new WAVReader(input, true /*allowTruncated*/)) { NumBitsType numBits = reader.NumBits; NumChannelsType numChannels = reader.NumChannels; int numFrames = reader.NumFrames; int samplingRate = reader.SamplingRate; float[] sampleData; try { sampleData = new float[(numFrames + 1) * reader.PointsPerFrame]; } catch (OutOfMemoryException) { throw new AudioFileReaderException(AudioFileReaderErrors.OutOfMemory); } reader.ReadPoints(sampleData, 0, numFrames * reader.PointsPerFrame); if (reader.Truncated) { const string ErrorTooShort = "The file appears to be incomplete. Appending silence to end of sample data."; MessageBox.Show(ErrorTooShort, "Import Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } SampleObjectRec ReturnedSampleObject = new SampleObjectRec( mainWindow.Document, sampleData, numFrames, numBits, numChannels, 0, 0, 0, 0, 0, 0, 0, samplingRate, Constants.MIDDLEC); ReturnedSampleObject.Name = Path.GetFileName(path); index = mainWindow.Document.SampleList.Count; mainWindow.Document.SampleList.Add(ReturnedSampleObject); new SampleWindow(registration, ReturnedSampleObject, mainWindow).Show(); return(true); } } catch (AudioFileReaderException exception) { MessageBox.Show(AudioFileReaderException.MessageFromError(exception.Error), "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } return(false); }