public float[][] GetWaveData() { int numchannels = 0; int dummy = 0; FMOD.SOUND_FORMAT dummyformat = FMOD.SOUND_FORMAT.NONE; FMOD.DSP_RESAMPLER dummyresampler = FMOD.DSP_RESAMPLER.LINEAR; int count = 0; const int WAVEDATASIZE = 512; system.getSoftwareFormat(ref dummy, ref dummyformat, ref numchannels, ref dummy, ref dummyresampler, ref dummy); float[][] wavedata = new float[numchannels][]; /* * DRAW WAVEDATA */ for (count = 0; count < numchannels; count++) { wavedata[count] = new float[WAVEDATASIZE]; if (currentChannel != null) { currentChannel.getWaveData(wavedata[count], WAVEDATASIZE, count); } } return(wavedata); }
private void drawSpectrum(Graphics g) { int numchannels = 0; int dummy = 0; FMOD.SOUND_FORMAT dummyformat = FMOD.SOUND_FORMAT.NONE; FMOD.DSP_RESAMPLER dummyresampler = FMOD.DSP_RESAMPLER.LINEAR; int count = 0; int count2 = 0; system.getSoftwareFormat(ref dummy, ref dummyformat, ref numchannels, ref dummy, ref dummyresampler, ref dummy); /* * DRAW SPECTRUM */ for (count = 0; count < numchannels; count++) { float max = 0; system.getSpectrum(spectrum, SPECTRUMSIZE, count, FMOD.DSP_FFT_WINDOW.TRIANGLE); for (count2 = 0; count2 < 255; count2++) { if (max < spectrum[count2]) { max = spectrum[count2]; } } /* * The upper band of frequencies at 44khz is pretty boring (ie 11-22khz), so we are only * going to display the first 256 frequencies, or (0-11khz) */ for (count2 = 0; count2 < 255; count2++) { float height; height = spectrum[count2] / max * GRAPHICWINDOW_HEIGHT; if (height >= GRAPHICWINDOW_HEIGHT) { height = GRAPHICWINDOW_HEIGHT - 1; } if (height < 0) { height = 0; } height = GRAPHICWINDOW_HEIGHT - height; g.FillRectangle(mBrushGreen, count2, height, 1.0f, GRAPHICWINDOW_HEIGHT - height); } } }
// ======================================================================================================================================== #region Unity lifecycle void Start() { this.gameObjectName = this.gameObject.name; result = FMOD.Factory.System_Create(out system); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "FMOD.Factory.System_Create"); result = system.getVersion(out version); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.getVersion"); if (version < FMOD.VERSION.number) { var msg = string.Format("FMOD lib version {0} doesn't match header version {1}", version, FMOD.VERSION.number); throw new System.Exception(msg); } int rate; FMOD.SPEAKERMODE sm; int sc; result = system.getSoftwareFormat(out rate, out sm, out sc); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.getSoftwareFormat"); AudioStreamSupport.LOG(LogLevel.INFO, this.logLevel, this.gameObjectName, null, "FMOD samplerate: {0}, speaker mode: {1}, num. of raw speakers {2}", rate, sm, sc); // TODO: evaluate maxchannels result = system.init(32, FMOD.INITFLAGS.NORMAL, extradriverdata); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.init"); this.SetOutput(this.outputDriverID); /* tags ERR_FILE_COULDNOTSEEK: * http://stackoverflow.com/questions/7154223/streaming-mp3-from-internet-with-fmod * http://www.fmod.org/docs/content/generated/FMOD_System_SetFileSystem.html */ // result = system.setFileSystem(null, null, null, null, null, null, -1); // ERRCHECK(result, "system.setFileSystem"); // Explicitly create the delegate object and assign it to a member so it doesn't get freed // by the garbage collected while it's being used this.pcmreadcallback = new FMOD.SOUND_PCMREADCALLBACK(PCMReadCallback); this.pcmsetposcallback = new FMOD.SOUND_PCMSETPOSCALLBACK(PCMSetPosCallback); this.elementSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.Int16)); // decodebuffersize samples worth of bytes will be called in read callback // createSound calls back, too this.pcmReadCallbackBuffer = new List <List <byte> >(); this.pcmReadCallbackBuffer.Add(new List <byte>()); this.pcmReadCallbackBuffer.Add(new List <byte>()); }
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); }
protected virtual IEnumerator Start() { this.gameObjectName = this.gameObject.name; /* * Create a System object and initialize. */ result = FMOD.Factory.System_Create(out system); ERRCHECK(result, "Factory.System_Create"); result = system.getVersion(out version); ERRCHECK(result, "system.getVersion"); if (version < FMOD.VERSION.number) { var msg = string.Format("FMOD lib version {0} doesn't match header version {1}", version, FMOD.VERSION.number); LOG(LogLevel.ERROR, msg); if (this.OnError != null) { this.OnError.Invoke(this.gameObjectName, msg); } throw new System.Exception(msg); } /* * initial internal FMOD samplerate should be 48000 on desktop; we change it on the sound only when stream requests it. */ result = system.setSoftwareFormat(48000, this.speakerMode, this.numOfRawSpeakers); ERRCHECK(result, "system.setSoftwareFormat"); int rate; FMOD.SPEAKERMODE sm; int smch; result = system.getSoftwareFormat(out rate, out sm, out smch); ERRCHECK(result, "system.getSoftwareFormat"); LOG(LogLevel.INFO, "FMOD samplerate: {0}, speaker mode: {1}, num. of raw speakers {2}", rate, sm, smch); // must be be4 init on iOS ... //result = system.setOutput(FMOD.OUTPUTTYPE.NOSOUND); //ERRCHECK(result, "system.setOutput"); if (this is AudioStreamMinimal) { result = system.init(8, FMOD.INITFLAGS.NORMAL, extradriverdata); } else { result = system.init(8, FMOD.INITFLAGS.STREAM_FROM_UPDATE, extradriverdata); } ERRCHECK(result, "system.init"); /* Increase the file buffer size a little bit to account for Internet lag. */ result = system.setStreamBufferSize(streamBufferSize, FMOD.TIMEUNIT.RAWBYTES); ERRCHECK(result, "system.setStreamBufferSize"); /* tags ERR_FILE_COULDNOTSEEK: * http://stackoverflow.com/questions/7154223/streaming-mp3-from-internet-with-fmod * http://www.fmod.org/docs/content/generated/FMOD_System_SetFileSystem.html */ result = system.setFileSystem(null, null, null, null, null, null, -1); ERRCHECK(result, "system.setFileSystem"); if (this.playOnStart) { this.Play(); } yield return(null); this.ready = true; }