thestk / rtaudio

A set of C++ classes that provide a common API for realtime audio input/output across Linux (native ALSA, JACK, PulseAudio and OSS), Macintosh OS X (CoreAudio and JACK), and Windows (DirectSound, ASIO, and WASAPI) operating systems.
Other
1.49k stars 317 forks source link

Question: Is it possible to get the audio data of the outputDevice #402

Closed Joel-play-lu closed 1 year ago

Joel-play-lu commented 1 year ago

Hello, I'm trying to use rtAudio with the wasapi api to analyse the audio currently playing on my speaker outputDevice.

I'm not sure if it is possible with rtAudio, the samples seem to all use input from a microphone and output the data, or I didn't understand them.

So far I tried opening the stream as output-only stream by settings the inputParameters to NULL, but the outputBuffer always seems to be empty in the callback, aka it is full of zeros 0.

I also tried to open the stream as input-only stream and settings the inputParameters with an outputDevice. This seems to work. The input buffer is full of value such as 0.00015, but it seems counter intuitive /hackish to put an outputDevice into the inputParams of the callback.

RtAudio::StreamParameters streamParams;
streamParams.deviceId = m_audio->getDefaultOutputDevice();
streamParams.nChannels = 2;
streamParams.firstChannel = 0;
unsigned int sampleRate = 44100;
m_audio->openStream(NULL, &streamParams, AUDIO_FORMAT, sampleRate, &m_bufferFrameSize, &StaticAudioCallback, this);

my callback only cast the input or output buffer as float* and output it's content to the console at the moment.

Is using an output device for the inputStream ok, or is it just a coincidence that I see data?

umlaeute commented 1 year ago

i cannot possibly comment on whether this is OK (but my gut feeling says that this is pure coincidence; I'm pretty sure that this cannot work in a cross-platform way).

Your actual backend might provide a "monitor" input device (for each output device) that can be used to query whatever is sent to the output, which i think is better solution.

Joel-play-lu commented 1 year ago

Answering my own question.

After some tests, with the sample Record.cpp in the compiled examples. I managed to record a .raw audio file using the output device as the inputParams for the callback. I used format RTAUDIO_SINT8, 44100hz and 1 channel.

The .raw file created was able to play flawlessly with the raw audio player here : https://www.francogarcia.com/en/tools/raw-audio-player/

So I assume it is supported. It is good enough for my use case anyway.