public byte[] convertToBrstm(byte[] data) { string temppath = $"{DateTime.Now.ToFileTimeUtc()}.wav"; using (var input = new WaveFileReader(new MemoryStream(data))) { if (input.WaveFormat.Encoding != WaveFormatEncoding.Pcm) { var outFormat = new WaveFormat(input.WaveFormat.SampleRate, input.WaveFormat.Channels); using (var resampler = new MediaFoundationResampler(input, outFormat)) { // resampler.ResamplerQuality = 48; WaveFileWriter.CreateWaveFile(temppath, resampler); } data = File.ReadAllBytes(temppath); } } WaveReader reader = new WaveReader(); WaveStructure structure = reader.ReadMetadata(new MemoryStream(data)); AudioData audio = reader.Read(data); audio.SetLoop(true, 0, structure.SampleCount); byte[] brstmFile = new BrstmWriter().GetFile(audio); File.Delete(temppath); return(brstmFile); }
public override Common ToCommon(object structure) { WaveStructure wave = structure as WaveStructure ?? throw new InvalidDataException("Could not parse file metadata."); var format = AudioFormat.None; switch (wave.BitsPerSample) { case 16: format = AudioFormat.Pcm16; break; case 8: format = AudioFormat.Pcm8; break; } return(new Common { SampleCount = wave.SampleCount, SampleRate = wave.SampleRate, ChannelCount = wave.ChannelCount, Looping = wave.Looping, LoopStart = wave.LoopStart, LoopEnd = wave.LoopEnd, Format = format }); }
public void LoadAudio(Stream stream, IFileFormat format) { stream.Position = 0; Format = format; foreach (string ext in Format.Extension) { string extension = ext.TrimStart('*'); switch (extension) { case ".bfstm": case ".bcstm": case ".bfwav": case ".bcwav": var bcfstmReader = new BCFstmReader(); audioWithConfig = bcfstmReader.ReadWithConfig(stream); stream.Position = 0; bxstmStructure = bcfstmReader.ReadMetadata(stream); break; case ".brstm": case ".brwav": var brstmReader = new BrstmReader(); bxstmStructure = brstmReader.ReadMetadata(stream); stream.Position = 0; audioWithConfig = brstmReader.ReadWithConfig(stream); break; case ".idsp": var idspReader = new IdspReader(); idspStructure = idspReader.ReadMetadata(stream); stream.Position = 0; audioWithConfig = idspReader.ReadWithConfig(stream); break; case ".hps": var hpsReader = new HpsReader(); hpsStructure = hpsReader.ReadMetadata(stream); stream.Position = 0; audioWithConfig = hpsReader.ReadWithConfig(stream); break; case ".wav": var wavReader = new WaveReader(); waveStructure = wavReader.ReadMetadata(stream); stream.Position = 0; audioWithConfig = wavReader.ReadWithConfig(stream); break; default: throw new Exception("Unsupported Extension " + ext); } audioData = audioWithConfig.Audio; } }
/// <summary> /// Format the raw PCM sound data. /// </summary> /// <param name="stream">The memory stream that will contain the sound data.</param> /// <param name="capture">The capture provider.</param> /// <param name="rawPCMSound">The raw PCM sound data.</param> /// <param name="soundCaptureType">The sound capture type.</param> private void FormatSound(MemoryStream stream, CaptureSoundSample capture, byte[] rawPCMSound, SoundCaptureType soundCaptureType = SoundCaptureType.Wav) { switch (soundCaptureType) { case SoundCaptureType.Pcm: // Write the raw PCm sound data. stream.Write(rawPCMSound, 0, rawPCMSound.Length); stream.Flush(); break; case SoundCaptureType.Wav: default: // Write the wave formatted data from the raw PCM sound data. WaveStructure waveStructure = WaveStructure.CreateDefaultStructure(capture.Channels, capture.SampleRate, capture.BitsPerSample, rawPCMSound); WaveFormat waveFormat = new WaveFormat(); waveFormat.Write(stream, waveStructure); break; } }