private FMOD.RESULT DSPCallback(ref FMOD.DSP_STATE dsp_state, IntPtr inbuffer, IntPtr outbuffer, uint length, int inchannels, ref int outchannels) { float[] buffer = new float[length * inchannels]; int i = 0; IntPtr currentPtr; this.inchannels = inchannels; for (i = 0; i < length * inchannels; i++) { currentPtr = new IntPtr(inbuffer.ToInt32() + (i * sizeof(float))); float sample = (float)System.Runtime.InteropServices.Marshal.PtrToStructure(currentPtr, typeof(float)); buffer[i] = LowPassFilter(sample, i % inchannels); sampleNumber++; if (sampleNumber >= sampleRate) { sampleNumber = 0; } } Marshal.Copy(buffer, 0, outbuffer, (int)length * inchannels); return(FMOD.RESULT.OK); }
unsafe public static FMOD.RESULT ReadAudioData(ref FMOD.DSP_STATE dspState, IntPtr inbuffer, IntPtr outbuffer, uint length, int inchannels, ref int outchannels) { FMOD.DSP dspInstance = new FMOD.DSP(dspState.instance); float *inb = (float *)inbuffer.ToPointer(); float *outb = (float *)outbuffer.ToPointer(); IntPtr userData; // NOTE: Without this check, Unity crashes! if (dspInstance.getUserData(out userData) == FMOD.RESULT.OK) { float *userBuffer = (float *)userData.ToPointer(); // TODO: See if there is a better way to do this. // Just copy back. for (uint samp = 0; samp < length; samp++) { /* * Feel free to unroll this. */ // TODO: There are at least 2 channels, what do we do? // Do we save the maximum, the average? userBuffer[samp] = inb[(samp * inchannels)]; for (int chan = 0; chan < outchannels; chan++) { long outIdx = (samp * outchannels) + chan; long inIdx = (samp * inchannels) + chan; float value = inb[inIdx]; outb[outIdx] = value; } } } return(FMOD.RESULT.OK); }
private RESULT ReadCallback(ref DSP_STATE dspState, IntPtr inbuffer, IntPtr outbuffer, uint length, int inchannels, ref int outchannels) { st.Stop(); var ticksPerMilisecond = Stopwatch.Frequency / 1000; var miliseconds = (double)st.ElapsedTicks / ticksPerMilisecond; //System.Diagnostics.Debug.WriteLine("miliseconds between callbacks: " + miliseconds); st.Restart(); float[] spectrum; if (this.ReadSpectrum(out spectrum)) { var noiseLevel = this.ComputeNoiseLevel(spectrum); var spectrumWithRemovedNoise = this.DecreaseSpectrumVolume(spectrum, noiseLevel); //spectrumWithRemovedNoise = spectrum; var dominant = this.ComputeDominant(spectrumWithRemovedNoise); //System.Diagnostics.Debug.WriteLine("dominant: " + dominant); var trimmedSpectrum = this.TrimSpectrumToInstrument(spectrumWithRemovedNoise); //trimmedSpectrum = spectrumWithRemovedNoise; this.NotifySoundSpectrumChangedListeners(trimmedSpectrum); this.NotifyListeners(dominant); } st.Stop(); ticksPerMilisecond = Stopwatch.Frequency/1000; miliseconds = (double) st.ElapsedTicks/ticksPerMilisecond; //System.Diagnostics.Debug.WriteLine("miliseconds of processing callback: " + miliseconds); st.Restart(); return RESULT.OK; }