public PCMStream(byte[] wavData) { _allocatedHGlobal = Marshal.AllocHGlobal(wavData.Length); Marshal.Copy(wavData, 0, _allocatedHGlobal, wavData.Length); RIFFHeader *header = (RIFFHeader *)_allocatedHGlobal; _bps = header->_fmtChunk._bitsPerSample; _numChannels = header->_fmtChunk._channels; _frequency = (int)header->_fmtChunk._samplesSec; _numSamples = (int)(header->_dataChunk._chunkSize / header->_fmtChunk._blockAlign); _source = (short *)((byte *)_allocatedHGlobal + header->GetSize()); _samplePos = 0; _looped = false; _loopStart = 0; _loopEnd = _numSamples; smplLoop[] loops = header->_smplLoops; if (loops.Length > 0) { _looped = true; _loopStart = (int)loops[0]._dwStart; _loopEnd = (int)loops[0]._dwEnd; } }
internal PCMStream(FileMap map) { _sourceMap = map; RIFFHeader *header = (RIFFHeader *)_sourceMap.Address; _bps = header->_fmtChunk._bitsPerSample; _numChannels = header->_fmtChunk._channels; _frequency = (int)header->_fmtChunk._samplesSec; _numSamples = (int)(header->_dataChunk._chunkSize / header->_fmtChunk._blockAlign); _source = (short *)(_sourceMap.Address + header->GetSize()); _samplePos = 0; _looped = false; _loopStart = 0; _loopEnd = _numSamples; smplLoop[] loops = header->_smplLoops; if (loops.Length > 0) { _looped = true; _loopStart = (int)loops[0]._dwStart; _loopEnd = (int)loops[0]._dwEnd; } }
public static byte[] ToByteArray(IAudioStream source, int samplePosition = 0, int maxSampleCount = int.MaxValue, bool appendSmplChunk = false) { int sampleCount = Math.Min(maxSampleCount, (source.Samples - samplePosition)); //Estimate size int outLen = 44 + (sampleCount * source.Channels * 2); if (appendSmplChunk && source.IsLooping) { outLen += 68; } //Create byte array byte[] wavData = new byte[outLen]; fixed(byte *address = wavData) { RIFFHeader *riff = (RIFFHeader *)address; *riff = new RIFFHeader(1, source.Channels, 16, source.Frequency, sampleCount); source.SamplePosition = samplePosition; source.ReadSamples(address + 44, sampleCount); if (appendSmplChunk && source.IsLooping) { riff->_length += 68; smplChunk *smpl = (smplChunk *)(address + outLen - 68); * smpl = new smplChunk { _chunkTag = smplChunk.smplTag, _chunkSize = 60, _dwManufacturer = 0, _dwProduct = 0, _dwSamplePeriod = 0, _dwMIDIUnityNote = 0, _dwMIDIPitchFraction = 0, _dwSMPTEFormat = 0, _dwSMPTEOffset = 0, _cSampleLoops = 1, _cbSamplerData = 0 }; smplLoop *loop = (smplLoop *)(address + outLen - 24); * loop = new smplLoop { _dwIdentifier = 0, _dwType = 0, _dwStart = (uint)source.LoopStartSample, _dwEnd = (uint)source.LoopEndSample, _dwFraction = 0, _dwPlayCount = 0 }; } } return(wavData); }
internal PCMStream(FileMap map) { _sourceMap = map; RIFFHeader *header = (RIFFHeader *)_sourceMap.Address; _bps = header->_fmtChunk._bitsPerSample; _numChannels = header->_fmtChunk._channels; _frequency = (int)header->_fmtChunk._samplesSec; _numSamples = (int)(header->_dataChunk._chunkSize / header->_fmtChunk._blockAlign); _source = (short *)(_sourceMap.Address + header->GetSize()); _samplePos = 0; }
public static void ToFile(IAudioStream source, string path) { using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.SequentialScan)) { //Estimate size int outLen = 44 + (source.Samples * source.Channels * 2); //Create file map stream.SetLength(outLen); using (FileMap map = FileMap.FromStreamInternal(stream, FileMapProtect.ReadWrite, 0, outLen)) { RIFFHeader *riff = (RIFFHeader *)map.Address; * riff = new RIFFHeader(1, source.Channels, 16, source.Frequency, source.Samples); source.SamplePosition = 0; source.ReadSamples(map.Address + 44, source.Samples); } } }
internal PCMStream(FileMap map) { _sourceMap = map; RIFFHeader *header = (RIFFHeader *)_sourceMap.Address; //May be more subchunks between RIFF and data, so search for data. dataChunk realDataChunk = new dataChunk(); VoidPtr dataPtr = 0; for (int i = 0; i < _sourceMap.Length; i++) { byte *dat = (byte *)_sourceMap.Address.address; dat += i; if (i == 0xa6) { i = 0xa6; } if (*dat == 'd') { if (*(++dat) == 'a' && *(++dat) == 't' && *(++dat) == 'a') { realDataChunk._chunkTag = *( uint * )dat - 3; realDataChunk._chunkSize = *( uint * )(dat + 1); dataPtr = dat + 5; break; } } } _bps = header->_fmtChunk._bitsPerSample; _numChannels = header->_fmtChunk._channels; _frequency = (int)header->_fmtChunk._samplesSec; _numSamples = (int)(realDataChunk._chunkSize / header->_fmtChunk._blockAlign); _source = ( short * )(dataPtr); _samplePos = 0; }