private void timer_Tick(object sender, System.EventArgs e) { if (system != null && sound != null) { uint recordpos = 0; uint playpos = 0; bool recording = false; bool playing = false; system.isRecording(selected, ref recording); system.getRecordPosition(selected, ref recordpos); if (channel != null) { channel.isPlaying(ref playing);; channel.getPosition(ref playpos, FMOD.TIMEUNIT.PCM); } statusBar.Text = "State:" + (recording ? playing ? " Recording / playing " : " Recording " : playing ? " Playing " : " Idle ") + " Record pos = " + recordpos + " Play pos = " + playpos + " Loop " + (looping ? "On" : "Off"); } if (system != null) { system.update(); } }
private void timer_Tick(object sender, System.EventArgs e) { FMOD.RESULT result; if (recording) { uint recordpos = 0; result = system.getRecordPosition(selected, ref recordpos); ERRCHECK(result); if (recordpos != lastrecordpos) { IntPtr ptr1 = IntPtr.Zero, ptr2 = IntPtr.Zero; int blocklength; uint len1 = 0, len2 = 0; blocklength = (int)recordpos - (int)lastrecordpos; if (blocklength < 0) { blocklength += (int)soundlength; } /* * Lock the sound to get access to the raw data. */ sound.@lock(lastrecordpos * 4, (uint)blocklength * 4, ref ptr1, ref ptr2, ref len1, ref len2); /* *4 = stereo 16bit. 1 sample = 4 bytes. */ /* * Write it to disk. */ if (ptr1 != IntPtr.Zero && len1 > 0) { byte[] buf = new byte[len1]; Marshal.Copy(ptr1, buf, 0, (int)len1); datalength += (int)len1; fs.Write(buf, 0, (int)len1); } if (ptr2 != IntPtr.Zero && len2 > 0) { byte[] buf = new byte[len2]; Marshal.Copy(ptr2, buf, 0, (int)len2); datalength += (int)len2; fs.Write(buf, 0, (int)len2); } /* * Unlock the sound to allow FMOD to use it again. */ sound.unlock(ptr1, ptr1, len1, len2); } lastrecordpos = recordpos; statusBar.Text = "Record buffer pos = " + recordpos + " Record time = " + datalength / 44100 / 4 / 60 + ":" + (datalength / 44100 / 4) % 60; } if (system != null) { system.update(); } }
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); } }