public void GetSoundInfo(string filename, out FMODEX.SOUND_TYPE soundType, out FMODEX.SOUND_FORMAT soundFormat, out int channelCount, out int bits, out float length) { soundType = FMODEX.SOUND_TYPE.AIFF; soundFormat = FMODEX.SOUND_FORMAT.AT9; channelCount = 0; bits = 0; length = 0; uint uLength = 0; FMODEX.Sound sound = null; FMODEX.RESULT result; result = system.createSound(filename, FMODEX.MODE.HARDWARE, ref sound); CheckError(result); result = sound.getFormat(ref soundType, ref soundFormat, ref channelCount, ref bits); CheckError(result); result = sound.getLength(ref uLength, FMODEX.TIMEUNIT.MS); length = uLength / 1000f; CheckError(result); result = sound.release(); CheckError(result); }
private Stream GetStream(FMODEX.Sound sound) { IntPtr data = IntPtr.Zero; MemoryStream stream = null; try { FMODEX.RESULT result; byte[] buffer = new byte[CHUNKSIZE]; data = Marshal.AllocHGlobal(CHUNKSIZE); uint length = 0, read = 0; uint bytesread = 0; FMODEX.SOUND_FORMAT format = FMODEX.SOUND_FORMAT.AT9; FMODEX.SOUND_TYPE type = FMODEX.SOUND_TYPE.AIFF; int channels = 0, bits = 0; uint samplingrate = 0, ms = 0, pcm = 0; stream = new MemoryStream(); result = sound.getLength(ref length, FMODEX.TIMEUNIT.PCMBYTES); CheckError(result); sound.getFormat(ref type, ref format, ref channels, ref bits); CheckError(result); sound.getLength(ref ms, FMODEX.TIMEUNIT.MS); CheckError(result); sound.getLength(ref pcm, FMODEX.TIMEUNIT.PCM); CheckError(result); samplingrate = 1000 * pcm / ms; byte[] bytes = new byte[4]; stream.WriteByte((byte)'R'); stream.WriteByte((byte)'I'); stream.WriteByte((byte)'F'); stream.WriteByte((byte)'F'); stream.Write(bytes, 0, bytes.Length); stream.WriteByte((byte)'W'); stream.WriteByte((byte)'A'); stream.WriteByte((byte)'V'); stream.WriteByte((byte)'E'); stream.WriteByte((byte)'f'); stream.WriteByte((byte)'m'); stream.WriteByte((byte)'t'); stream.WriteByte((byte)' '); bytes = BitConverter.GetBytes(16); stream.Write(bytes, 0, bytes.Length); stream.WriteByte((byte)1); stream.WriteByte((byte)0); bytes = BitConverter.GetBytes((ushort)channels); stream.Write(bytes, 0, bytes.Length); bytes = BitConverter.GetBytes(samplingrate); stream.Write(bytes, 0, bytes.Length); bytes = BitConverter.GetBytes((uint)(samplingrate * channels * bits / 8)); stream.Write(bytes, 0, bytes.Length); bytes = BitConverter.GetBytes((ushort)(bits / 8 * channels)); stream.Write(bytes, 0, bytes.Length); bytes = BitConverter.GetBytes((ushort)bits); stream.Write(bytes, 0, bytes.Length); stream.WriteByte((byte)'d'); stream.WriteByte((byte)'a'); stream.WriteByte((byte)'t'); stream.WriteByte((byte)'a'); bytes = BitConverter.GetBytes(length); stream.Write(bytes, 0, bytes.Length); bytesread = 0; do { result = sound.readData(data, CHUNKSIZE, ref read); Marshal.Copy(data, buffer, 0, CHUNKSIZE); stream.Write(buffer, 0, (int)read); bytesread += read; }while (result == FMODEX.RESULT.OK && read == CHUNKSIZE); stream.Seek(4, SeekOrigin.Begin); bytes = BitConverter.GetBytes((uint)(stream.Length - 8)); stream.Write(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin); } catch { } return(stream); }