本文整理汇总了C#中NAudio.Wave.WaveFileReader类的典型用法代码示例。如果您正苦于以下问题:C# WaveFileReader类的具体用法?C# WaveFileReader怎么用?C# WaveFileReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WaveFileReader类属于NAudio.Wave命名空间,在下文中一共展示了WaveFileReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AudioPlayer
public AudioPlayer(Resource resource, TabPage tab)
{
var soundData = (Sound)resource.Blocks[BlockType.DATA];
var stream = soundData.GetSoundStream();
waveOut = new WaveOutEvent();
if (soundData.Type == Sound.AudioFileType.WAV)
{
var rawSource = new WaveFileReader(stream);
waveOut.Init(rawSource);
}
else if (soundData.Type == Sound.AudioFileType.MP3)
{
var rawSource = new Mp3FileReader(stream);
waveOut.Init(rawSource);
}
playButton = new Button();
playButton.Text = "Play";
playButton.TabIndex = 1;
playButton.Size = new Size(100, 25);
playButton.Click += PlayButton_Click;
tab.Controls.Add(playButton);
}
示例2: PlayMusic
public void PlayMusic(string sample, IAudioClient voiceClient)
{
using (WaveFileReader pcm = new WaveFileReader(sample))
{
int blocksize = pcm.WaveFormat.AverageBytesPerSecond / 5;
byte[] buffer = new byte[blocksize];
int offset = 0;
try
{
while (offset < pcm.Length / blocksize && !skip)
{
if (currentPlaying != sample)
{
}
offset++;
pcm.Read(buffer, 0, blocksize);
voiceClient.Send(buffer, 0, blocksize);
//voiceClient.Wait();
}
}
catch (Exception e)
{
Console.Write(e.ToString());
}
}
return;
}
示例3: ConcatenateWav
/// <summary>
/// Merge multiple .wav files together and save the output.
/// </summary>
/// <param name="outputFile">The path to save the output to.</param>
/// <param name="sourceFiles">An IEnumerable list of files to merge.</param>
private static void ConcatenateWav(string outputFile, IEnumerable<string> sourceFiles) {
byte[] buffer = new byte[1024];
WaveFileWriter waveFileWriter = null;
try {
foreach (string sourceFile in sourceFiles) {
using (WaveFileReader reader = new WaveFileReader(sourceFile)) {
if (waveFileWriter == null) {
// first time in create new Writer
waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
}
else {
if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat)) {
throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
}
}
int read;
while ((read = reader.Read(buffer, 0, buffer.Length)) > 0) {
waveFileWriter.WriteData(buffer, 0, read);
}
}
}
}
finally {
if (waveFileWriter != null) {
waveFileWriter.Dispose();
}
}
}
示例4: FlushUpdatesHeaderEvenIfDisposeNotCalled
public void FlushUpdatesHeaderEvenIfDisposeNotCalled()
{
var ms = new MemoryStream();
var testSequence = new byte[] { 0x1, 0x2, 0xFF, 0xFE };
var testSequence2 = new byte[] { 0x3, 0x4, 0x5 };
var writer = new WaveFileWriter(new IgnoreDisposeStream(ms), new WaveFormat(16000, 24, 1));
writer.Write(testSequence, 0, testSequence.Length);
writer.Flush();
// BUT NOT DISPOSED
// another write that was not flushed
writer.Write(testSequence2, 0, testSequence2.Length);
// check the Reader can read it
ms.Position = 0;
using (var reader = new WaveFileReader(ms))
{
Assert.AreEqual(16000, reader.WaveFormat.SampleRate, "Sample Rate");
Assert.AreEqual(24, reader.WaveFormat.BitsPerSample, "Bits Per Sample");
Assert.AreEqual(1, reader.WaveFormat.Channels, "Channels");
Assert.AreEqual(testSequence.Length, reader.Length, "File Length");
var buffer = new byte[600]; // 24 bit audio, block align is 3
int read = reader.Read(buffer, 0, buffer.Length);
Assert.AreEqual(testSequence.Length, read, "Data Length");
for (int n = 0; n < read; n++)
{
Assert.AreEqual(testSequence[n], buffer[n], "Byte " + n);
}
}
writer.Dispose(); // to stop the finalizer from moaning
}
示例5: ReaderShouldReadBackSameDataWrittenWithWrite
public void ReaderShouldReadBackSameDataWrittenWithWrite()
{
var ms = new MemoryStream();
var testSequence = new byte[] { 0x1, 0x2, 0xFF, 0xFE };
using (var writer = new WaveFileWriter(new IgnoreDisposeStream(ms), new WaveFormat(16000, 24, 1)))
{
writer.Write(testSequence, 0, testSequence.Length);
}
// check the Reader can read it
ms.Position = 0;
using (var reader = new WaveFileReader(ms))
{
Assert.AreEqual(16000, reader.WaveFormat.SampleRate, "Sample Rate");
Assert.AreEqual(24, reader.WaveFormat.BitsPerSample, "Bits Per Sample");
Assert.AreEqual(1, reader.WaveFormat.Channels, "Channels");
Assert.AreEqual(testSequence.Length, reader.Length, "File Length");
var buffer = new byte[600]; // 24 bit audio, block align is 3
int read = reader.Read(buffer, 0, buffer.Length);
Assert.AreEqual(testSequence.Length, read, "Data Length");
for (int n = 0; n < read; n++)
{
Assert.AreEqual(testSequence[n], buffer[n], "Byte " + n);
}
}
}
示例6: OutputWaveToFile
public void OutputWaveToFile(
string textToSpeak,
string waveFile)
{
if (string.IsNullOrWhiteSpace(textToSpeak))
{
return;
}
this.StartSasara();
var tempWave = Path.GetTempFileName();
talker.OutputWaveToFile(
textToSpeak,
tempWave);
// ささらは音量が小さめなので増幅する
using (var reader = new WaveFileReader(tempWave))
{
var prov = new VolumeWaveProvider16(reader);
prov.Volume = Settings.Default.SasaraGain;
WaveFileWriter.CreateWaveFile(
waveFile,
prov);
}
if (File.Exists(tempWave))
{
File.Delete(tempWave);
}
}
示例7: AudioSample
public AudioSample(string fileName)
{
_fileName = fileName;
WaveFileReader reader = new WaveFileReader(fileName);
//offsetStream = new WaveOffsetStream(reader);
//channelStream = new WaveChannel32(offsetStream);
channelStream = new WaveChannel32(reader);
muted = false;
volume = 1.0f;
#region ReverseStuff
//Code to reverse
NAudioBufferReverse nbr = new NAudioBufferReverse();
reversedSample = new byte[(int)channelStream.Length];
channelStream.Read(reversedSample, 0, (int)channelStream.Length);
bytesPerSample = (channelStream.WaveFormat.BitsPerSample / 8) * channelStream.WaveFormat.Channels;
reversedSample = nbr.reverseSample(reversedSample, (int)channelStream.Length, bytesPerSample);
#endregion
}
示例8: WavToMP3
/// <summary>
/// Convert WAV to MP3 using libmp3lame library
/// </summary>
/// <param name="waveFileName">WAV filename</param>
/// <param name="mp3FileName">MP3 filename</param>
/// <param name="bitRate">Bit rate, default 128</param>
/// <param name="artist">Optional artist name</param>
/// <param name="album">Optional album name</param>
/// <param name="setID3Tags">Set ID3 tags</param>
public static bool WavToMP3(string waveFileName, string mp3FileName, int bitRate = 128, string artist = null, string album = null, bool setID3Tags = false, string genre = "148")
{
bool result = true;
try
{
ID3TagData tags = new ID3TagData();
if (setID3Tags)
{
if (!String.IsNullOrEmpty(artist))
{
tags.Artist = artist;
tags.Album = album;
tags.Genre = genre;
}
}
using (var reader = new WaveFileReader(waveFileName))
using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate, tags))
reader.CopyTo(writer);
}
catch (Exception)
{
result = false;
}
return result;
}
示例9: PianoSynth
public PianoSynth()
{
mixer = new WaveMixerStream32();
mixer.AutoStop = false;
int i = 0;
foreach (var note in noteFiles)
{
reader[i] = new WaveFileReader(note);
offsetStream[i] = new WaveOffsetStream(reader[i]);
channelSteam[i] = new WaveChannel32(offsetStream[i]);
channelSteam[i].Position = channelSteam[i].Length;
mixer.AddInputStream(channelSteam[i]);
i++;
}
if (waveOutDevice == null)
{
waveOutDevice = new WaveOut {DeviceNumber = 0};
waveOutDevice.Init(mixer);
waveOutDevice.Volume = 0;
waveOutDevice.Play();
waveOutDevice.Volume = 100;
}
}
示例10: CanLoadAndReadVariousProblemWavFiles
public void CanLoadAndReadVariousProblemWavFiles()
{
string testDataFolder = @"C:\Users\Mark\Downloads\NAudio";
if (!Directory.Exists(testDataFolder))
{
Assert.Ignore("{0} not found", testDataFolder);
}
foreach (string file in Directory.GetFiles(testDataFolder, "*.wav"))
{
string wavFile = Path.Combine(testDataFolder, file);
Debug.WriteLine(String.Format("Opening {0}", wavFile));
using (var reader = new WaveFileReader(wavFile))
{
byte[] buffer = new byte[reader.WaveFormat.AverageBytesPerSecond];
int bytesRead;
int total = 0;
do
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
total += bytesRead;
} while (bytesRead > 0);
Debug.WriteLine(String.Format("Read {0} bytes", total));
}
}
}
示例11: playbackFile
public void playbackFile(string filename)
{
WaveFileReader reader = new WaveFileReader(filename);
WaveChannel32 channelStream = new WaveChannel32(reader);
mixer.AddInputStream(channelStream);
channelStream.Position = 0;
}
示例12: Read
public static Sound Read(Stream source)
{
BinaryReaderEx reader = new BinaryReaderEx(source);
Sound result = new Sound();
int id = reader.ReadInt32();
if (id == 0x00394453)
{
int headerLength = reader.ReadInt32();
int sampleLength = reader.ReadInt32();
headerLength -= 12;
if (headerLength > 0)
reader.ReadBytes(headerLength);
byte[] wavData = reader.ReadBytes(sampleLength);
using (MemoryStream wavDataMem = new MemoryStream(wavData))
{
using (WaveStream wavStream = new WaveFileReader(wavDataMem))
{
byte[] rawWaveData = new byte[wavStream.Length];
wavStream.Read(rawWaveData, 0, (int)wavStream.Length);
result.SetSound(rawWaveData, wavStream.WaveFormat);
}
}
}
return result;
}
示例13: BonIverStemPlayer
// set everything up
public BonIverStemPlayer(Engine.Engine engine) {
mixer.AutoStop = false;
waveOutDevice.Init(mixer);
Engine = engine;
// load in each of our files
reader[0] = new WaveFileReader(@"C:\\Users\\Badger\\repos\\balloon\\SkeletalTracking\\boniver\\bangorkook.wav");
reader[1] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\chippewa_falls.wav");
reader[2] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\eleva.wav");
reader[3] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\gilman.wav");
reader[4] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\grand_rapids.wav");
reader[5] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\heigh_on.wav");
reader[6] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\lake_hallie.wav");
reader[7] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\le_grange_wi.wav");
reader[8] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\long_plain.wav");
reader[9] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\mandolin_wa.wav");
reader[10] = new WaveFileReader(@"C:\Users\Badger\repos\balloon\SkeletalTracking\boniver\virginia.wav");
// set the starting positions
// and load into the mixer
for (int i = 0; i < 11; i++) {
channelStream[i] = new WaveChannel32(reader[i]);
channelStream[i].Position = 0;
mixer.AddInputStream(channelStream[i]);
}
}
示例14: EncodeFile
private void EncodeFile()
{
string inputFileName = GetInputFileName("Select PCM WAV File to Encode");
if (inputFileName == null)
return;
using (var reader = new WaveFileReader(inputFileName))
{
if (reader.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
{
MessageBox.Show("Please select a PCM WAV file to encode");
return;
}
WaveFormat targetFormat = GetTargetFormat(reader.WaveFormat);
if (targetFormat == null)
{
return;
}
string outputFileName = GetOutputFileName("Select Ouput File Name");
if (outputFileName == null)
{
return;
}
using (WaveStream convertedStream = new WaveFormatConversionStream(targetFormat, reader))
{
WaveFileWriter.CreateWaveFile(outputFileName, convertedStream);
}
if (checkBoxAutoLaunchConvertedFile.Checked)
{
System.Diagnostics.Process.Start(outputFileName);
}
}
}
示例15: SoundResource
public SoundResource(ResourceManager mgr, string fullName, Stream stream, Format fmt)
{
_manager = mgr;
FullName = fullName;
switch (fmt)
{
case Format.MP3:
{
Mp3FileReader mp3 = new Mp3FileReader(stream);
_reader = mp3;
break;
}
case Format.WAV:
{
WaveFileReader wav = new WaveFileReader(stream);
_reader = wav;
break;
}
default:
throw new InvalidOperationException("Unsupported extension.");
}
_stream = new WaveChannel32(_reader);
_stream.PadWithZeroes = false;
_wavDevice.PlaybackStopped += _wavDevice_PlaybackStopped;
}