private void JingleInit(string filename) { FMOD.Sound tempSound = null; thisLock = new Object(); // Retrieve information about sound FMOD.RESULT result = system.createSound(filename, (FMOD.MODE._2D | FMOD.MODE.SOFTWARE), ref tempSound); ErrorCheck(result); FMOD.SOUND_TYPE soundType = (FMOD.SOUND_TYPE) 0; FMOD.SOUND_FORMAT soundFormat = (FMOD.SOUND_FORMAT) 0; int tempBits = 0, tempChannels = 0, tempPriority = 0; uint tempPcm = 0; float tempFrequency = 0.0f, tempPan = 0.0f, tempVolume = 0.0f; result = tempSound.getFormat(ref soundType, ref soundFormat, ref tempChannels, ref tempBits); ErrorCheck(result); result = tempSound.getDefaults(ref tempFrequency, ref tempVolume, ref tempPan, ref tempPriority); ErrorCheck(result); result = tempSound.getLength(ref tempPcm, FMOD.TIMEUNIT.PCM); ErrorCheck(result); // Fill sound parameters ChannelCount = tempChannels; Frequency = (int)tempFrequency; pcm = tempPcm; bits = tempBits; // Obtain Raw PCM data from sound GetRawData(tempSound, tempChannels, tempBits, tempPcm); // Release temp sound instance tempSound.release(); tempSound = null; FMOD.CREATESOUNDEXINFO exInfo = new FMOD.CREATESOUNDEXINFO(); exInfo.cbsize = Marshal.SizeOf(exInfo); exInfo.length = (uint)(pcm * sizeof(short)); exInfo.numchannels = ChannelCount; exInfo.format = FMOD.SOUND_FORMAT.PCM16; exInfo.defaultfrequency = (int)Frequency; // Create a stream from obtained data result = system.createStream(SoundDataBuffer.Buffer, (FMOD.MODE.OPENMEMORY | FMOD.MODE.OPENRAW), ref exInfo, ref sounds); ErrorCheck(result); } // loading jingle
public static void WriteWavHeader(FileStream stream, FMOD.Sound sound, int length, Object usingFmod) { FMOD.SOUND_TYPE type = FMOD.SOUND_TYPE.UNKNOWN; FMOD.SOUND_FORMAT format = FMOD.SOUND_FORMAT.NONE; int channels = 0, bits = 0, temp1 = 0; float rate = 0.0f; float temp = 0.0f; if (sound == null) { return; } lock (usingFmod) { sound.getFormat(ref type, ref format, ref channels, ref bits); sound.getDefaults(ref rate, ref temp, ref temp, ref temp1); } log.Info("WaveWriter.WriteWavHeader: sound format: dataLength " + length + ", type " + type + ", format " + format + ", channels " + channels + ", bits " + bits + ", rate " + rate); stream.Seek(0, SeekOrigin.Begin); FmtChunk fmtChunk = new FmtChunk(); DataChunk dataChunk = new DataChunk(); WavHeader wavHeader = new WavHeader(); RiffChunk riffChunk = new RiffChunk(); fmtChunk.chunk = new RiffChunk(); fmtChunk.chunk.id = new char[4]; fmtChunk.chunk.id[0] = 'f'; fmtChunk.chunk.id[1] = 'm'; fmtChunk.chunk.id[2] = 't'; fmtChunk.chunk.id[3] = ' '; fmtChunk.chunk.size = Marshal.SizeOf(fmtChunk) - Marshal.SizeOf(riffChunk); fmtChunk.wFormatTag = 1; fmtChunk.nChannels = (ushort)channels; fmtChunk.nSamplesPerSec = (uint)rate; fmtChunk.nAvgBytesPerSec = (uint)(rate * channels * bits / 8); fmtChunk.nBlockAlign = (ushort)(1 * channels * bits / 8); fmtChunk.wBitsPerSample = (ushort)bits; dataChunk.chunk = new RiffChunk(); dataChunk.chunk.id = new char[4]; dataChunk.chunk.id[0] = 'd'; dataChunk.chunk.id[1] = 'a'; dataChunk.chunk.id[2] = 't'; dataChunk.chunk.id[3] = 'a'; dataChunk.chunk.size = (int)length; wavHeader.chunk = new RiffChunk(); wavHeader.chunk.id = new char[4]; wavHeader.chunk.id[0] = 'R'; wavHeader.chunk.id[1] = 'I'; wavHeader.chunk.id[2] = 'F'; wavHeader.chunk.id[3] = 'F'; wavHeader.chunk.size = (int)(Marshal.SizeOf(fmtChunk) + Marshal.SizeOf(riffChunk) + length); wavHeader.rifftype = new char[4]; wavHeader.rifftype[0] = 'W'; wavHeader.rifftype[1] = 'A'; wavHeader.rifftype[2] = 'V'; wavHeader.rifftype[3] = 'E'; /* * Write out the WAV header. */ IntPtr wavHeaderPtr = Marshal.AllocHGlobal(Marshal.SizeOf(wavHeader)); IntPtr fmtChunkPtr = Marshal.AllocHGlobal(Marshal.SizeOf(fmtChunk)); IntPtr dataChunkPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dataChunk)); byte [] wavHeaderBytes = new byte[Marshal.SizeOf(wavHeader)]; byte [] fmtChunkBytes = new byte[Marshal.SizeOf(fmtChunk)]; byte [] dataChunkBytes = new byte[Marshal.SizeOf(dataChunk)]; Marshal.StructureToPtr(wavHeader, wavHeaderPtr, false); Marshal.Copy(wavHeaderPtr, wavHeaderBytes, 0, Marshal.SizeOf(wavHeader)); Marshal.StructureToPtr(fmtChunk, fmtChunkPtr, false); Marshal.Copy(fmtChunkPtr, fmtChunkBytes, 0, Marshal.SizeOf(fmtChunk)); Marshal.StructureToPtr(dataChunk, dataChunkPtr, false); Marshal.Copy(dataChunkPtr, dataChunkBytes, 0, Marshal.SizeOf(dataChunk)); stream.Write(wavHeaderBytes, 0, Marshal.SizeOf(wavHeader)); stream.Write(fmtChunkBytes, 0, Marshal.SizeOf(fmtChunk)); stream.Write(dataChunkBytes, 0, Marshal.SizeOf(dataChunk)); }
private void SaveToWav() { FileStream fs = null; int channels = 0, bits = 0; float rate = 0; IntPtr ptr1 = new IntPtr(), ptr2 = new IntPtr(); uint lenbytes = 0, len1 = 0, len2 = 0; FMOD.SOUND_TYPE type = FMOD.SOUND_TYPE.WAV; FMOD.SOUND_FORMAT format = FMOD.SOUND_FORMAT.NONE; int temp1 = 0; float temp3 = 0; if (sound == null) { return; } sound.getFormat(ref type, ref format, ref channels, ref bits); sound.getDefaults(ref rate, ref temp3, ref temp3, ref temp1); sound.getLength(ref lenbytes, FMOD.TIMEUNIT.PCMBYTES); FmtChunk fmtChunk = new FmtChunk(); DataChunk dataChunk = new DataChunk(); WavHeader wavHeader = new WavHeader(); RiffChunk riffChunk = new RiffChunk(); fmtChunk.chunk = new RiffChunk(); fmtChunk.chunk.id = new char[4]; fmtChunk.chunk.id[0] = 'f'; fmtChunk.chunk.id[1] = 'm'; fmtChunk.chunk.id[2] = 't'; fmtChunk.chunk.id[3] = ' '; fmtChunk.chunk.size = Marshal.SizeOf(fmtChunk) - Marshal.SizeOf(riffChunk); fmtChunk.wFormatTag = 1; fmtChunk.nChannels = (ushort)channels; fmtChunk.nSamplesPerSec = (uint)rate; fmtChunk.nAvgBytesPerSec = (uint)(rate * channels * bits / 8); fmtChunk.nBlockAlign = (ushort)(1 * channels * bits / 8); fmtChunk.wBitsPerSample = (ushort)bits; dataChunk.chunk = new RiffChunk(); dataChunk.chunk.id = new char[4]; dataChunk.chunk.id[0] = 'd'; dataChunk.chunk.id[1] = 'a'; dataChunk.chunk.id[2] = 't'; dataChunk.chunk.id[3] = 'a'; dataChunk.chunk.size = (int)lenbytes; wavHeader.chunk = new RiffChunk(); wavHeader.chunk.id = new char[4]; wavHeader.chunk.id[0] = 'R'; wavHeader.chunk.id[1] = 'I'; wavHeader.chunk.id[2] = 'F'; wavHeader.chunk.id[3] = 'F'; wavHeader.chunk.size = (int)(Marshal.SizeOf(fmtChunk) + Marshal.SizeOf(riffChunk) + lenbytes); wavHeader.rifftype = new char[4]; wavHeader.rifftype[0] = 'W'; wavHeader.rifftype[1] = 'A'; wavHeader.rifftype[2] = 'V'; wavHeader.rifftype[3] = 'E'; fs = new FileStream("record.wav", FileMode.Create, FileAccess.Write); /* * Write out the WAV header. */ IntPtr wavHeaderPtr = Marshal.AllocHGlobal(Marshal.SizeOf(wavHeader)); IntPtr fmtChunkPtr = Marshal.AllocHGlobal(Marshal.SizeOf(fmtChunk)); IntPtr dataChunkPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dataChunk)); byte [] wavHeaderBytes = new byte[Marshal.SizeOf(wavHeader)]; byte [] fmtChunkBytes = new byte[Marshal.SizeOf(fmtChunk)]; byte [] dataChunkBytes = new byte[Marshal.SizeOf(dataChunk)]; Marshal.StructureToPtr(wavHeader, wavHeaderPtr, false); Marshal.Copy(wavHeaderPtr, wavHeaderBytes, 0, Marshal.SizeOf(wavHeader)); Marshal.StructureToPtr(fmtChunk, fmtChunkPtr, false); Marshal.Copy(fmtChunkPtr, fmtChunkBytes, 0, Marshal.SizeOf(fmtChunk)); Marshal.StructureToPtr(dataChunk, dataChunkPtr, false); Marshal.Copy(dataChunkPtr, dataChunkBytes, 0, Marshal.SizeOf(dataChunk)); fs.Write(wavHeaderBytes, 0, Marshal.SizeOf(wavHeader)); fs.Write(fmtChunkBytes, 0, Marshal.SizeOf(fmtChunk)); fs.Write(dataChunkBytes, 0, Marshal.SizeOf(dataChunk)); /* * Lock the sound to get access to the raw data. */ sound.@lock(0, lenbytes, ref ptr1, ref ptr2, ref len1, ref len2); /* * Write it to disk. */ byte [] rawdata = new byte[len1]; Marshal.Copy(ptr1, rawdata, 0, (int)len1); fs.Write(rawdata, 0, (int)len1); /* * Unlock the sound to allow FMOD to use it again. */ sound.unlock(ptr1, ptr2, len1, len2); fs.Close(); MessageBox.Show("Written to record.wav"); }