Ejemplo n.º 1
0
        public bool AddSound(byte[] data, string dicname)
        {
            if (sounds.ContainsKey(dicname))
            {
                return(true);
            }
            try
            {
                // first write to temp file
                var temp = Path.GetTempFileName();
                File.WriteAllBytes(temp, data);

                FMODEX.Sound  sound = null;
                FMODEX.RESULT result;
                result = system.createSound(temp, FMODEX.MODE.OPENONLY | FMODEX.MODE.ACCURATETIME, ref sound);
                CheckError(result);
                var stream = GetStream(sound);
                AddSound(stream, dicname);
                sound.release();
                if (File.Exists(temp))
                {
                    File.Delete(temp);
                }
                return(true);
            }
            catch
            {
                MessageBox.Show(PPDExceptionContentProvider.Provider.GetContent(PPDExceptionType.SoundReadError));
                return(false);
            }
        }
Ejemplo n.º 2
0
 public bool AddSound(string filename, string dicname)
 {
     if (!File.Exists(filename))
     {
         return(false);
     }
     if (sounds.ContainsKey(dicname))
     {
         return(true);
     }
     try
     {
         FMODEX.Sound  sound = null;
         FMODEX.RESULT result;
         result = system.createSound(filename, FMODEX.MODE.OPENONLY | FMODEX.MODE.ACCURATETIME, ref sound);
         CheckError(result);
         var stream = GetStream(sound);
         sound.release();
         CheckError(result);
         AddSound(stream, dicname);
         return(true);
     }
     catch
     {
         MessageBox.Show(PPDExceptionContentProvider.Provider.GetContent(PPDExceptionType.SoundReadError));
         return(false);
     }
 }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }