Exemplo n.º 1
0
        /// <summary>
        /// Konvertieren von von Sound Dateien zu WAVE mit PCM Header
        /// </summary>
        /// <param name="src">Kompletter Pfad zur Quell Datei</param>
        /// <param name="dest">Kompletter Pfad zur Ziel Datei</param>
        /// <returns></returns>
        public bool ConvertToWAVE(string src_file, string dest_file, ConvertToWaveProgressDelegate convertToWaveProgressDelegate)
        {
            uint version = 0;

            FMOD.RESULT result;
            const int   CHUNKSIZE = 262144;

            FMOD.System system = null;
            FMOD.Sound  sound  = null;

            /*
             *  Global Settings
             */
            result = FMOD.Factory.System_Create(ref system);
            if (result != FMOD.RESULT.OK)
            {
                return(false);
            }

            result = system.getVersion(ref version);
            ERRCHECK(result);
            if (version < FMOD.VERSION.number)
            {
                //MessageBox.Show("Error!  You are using an old version of FMOD " + version.ToString("X") + ".  This program requires " + FMOD.VERSION.number.ToString("X") + ".");
                return(false);
            }

            result = system.init(1, FMOD.INITFLAGS.NORMAL, (IntPtr)null);
            ERRCHECK(result);

            result = system.createSound(src_file, FMOD.MODE.OPENONLY | FMOD.MODE.ACCURATETIME, ref sound);

            if (result != FMOD.RESULT.OK)
            {
                return(false);
            }


            /*
             *  Decode the sound and write it to a .raw file.
             */
            IntPtr data = Marshal.AllocHGlobal(CHUNKSIZE);

            byte[] buffer = new byte[CHUNKSIZE];
            uint   length = 0, read = 0;
            uint   bytesread = 0;

            FileStream fs = new FileStream(dest_file, FileMode.Create, FileAccess.Write);

            result = sound.getLength(ref length, FMOD.TIMEUNIT.PCMBYTES);
            ERRCHECK(result);

            bytesread = 0;
            do
            {
                result = sound.readData(data, CHUNKSIZE, ref read);

                Marshal.Copy(data, buffer, 0, CHUNKSIZE);

                fs.Write(buffer, 0, (int)read);

                bytesread += read;

                double percent = 100.0 / (double)length * (double)bytesread;

                convertToWaveProgressDelegate(src_file, dest_file, percent);
                System.Windows.Forms.Application.DoEvents();
                //statusBar.Text = "writing " + bytesread + " bytes of " + length + " to output.raw";
            }while (result == FMOD.RESULT.OK && read == CHUNKSIZE);

            //statusBar.Text = "done";

            /*
             *  Loop terminates when either
             *  1. the read function returns an error.  (ie FMOD_ERR_FILE_EOF etc).
             *  2. the amount requested was different to the amount returned. (somehow got an EOF without the file error, maybe a non stream file format like mod/s3m/xm/it/midi).
             *
             *  If 'bytesread' is bigger than 'length' then it just means that FMOD miscalculated the size,
             *  but this will not usually happen if FMOD_ACCURATETIME is used.  (this will give the correct length for VBR formats)
             */

            fs.Close();


            /*
             *  Shut down
             */
            if (sound != null)
            {
                result = sound.release();
                ERRCHECK(result);
            }
            if (system != null)
            {
                result = system.close();
                ERRCHECK(result);
                result = system.release();
                ERRCHECK(result);
            }

            return(true);
        }
