andrewrk / libsoundio

C library for cross-platform real-time audio input and output
http://libsound.io/
MIT License
1.92k stars 229 forks source link

Recording from an output device? #229

Closed philippemnoel closed 4 years ago

philippemnoel commented 4 years ago

Hello,

Is there a feature to record from an output device? I'm making an application which must record "what the user hears" when they use a device. I've tried selecting an output device (default speakers/headphones) and using an instream but that doesn't seem compatible.

Is it supported at all? If so how can I do it, if not what would you recommend doing?

Cheers,

Philippe

Suhail commented 4 years ago

I have the same question actually!

Here's some more info:

$ ./sio_record --device "AppleHDAEngineOutput:1F,3,0,1,1:0" out.raw
Invalid device id: AppleHDAEngineOutput:1F,3,0,1,1:0

from sio_list_devices:

--------Output Devices--------

Built-in Output
  id: AppleHDAEngineOutput:1F,3,0,1,1:0
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
    48000 - 48000
    96000 - 96000
  current sample rate: 48000
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031250 sec
  max software latency: 0.08533333 sec
  current software latency: 0.01066667 sec

Scarlett 2i2 USB (default)
  id: AppleUSBAudioEngine:Focusrite:Scarlett 2i2 USB:14630000:1,2
  channel layouts:
    Stereo
  current layout: Stereo
  sample rates:
    44100 - 44100
    48000 - 48000
    88200 - 88200
    96000 - 96000
  current sample rate: 48000
  formats: signed 16-bit LE, signed 32-bit LE, float 32-bit LE, float 64-bit LE
  min software latency: 0.00031250 sec
  max software latency: 0.08533333 sec
  current software latency: 0.01066667 sec

If I do this on my Scarlett 2i2 USB (default) id: AppleUSBAudioEngine:Focusrite:Scarlett 2i2 USB:14630000:1,2

It begins recording but when I run sox: sox -r 48000 -e floating-point -b 32 -c 1 out.raw out.wav it's just silence.

I also tried sending a sine wave like so:

./sio_sine
Backend: CoreAudio
Output device: Scarlett 2i2 USB

then recording that device's output: ./sio_record --device "AppleUSBAudioEngine:Focusrite:Scarlett 2i2 USB:14630000:1,2" out.raw

then converting it: sox -r 48000 -e floating-point -b 32 -c 1 out.raw out.wav

I dropped the file in Ableton and there's no audio data. It does look like the raw file has data and it's not a bunch of null values so maybe the conversion is wrong?

Two questions:

Computer: macOS Mojave, 10.14.6, MacBook Pro 2017

Suhail commented 4 years ago

A mini update: I did manage to get this all working on Ubuntu 18.04 just fine.

philippemnoel commented 4 years ago

How did you fix the built-in output returning invalid?

Suhail commented 4 years ago

Unfortunately, I didnt. I wrote/read from dummy on Linux.

This was the correct way to convert the audio:

ffmpeg -f f32le -ar 48000 -ac 2 -c:a pcm_f32le -i out.raw out.wav -y

philippemnoel commented 4 years ago

Yeah that's what I ended up doing as well, maybe an interesting future feature?