private unsafe static short[] captureSamples(IntPtr captureDevice, int frequency, int sampleCount) { short[] samples = new short[sampleCount]; int samplesTaken = 0; bool isRecording = false; double?previousRMS = null; short[] buffer = new short[frequency]; fixed(int *samplesAvailable = new int[1]) fixed(short *bufferPointer = buffer) { while (samplesTaken < sampleCount) { IntPtr samplesAvailablePointer = new IntPtr((void *)samplesAvailable); Alc.GetIntegerv(captureDevice, Alc.EnumCaptureSamples, 1, samplesAvailablePointer); int samplesAvailableCount = samplesAvailable[0]; const int captureCount = 2048; if (samplesAvailableCount > captureCount) { Alc.CaptureSamples(captureDevice, (void *)bufferPointer, samplesAvailableCount); // TODO: ... do something with the buffer double rms = calculateRMS(buffer, samplesAvailableCount); double maxAmplitude = short.MaxValue; double intensity = rms / maxAmplitude; printIntensity(intensity); // compare current rms with previous values to find beginning of a keyword const double factor = 10; if (rms > previousRMS * factor) // beginning of window { isRecording = true; //Console.Beep(10000, 10); } if (isRecording) { for (int i = 0; i < samplesAvailableCount; i++) { samples[samplesTaken++] = buffer[i]; if (samplesTaken == sampleCount) { goto done; } } } previousRMS = rms; } } } done: resetConsoleSettings(); return(samples.ToArray()); }