Exemplo n.º 2
0
        private void playButton_Click(object sender, System.EventArgs e)
        {
            uint version = 0;

            FMOD.RESULT result;

            /*
             *  Global Settings
             */
            result = FMOD.Factory.System_Create(ref system);
            ERRCHECK(result);

            result = system.getVersion(ref version);
            ERRCHECK(result);
            if (version < FMOD.VERSION.number)
            {
                MessageBox.Show("Error!  You are using an old version of FMOD " + version.ToString("X") + ".  This program requires " + FMOD.VERSION.number.ToString("X") + ".");
                Application.Exit();
            }

            result = system.init(1, FMOD.INITFLAGS.NORMAL, (IntPtr)null);
            ERRCHECK(result);

            result = system.createSound("../../../../../examples/media/wave.mp3", FMOD.MODE.OPENONLY | FMOD.MODE.ACCURATETIME, ref sound);
            ERRCHECK(result);

            /*
             *  Decode the sound and write it to a .raw file.
             */
            {
                IntPtr data = Marshal.AllocHGlobal(CHUNKSIZE);
                byte[] buffer = new byte[CHUNKSIZE];
                uint   length = 0, read = 0;
                uint   bytesread = 0;

                FileStream fs = new FileStream("output.raw", FileMode.Create, FileAccess.Write);

                result = sound.getLength(ref length, FMOD.TIMEUNIT.PCMBYTES);
                ERRCHECK(result);

                bytesread = 0;
                do
                {
                    result = sound.readData(data, CHUNKSIZE, ref read);

                    Marshal.Copy(data, buffer, 0, CHUNKSIZE);

                    fs.Write(buffer, 0, (int)read);

                    bytesread += read;

                    statusBar.Text = "writing " + bytesread + " bytes of " + length + " to output.raw";
                }while(result == FMOD.RESULT.OK && read == CHUNKSIZE);

                statusBar.Text = "done";

                /*
                 *  Loop terminates when either
                 *  1. the read function returns an error.  (ie FMOD_ERR_FILE_EOF etc).
                 *  2. the amount requested was different to the amount returned. (somehow got an EOF without the file error, maybe a non stream file format like mod/s3m/xm/it/midi).
                 *
                 *  If 'bytesread' is bigger than 'length' then it just means that FMOD miscalculated the size,
                 *  but this will not usually happen if FMOD_ACCURATETIME is used.  (this will give the correct length for VBR formats)
                 */

                fs.Close();


                /*
                 *  Shut down
                 */
                if (sound != null)
                {
                    result = sound.release();
                    ERRCHECK(result);
                }
                if (system != null)
                {
                    result = system.close();
                    ERRCHECK(result);
                    result = system.release();
                    ERRCHECK(result);
                }
            }
        }
Exemplo n.º 3
0
        private float BPMDetection(string filename)
        {
            float bpm = 0.0f;

            const int CHUNKSIZE = 4096;

            FMOD.RESULT result;
            FMOD.System systemBpm = null;
            FMOD.Sound  soundBpm  = null;

            // Create a System object and initialize.
            result = FMOD.Factory.System_Create(ref systemBpm);
            ERRCHECK(result);

            // Zuerst in eine WAVE-Umwandeln
            result = systemBpm.init(1, FMOD.INITFLAGS.NORMAL, (IntPtr)null);
            ERRCHECK(result);

            result = systemBpm.createSound(filename, FMOD.MODE.OPENONLY | FMOD.MODE.ACCURATETIME, ref soundBpm);
            ERRCHECK(result);

            /*
             *  Decode the sound and write it to a .raw file.
             */
            {
                IntPtr data = Marshal.AllocHGlobal(CHUNKSIZE);
                byte[] buffer = new byte[CHUNKSIZE];
                uint   length = 0, read = 0;
                uint   bytesread = 0;

                MemoryStream memStream = new MemoryStream();

                result = soundBpm.getLength(ref length, FMOD.TIMEUNIT.PCMBYTES);
                ERRCHECK(result);

                bytesread = 0;
                do
                {
                    result = soundBpm.readData(data, CHUNKSIZE, ref read);

                    Marshal.Copy(data, buffer, 0, CHUNKSIZE);

                    memStream.Write(buffer, 0, (int)read);

                    bytesread += read;
                }while (result == FMOD.RESULT.OK && read == CHUNKSIZE);

                /*
                 *  Loop terminates when either
                 *  1. the read function returns an error.  (ie FMOD_ERR_FILE_EOF etc).
                 *  2. the amount requested was different to the amount returned. (somehow got an EOF without the file error, maybe a non stream file format like mod/s3m/xm/it/midi).
                 *
                 *  If 'bytesread' is bigger than 'length' then it just means that FMOD miscalculated the size,
                 *  but this will not usually happen if FMOD_ACCURATETIME is used.  (this will give the correct length for VBR formats)
                 */

                memStream.Close();

                // In float umwandeln
                byte[]  samples      = memStream.ToArray();
                float[] floatSamples = new float[samples.Length / 2];
                //for (int i = 0; i < samples.Length; i += 4)
                for (int i = samples.Length / 4; i < samples.Length * 3 / 4; i += 4)
                {
                    short sampleLeft  = BitConverter.ToInt16(samples, i);
                    short sampleRight = BitConverter.ToInt16(samples, i + 2);

                    floatSamples[i / 2]     = (float)sampleLeft / 32768.0f;
                    floatSamples[i / 2 + 1] = (float)sampleRight / 32768.0f;
                }

                BigMansStuff.PracticeSharp.Core.SoundTouchSharp soundTouch = new BigMansStuff.PracticeSharp.Core.SoundTouchSharp();

                bpm = soundTouch.DetectBPM(2, 44100, floatSamples, floatSamples.Length);

                /*
                 *  Shut down
                 */
                if (soundBpm != null)
                {
                    result = soundBpm.release();
                    ERRCHECK(result);
                }
                if (systemBpm != null)
                {
                    result = systemBpm.close();
                    ERRCHECK(result);
                    result = systemBpm.release();
                    ERRCHECK(result);
                }
            }

            return(bpm);
        }