本文整理汇总了C#中NAudio.Wave.Mp3FileReader类的典型用法代码示例。如果您正苦于以下问题:C# Mp3FileReader类的具体用法?C# Mp3FileReader怎么用?C# Mp3FileReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Mp3FileReader类属于NAudio.Wave命名空间,在下文中一共展示了Mp3FileReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestSpeed
public TestSpeed()
{
//WaveStream str1 = new Mp3FileReader("C:\\Users\\mtemkine\\Desktop\\snd\\guitar1.mp3");
//WaveStream str2 = new Mp3FileReader("C:\\Users\\mtemkine\\Desktop\\snd\\molecules.mp3");
//WaveMixerStream32 mix = new WaveMixerStream32(new [] {str1, str2}, false);
var mp3Filename = @"C:\WS\jmerde\trunk\_VisualStudio\AudioBooker\IlyaFranker\Content\snd\ilyafrank_open.mp3";
WavComposite writer = new WavComposite("../../wawa.wav");
writer.AppendAllOfFile(mp3Filename, null);
writer.AppendAllOfFile(mp3Filename, 2);
writer.AppendAllOfFile(mp3Filename, 0.5);
writer.Close();
//format = readerWave.WaveFormat;
//writer = new WaveFileWriter(destFilename, format);
//var wave = new Mp3FileReader(mp3Filename);
using (var reader = new Mp3FileReader(mp3Filename))
using (var pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
using (var downsampledStream = new WaveFormatConversionStream(new WaveFormat(
(int)(pcmStream.WaveFormat.SampleRate * 1.5),
reader.WaveFormat.BitsPerSample,
reader.WaveFormat.Channels), pcmStream))
{
WaveFileWriter.CreateWaveFile("../../xello.wav", downsampledStream);
}
}
示例2: PlaySound
public void PlaySound(string name, Action done = null)
{
FileStream ms = File.OpenRead(_soundLibrary[name]);
var rdr = new Mp3FileReader(ms);
WaveStream wavStream = WaveFormatConversionStream.CreatePcmStream(rdr);
var baStream = new BlockAlignReductionStream(wavStream);
var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
waveOut.Init(baStream);
waveOut.Play();
var bw = new BackgroundWorker();
bw.DoWork += (s, o) =>
{
while (waveOut.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(100);
}
waveOut.Dispose();
baStream.Dispose();
wavStream.Dispose();
rdr.Dispose();
ms.Dispose();
if (done != null) done();
};
bw.RunWorkerAsync();
}
示例3: Encode
public byte[] Encode()
{
int ms = 20;
int channels = 2;
int sampleRate = 48000;
int blockSize = 48 * 2 * channels * ms; //the size per each frame to encode
byte[] buffer = new byte[blockSize]; //a nicely sized pcm buffer to work with.
var outFormat = new WaveFormat(sampleRate, 16, channels);
if(__filename.EndsWith(".mp3"))
{
using (var mp3Reader = new Mp3FileReader(__filename))
{
using (var resampler = new WaveFormatConversionStream(outFormat, mp3Reader))
{
int byteCount;
using (BinaryWriter bw = new BinaryWriter(new MemoryStream()))
{
while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0)
{
//now to encode
byte[] opusOutput = new byte[buffer.Length]; //extra bytes but that's okay
int opusEncoded = encoder.EncodeFrame(buffer, 0, opusOutput);
bw.Write((ushort)opusEncoded);
bw.Write(opusOutput, 0, opusEncoded);
}
MemoryStream baseStream = bw.BaseStream as MemoryStream;
return baseStream.ToArray();
}
}
}
}
return null;
}
示例4: CanLoadAndReadVariousProblemMp3Files
public void CanLoadAndReadVariousProblemMp3Files()
{
string testDataFolder = @"C:\Users\Mark\Downloads\NAudio";
if (!Directory.Exists(testDataFolder))
{
Assert.Ignore("{0} not found", testDataFolder);
}
foreach (string file in Directory.GetFiles(testDataFolder, "*.mp3"))
{
string mp3File = Path.Combine(testDataFolder, file);
Debug.WriteLine(String.Format("Opening {0}", mp3File));
using (var reader = new Mp3FileReader(mp3File))
{
byte[] buffer = new byte[4096];
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));
}
}
}
示例5: CutFile
private static void CutFile(string sourceFile, int startSecond, int endSecond, string resultFile)
{
using (var reader = new Mp3FileReader(sourceFile))
{
FileStream writer = File.Create(resultFile);
Mp3Frame frame;
while ((frame = reader.ReadNextFrame()) != null)
{
var currentSecond = (int)reader.CurrentTime.TotalSeconds;
if (currentSecond >= startSecond && currentSecond <= endSecond)
{
writer.Write(frame.RawData, 0, frame.RawData.Length);
}
else
{
if (currentSecond > endSecond)
{
writer.Dispose();
break;
}
}
}
writer.Dispose();
}
}
示例6: Play
public static Task Play(this Captcha captcha)
{
return Task.Run(() =>
{
using (MemoryStream memory = new MemoryStream(captcha.Data, false))
{
memory.Seek(0, SeekOrigin.Begin);
using (Mp3FileReader reader = new Mp3FileReader(memory))
using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(reader))
using (WaveStream stream = new BlockAlignReductionStream(pcm))
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(stream);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(100);
}
}
}
}
});
}
示例7: GetFileLength
private static TimeSpan GetFileLength(string mp3Path)
{
using (var reader = new Mp3FileReader(mp3Path))
{
return reader.TotalTime;
}
}
示例8: WavHeaderFromMp3
/// <summary>
/// Считывает данные о заголоке из MP3 файла
/// </summary>
public static NAudio.Wave.WaveFormat WavHeaderFromMp3(Stream Stream)
{
using (var reader = new NAudio.Wave.Mp3FileReader(Stream))
{
return reader.WaveFormat;
}
}
示例9: GetNextChunk
public double[][] GetNextChunk(int samplesDesired, Mp3FileReader fr, bool fullnormalize = false)
{
byte[] buffer = new byte[samplesDesired * 4];
short[] left = new short[samplesDesired];
//short[] right = new short[samplesDesired];
double[] leftd = new double[samplesDesired];
//double[] rightd = new double[samplesDesired];
int bytesRead = 0;
try
{
bytesRead = fr.Read(buffer, 0, 4 * samplesDesired);
}
catch
{
new Exception("An error occurred while reading file");
}
int index = 0;
for (int sample = 0; sample < bytesRead / 4; sample++)
{
left[sample] = BitConverter.ToInt16(buffer, index); index += 2;
//right[sample] = BitConverter.ToInt16(buffer, index);
index += 2;
}
if (fullnormalize)
leftd = Utilities.NormalizeFull(ref left, left.Length);
else
leftd = Utilities.Normalize(ref left, left.Length);
return new double[][] { leftd, null }; //rightd };
}
示例10: Mp3ToWav
public static void Mp3ToWav(string mp3File, string outputFile)
{
using (Mp3FileReader reader = new Mp3FileReader(mp3File))
{
WaveFileWriter.CreateWaveFile(outputFile, reader);
}
}
示例11: 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);
}
示例12: FileSelected
protected void FileSelected(string path)
{
fileBrowser = null;
if (path != null)
{
Debug.Log(path);
if(path.Contains(".mp3"))
{
Debug.Log(path);
using (Mp3FileReader reader = new Mp3FileReader(path))
{
Debug.Log("Reached");
path = path.Replace(".mp3", ".wav");
WaveFileWriter.CreateWaveFile(path , reader);
}
}
path = "file://" + path;
WWW wtf = new WWW(path);
//Wait for wtf to finish
while (!wtf.isDone)
{
}
SceneManager.getInstance.setClip(wtf.GetAudioClip(false));
}
}
示例13: PlayMP3
public bool PlayMP3(string fileName)
{
if (fileName == null) { return false; }
if (fileName.StartsWith("http")) {
return StreamMP3(fileName);
}
speaking = true;
WSRConfig.GetInstance().logInfo("PLAYER", "Start MP3 Player");
using (var ms = File.OpenRead(fileName))
using (var mp3Reader = new Mp3FileReader(ms))
using (var pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader))
using (var baStream = new BlockAlignReductionStream(pcmStream))
using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback())) {
waveOut.Init(baStream);
waveOut.Play();
played.Add(fileName);
while (baStream.CurrentTime < baStream.TotalTime && played.Contains(fileName)) {
Thread.Sleep(100);
}
played.Remove(fileName);
waveOut.Stop();
}
WSRConfig.GetInstance().logInfo("PLAYER", "End MP3 Player");
speaking = false;
return true;
}
示例14: Describe
public string Describe(string fileName)
{
var stringBuilder = new StringBuilder();
using (var reader = new Mp3FileReader(fileName))
{
Mp3WaveFormat wf = reader.Mp3WaveFormat;
stringBuilder.AppendFormat("MP3 File WaveFormat: {0} {1}Hz {2} channels {3} bits per sample\r\n",
wf.Encoding, wf.SampleRate,
wf.Channels, wf.BitsPerSample);
stringBuilder.AppendFormat("Extra Size: {0} Block Align: {1} Average Bytes Per Second: {2}\r\n",
wf.ExtraSize, wf.BlockAlign,
wf.AverageBytesPerSecond);
stringBuilder.AppendFormat("ID: {0} Flags: {1} Block Size: {2} Frames per Block: {3}\r\n",
wf.id, wf.flags, wf.blockSize, wf.framesPerBlock
);
stringBuilder.AppendFormat("Length: {0} bytes: {1} \r\n", reader.Length, reader.TotalTime);
stringBuilder.AppendFormat("ID3v1 Tag: {0}\r\n", reader.Id3v1Tag == null ? "None" : reader.Id3v1Tag.ToString());
stringBuilder.AppendFormat("ID3v2 Tag: {0}\r\n", reader.Id3v2Tag == null ? "None" : reader.Id3v2Tag.ToString());
Mp3Frame frame;
while ((frame = reader.ReadNextFrame()) != null)
{
stringBuilder.AppendFormat("{0},{1},{2}Hz,{3},{4}bps, length {5}\r\n",
frame.MpegVersion, frame.MpegLayer,
frame.SampleRate, frame.ChannelMode,
frame.BitRate, frame.FrameLength);
}
}
return stringBuilder.ToString();
}
示例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;
}