Ejemplo n.º 1
0
        private void start_Click(object sender, System.EventArgs e)
        {
            FMOD.RESULT result;

            result = system.recordStart(selected, sound, true);
            ERRCHECK(result);

            soundlength   = 0;
            lastrecordpos = 0;
            datalength    = 0;

            fs = new FileStream("record.wav", FileMode.Create, FileAccess.Write);

            /*
             *  Write out the wav header.  As we don't know the length yet it will be 0.
             */
            WriteWavHeader(0);

            recording = true;

            result = sound.getLength(ref soundlength, FMOD.TIMEUNIT.PCM);
            ERRCHECK(result);

            start.Enabled = false;
        }
Ejemplo n.º 2
0
        private void recordButton_Click(object sender, System.EventArgs e)
        {
            FMOD.RESULT result;

            result = system.recordStart(selected, sound, looping);
            ERRCHECK(result);
        }
Ejemplo n.º 3
0
        private void comboBoxRecord_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
            FMOD.RESULT            result;
            FMOD.DSP_RESAMPLER     resampler = FMOD.DSP_RESAMPLER.MAX;
            int selected = comboBoxRecord.SelectedIndex;
            int temp     = 0;

            FMOD.SOUND_FORMAT format = FMOD.SOUND_FORMAT.NONE;

            result = system.setSoftwareFormat(OUTPUTRATE, FMOD.SOUND_FORMAT.PCM16, 1, 0, 0);
            ERRCHECK(result);

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

            result = system.getSoftwareFormat(ref outputfreq, ref format, ref temp, ref temp, ref resampler, ref temp);
            ERRCHECK(result);

            /*
             *  Create a sound to record to.
             */
            exinfo.cbsize           = Marshal.SizeOf(exinfo);
            exinfo.numchannels      = 1;
            exinfo.format           = FMOD.SOUND_FORMAT.PCM16;
            exinfo.defaultfrequency = OUTPUTRATE;
            exinfo.length           = (uint)(exinfo.defaultfrequency * 2 * exinfo.numchannels * 5);

            result = system.createSound((string)null, (FMOD.MODE._2D | FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER), ref exinfo, ref sound);
            ERRCHECK(result);

            comboBoxOutput.Enabled   = false;
            comboBoxPlayback.Enabled = false;
            comboBoxRecord.Enabled   = false;

            /*
             *  Start recording
             */
            result = system.recordStart(selected, sound, true);
            ERRCHECK(result);

            Thread.Sleep(200);      /* Give it some time to record something */

            result = system.playSound(FMOD.CHANNELINDEX.REUSE, sound, false, ref channel);
            ERRCHECK(result);

            /* Dont hear what is being recorded otherwise it will feedback.  Spectrum analysis is done before volume scaling in the DSP chain */
            result = channel.setVolume(0);
            ERRCHECK(result);
        }
Ejemplo n.º 4
0
        IEnumerator RecordCR()
        {
            int recRate = 0;
            int namelen = 255;

            System.Text.StringBuilder name = new System.Text.StringBuilder(namelen);
            System.Guid       guid;
            FMOD.SPEAKERMODE  speakermode;
            FMOD.DRIVER_STATE driverstate;
            result = system.getRecordDriverInfo(this.recordDeviceId, name, namelen, out guid, out recRate, out speakermode, out recChannels, out driverstate);
            ERRCHECK(result, "system.getRecordDriverInfo");

            // compensate the input rate for the current output rate
            this.GetComponent <AudioSource>().pitch = ((float)(recRate * recChannels) / (float)(AudioSettings.outputSampleRate * (int)AudioSettings.speakerMode));

            exinfo                  = new FMOD.CREATESOUNDEXINFO();
            exinfo.numchannels      = recChannels;
            exinfo.format           = FMOD.SOUND_FORMAT.PCM16;
            exinfo.defaultfrequency = recRate;
            exinfo.length           = (uint)(recRate * sizeof(short) * recChannels);

            result = system.createSound(string.Empty, FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER, ref exinfo, out sound);
            ERRCHECK(result, "system.createSound");

            this.GetComponent <AudioSource>().Play();

            result = system.recordStart(this.recordDeviceId, sound, true);
            ERRCHECK(result, "system.recordStart");

            result = sound.getLength(out soundlength, FMOD.TIMEUNIT.PCM);
            ERRCHECK(result, "sound.getLength");

            this.isRecording = true;

            if (this.OnRecordingStarted != null)
            {
                this.OnRecordingStarted.Invoke(this.gameObjectName);
            }

            for (;;)
            {
                result = system.update();
                ERRCHECK(result, "system.update", false);

                if (this.isPaused)
                {
                    yield return(null);
                }

                uint recordpos = 0;

                system.getRecordPosition(this.recordDeviceId, out recordpos);
                ERRCHECK(result, "system.getRecordPosition");

                if (recordpos != lastrecordpos)
                {
                    int blocklength;

                    blocklength = (int)recordpos - (int)lastrecordpos;
                    if (blocklength < 0)
                    {
                        blocklength += (int)soundlength;
                    }

                    /*
                     * Lock the sound to get access to the raw data.
                     */
                    result = sound.@lock((uint)(lastrecordpos * exinfo.numchannels * 2), (uint)(blocklength * exinfo.numchannels * 2), out ptr1, out ptr2, out len1, out len2);   /* * exinfo.numchannels * 2 = stereo 16bit.  1 sample = 4 bytes. */

                    /*
                     * Write it to output.
                     */
                    if (ptr1.ToInt64() != 0 && len1 > 0)
                    {
                        datalength += len1;
                        byte[] barr = new byte[len1];
                        Marshal.Copy(ptr1, barr, 0, (int)len1);

                        this.AddBytesToOutputBuffer(barr);
                    }
                    if (ptr2.ToInt64() != 0 && len2 > 0)
                    {
                        datalength += len2;
                        byte[] barr = new byte[len2];
                        Marshal.Copy(ptr2, barr, 0, (int)len2);
                        this.AddBytesToOutputBuffer(barr);
                    }

                    /*
                     * Unlock the sound to allow FMOD to use it again.
                     */
                    result = sound.unlock(ptr1, ptr2, len1, len2);
                }

                lastrecordpos = recordpos;

                // print(string.Format("Record buffer pos = {0} : Record time = {1}:{2}", recordpos, datalength / exinfo.defaultfrequency / exinfo.numchannels / 2 / 60, (datalength / exinfo.defaultfrequency / exinfo.numchannels / 2) % 60));

                // System.Threading.Thread.Sleep(10);

                yield return(null);
            }
        }