private 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.GetSampleArray(); int bytesPerSample = reader.BitsPerSample / 8; ad.pcm = new List <AudioDataPerChannel>(); for (int ch = 0; ch < reader.NumChannels; ++ch) { var pcmOneChannel = new byte[reader.NumFrames * bytesPerSample]; for (int i = 0; i < reader.NumFrames; ++i) { for (int b = 0; b < reader.BitsPerSample / 8; ++b) { pcmOneChannel[bytesPerSample * i + b] = interleaved[bytesPerSample * (reader.NumChannels * i + ch) + b]; } } var pcm24 = PcmDataLib.Util.ConvertTo24bit(reader.BitsPerSample, reader.NumFrames, reader.SampleValueRepresentationType, pcmOneChannel); var adp = new AudioDataPerChannel(); adp.data = pcm24; adp.offsBytes = 0; adp.bitsPerSample = 24; adp.totalSamples = ad.meta.totalSamples; ad.pcm.Add(adp); } // converted to 24bit ad.meta.bitsPerSample = 24; ad.preferredSaveFormat = FileFormatType.FLAC; return(0); } }
private void buttonStartConversion_Click(object sender, RoutedEventArgs e) { if (textBoxInputFile.Text.Length == 0) { MessageBox.Show(Properties.Resources.ErrSpecifyInputWavFile); return; } if (textBoxOutputFile.Text.Length == 0) { MessageBox.Show(Properties.Resources.ErrSpecifyOutputWavFile); return; } string pathRead = textBoxInputFile.Text; string pathWrite = textBoxOutputFile.Text; WavRWLib2.WavReader r = new WavRWLib2.WavReader(); using (var br = new BinaryReader(File.Open(pathRead, FileMode.Open, FileAccess.Read, FileShare.Read))) { if (!r.ReadHeaderAndSamples(br, 0, -1)) { string s = string.Format("エラー: ファイル読み込み失敗 {0}", pathRead); MessageBox.Show(s); Log(s + "\n"); return; } } Log(string.Format("WAVファイル読み込み。{0}\n 読み込んだファイルのfmt subchunkSize = {1}\n", pathRead, r.FmtSubChunkSize)); WavRWLib2.WavWriter w = new WavRWLib2.WavWriter(); string directory = System.IO.Path.GetDirectoryName(pathWrite); if (!System.IO.Directory.Exists(directory)) { System.IO.Directory.CreateDirectory(directory); } using (var bw = new BinaryWriter(File.Open(pathWrite, FileMode.Create, FileAccess.Write, FileShare.Write))) { w.Write(bw, r.NumChannels, r.BitsPerSample, r.ValidBitsPerSample, r.SampleRate, r.SampleValueRepresentationType, r.NumFrames, r.GetSampleArray()); } Log(string.Format("WAVファイル書き込み終了。{0}\n", pathWrite)); }
private 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.GetSampleArray(); int bytesPerSample = reader.BitsPerSample / 8; ad.pcm = new List<AudioDataPerChannel>(); for (int ch = 0; ch < reader.NumChannels; ++ch) { var pcmOneChannel = new byte[reader.NumFrames * bytesPerSample]; for (int i = 0; i < reader.NumFrames; ++i) { for (int b = 0; b < reader.BitsPerSample / 8; ++b) { pcmOneChannel[bytesPerSample * i + b] = interleaved[bytesPerSample * (reader.NumChannels * i + ch) + b]; } } var pcm24 = PcmDataLib.Util.ConvertTo24bit(reader.BitsPerSample, reader.NumFrames, reader.SampleValueRepresentationType, pcmOneChannel); var adp = new AudioDataPerChannel(); adp.data = pcm24; adp.offsBytes = 0; adp.bitsPerSample = 24; adp.totalSamples = ad.meta.totalSamples; ad.pcm.Add(adp); } // converted to 24bit ad.meta.bitsPerSample = 24; ad.preferredSaveFormat = FileFormatType.FLAC; return 0; } }
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; }