public static int ReadWavFile(string path, out AudioData ad) { ad = new AudioData(); using (var br = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))) { var reader = new WavRWLib2.WavReader(); if (!reader.ReadHeaderAndSamples(br, 0, -1)) { MessageBox.Show( string.Format("Error: Failed to read WAV file: {0}", path)); return(-1); } ad.meta = new WWFlacRWCS.Metadata(); ad.meta.albumStr = reader.AlbumName; ad.meta.artistStr = reader.ArtistName; ad.meta.titleStr = reader.Title; ad.meta.pictureBytes = reader.PictureBytes; ad.picture = reader.PictureData; ad.meta.totalSamples = reader.NumFrames; ad.meta.channels = reader.NumChannels; ad.meta.sampleRate = reader.SampleRate; var interleaved = reader.GetSampleLargeArray(); int bytesPerSample = reader.BitsPerSample / 8; ad.pcm = new List <AudioDataPerChannel>(); for (int ch = 0; ch < reader.NumChannels; ++ch) { var pcmOneChannel = new WWUtil.LargeArray <byte>(reader.NumFrames * bytesPerSample); for (long i = 0; i < reader.NumFrames; ++i) { for (int b = 0; b < reader.BitsPerSample / 8; ++b) { pcmOneChannel.Set(bytesPerSample * i + b, interleaved.At(bytesPerSample * (reader.NumChannels * i + ch) + b)); } } #if true var adp = new AudioDataPerChannel(); adp.mData = pcmOneChannel; adp.mOffsBytes = 0; adp.mBitsPerSample = reader.BitsPerSample; adp.mValueRepresentationType = reader.SampleValueRepresentationType; #else var pcm32 = PcmDataLib.PcmDataUtil.ConvertTo32bitInt(reader.BitsPerSample, reader.NumFrames, reader.SampleValueRepresentationType, pcmOneChannel); var adp = new AudioDataPerChannel(); adp.mData = pcm32; adp.mOffsBytes = 0; adp.mBitsPerSample = 32; adp.mValueRepresentationType = PcmDataLib.PcmData.ValueRepresentationType.SInt; #endif adp.mTotalSamples = ad.meta.totalSamples; ad.pcm.Add(adp); } ad.meta.bitsPerSample = reader.BitsPerSample; ad.preferredSaveFormat = WWAFUtil.FileFormatType.FLAC; if (24 < ad.meta.bitsPerSample) { ad.preferredSaveFormat = WWAFUtil.FileFormatType.WAVE; } return(0); } }
private void LoadPcm_DoWork(object sender, DoWorkEventArgs e) { string path = (string)e.Argument; var r = new LoadPcmResult(); r.path = path; r.result = false; r.pcmData = null; mPlayPcmData = null; try { using (var br = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))) { var reader = new WavRWLib2.WavReader(); if (reader.ReadHeaderAndSamples(br, 0, -1) && reader.NumChannels == NUM_CHANNELS) { var b = reader.GetSampleLargeArray(); r.pcmData = new PcmDataLib.PcmData(); r.pcmData.SetFormat(NUM_CHANNELS, reader.BitsPerSample, reader.ValidBitsPerSample, reader.SampleRate, reader.SampleValueRepresentationType, reader.NumFrames); r.pcmData.SetSampleLargeArray(b); } } } catch (Exception ex) { Console.WriteLine(ex); r.pcmData = null; } if (r.pcmData == null) { try { var flacRW = new WWFlacRWCS.FlacRW(); int rv = flacRW.DecodeAll(r.path); if (0 <= rv) { WWFlacRWCS.Metadata metaData; flacRW.GetDecodedMetadata(out metaData); if (metaData.channels == NUM_CHANNELS) { var pcmBytes = new LargeArray <byte>(metaData.PcmBytes); int bytesPerSample = metaData.bitsPerSample / 8; var fragment = new byte[bytesPerSample]; for (long pos = 0; pos < metaData.totalSamples; ++pos) { for (int ch = 0; ch < NUM_CHANNELS; ++ch) { flacRW.GetDecodedPcmBytes(ch, pos * bytesPerSample, out fragment, bytesPerSample); pcmBytes.CopyFrom(fragment, 0, (long)bytesPerSample * (NUM_CHANNELS * pos + ch), bytesPerSample); } } r.pcmData = new PcmDataLib.PcmData(); r.pcmData.SetFormat(NUM_CHANNELS, metaData.bitsPerSample, metaData.bitsPerSample, metaData.sampleRate, PcmDataLib.PcmData.ValueRepresentationType.SInt, metaData.totalSamples); r.pcmData.SetSampleLargeArray(pcmBytes); } } flacRW.DecodeEnd(); } catch (Exception ex) { Console.WriteLine(ex); r.pcmData = null; } } if (r.pcmData != null) { r.result = true; } else { r.result = false; } e.Result = r; }