You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An C++ lib to capture specifed app's audio by it's PID number. And also sample C# app to showing usage.
I made a .dll that originally as .exe and I found it in Microsoft's github repository and can be used as an .exe application. This program capture specifed app's audio by it's PID number. So I converted it .dll and event typed; you can use it in your C# programs via P/Invoke as an event-based method. So when you call the StartCaptureAsync method from the library from C#, an event is triggered continuously with audio data until you stop it. StopCaptureAsync. I needed something like this and it was not in the NAudio library and CScore and then I developed it. I also contributed to the NAudio and CSCore library. Maybe @filoe you would like to add it into CScore as a feature/plugin.
Note that lib requires Windows 10 build 20348 or later. (Windows 11)
usingSystem.Runtime.InteropServices;usingSystem.Text;classProgram{delegatevoidAudioCallback(IntPtrdata,intlength);[DllImport("ApplicationLoopback.dll",CallingConvention=CallingConvention.StdCall)]staticexternvoidSetAudioCallback(AudioCallbackcallback);[DllImport("ApplicationLoopback.dll",CallingConvention=CallingConvention.StdCall)]staticexternIntPtrStartCaptureAsync(uintprocessId,boolincludeProcessTree,ushortchannel,uintsampleRate,ushortbitsPerSample);[DllImport("ApplicationLoopback.dll",CallingConvention=CallingConvention.StdCall)]staticexternintStopCaptureAsync();staticvoidOnAudioReceived(IntPtrdata,intlength){byte[]buffer=newbyte[length];Marshal.Copy(data,buffer,0,length);ms.Write(buffer,0,buffer.Length);// Writing PCM to temp stream to converting it to WAV later.Console.WriteLine($"Audio bytes are receiving from specifed process: {length} byte");}staticMemoryStreamms;staticvoidMain(){Console.CancelKeyPress+=newConsoleCancelEventHandler(OnCancelKeyPress);ms=newMemoryStream();SetAudioCallback(OnAudioReceived);// we are declaring our audio output event in PCM format.// 10560 was chrome's PID on my machine.StartCaptureAsync(10560,true,1,44100,16);// Process PID number and includes process tree or not.}staticvoidOnCancelKeyPress(objectsender,ConsoleCancelEventArgse){StopCaptureAsync();WavConverter.WriteWavFile(ms,"Audio.wav",44100,1,16);// We are converting PCM format to WAV.ms.Close();ms.Flush();ms.Dispose();}publicclassWavConverter{publicstaticvoidWriteWavFile(MemoryStreampcmStream,stringoutputPath,intsampleRate,shortchannels,shortbitDepth){// PCM databyte[]pcmData=pcmStream.ToArray();using(FileStreamfs=newFileStream(outputPath,FileMode.Create)){// WAV file headerWriteWavHeader(fs,pcmData.Length,sampleRate,channels,bitDepth);// PCM writingfs.Write(pcmData,0,pcmData.Length);}}privatestaticvoidWriteWavHeader(FileStreamfs,intpcmDataLength,intsampleRate,shortchannels,shortbitDepth){intblockAlign=channels*(bitDepth/8);intbyteRate=sampleRate*blockAlign;intdataChunkSize=pcmDataLength;intchunkSize=36+dataChunkSize;// "RIFF" headerfs.Write(Encoding.ASCII.GetBytes("RIFF"),0,4);fs.Write(BitConverter.GetBytes(chunkSize),0,4);fs.Write(Encoding.ASCII.GetBytes("WAVE"),0,4);// "fmt " subchunkfs.Write(Encoding.ASCII.GetBytes("fmt "),0,4);fs.Write(BitConverter.GetBytes(16),0,4);// Subchunk1Size (16 for PCM)fs.Write(BitConverter.GetBytes((short)1),0,2);// AudioFormat (1 for PCM)fs.Write(BitConverter.GetBytes(channels),0,2);// NumChannelsfs.Write(BitConverter.GetBytes(sampleRate),0,4);// SampleRatefs.Write(BitConverter.GetBytes(byteRate),0,4);// ByteRatefs.Write(BitConverter.GetBytes(blockAlign),0,2);// BlockAlignfs.Write(BitConverter.GetBytes(bitDepth),0,2);// BitsPerSample// "data" subchunkfs.Write(Encoding.ASCII.GetBytes("data"),0,4);fs.Write(BitConverter.GetBytes(dataChunkSize),0,4);// DataSize}}}
ApplicationLoopBack Plugin for NAudio and CScore
An C++ lib to capture specifed app's audio by it's PID number. And also sample C# app to showing usage.
I made a .dll that originally as .exe and I found it in Microsoft's github repository and can be used as an .exe application. This program capture specifed app's audio by it's PID number. So I converted it .dll and event typed; you can use it in your C# programs via P/Invoke as an event-based method. So when you call the
StartCaptureAsync
method from the library from C#, an event is triggered continuously with audio data until you stop it.StopCaptureAsync
. I needed something like this and it was not in the NAudio library and CScore and then I developed it. I also contributed to the NAudio and CSCore library. Maybe @filoe you would like to add it into CScore as a feature/plugin.Note that lib requires Windows 10 build 20348 or later. (Windows 11)
git repo of this project: https://github.com/qH0sT/ApplicationLoopBack
Original .exe program repo of Microsoft: https://learn.microsoft.com/en-us/samples/microsoft/windows-classic-samples/applicationloopbackaudio-sample/
The text was updated successfully, but these errors were encountered: