Skip to content

Conversation

@jkarthic
Copy link

@jkarthic jkarthic commented Apr 9, 2024

Mac's CoreAudio API doesn't support capture of system audio. But the newly introduced ScreenCaptureKit API support capturing of system audio. This will require the user to provide screen sharing permissions though.

This is the initial version that supports only mono capture and blocking APIs.
But it can be easily extended to support other features as well.

@fightingforyou46
Copy link

I have successfully built and replaced libportaudio.2.dylib into my directory
sounddevice/portaudio/ and renamed it to libportaudio.dylib. After checking, there was no loopback device added to the audio device list

@jkarthic
Copy link
Author

I have successfully built and replaced libportaudio.2.dylib into my directory sounddevice/portaudio/ and renamed it to libportaudio.dylib. After checking, there was no loopback device added to the audio device list

@fightingforyou46
I tested with the following standard commands and it lists the loopback device for me.

git clone https://github.com/get-wrecked/portaudio/
cd portaudio
git checkout mac_screen_capture_kit
mkdir build
cd build
cmake .. -DPA_BUILD_TESTS=ON
cmake --build . --config Release
test/patest_enumerate_default_latency

System Audio [Loopback] is the name of this new device.

Could you please try the above steps just to rule out any issues with your test or build setup.
Also this Screen Capture Kit API will need MacOS 13.0 or above.

@fightingforyou46
Copy link

fightingforyou46 commented Apr 11, 2024

I saw it like this:

|_. Bad Default Latency? |_. Device Number |_. I/O Channels |_. Device Name |_. Host API |_. Default High Input Latency |_. Default Low Input Latency |_. Default High Output Latency |_. Default Low Output latency |
|   |   0 | 2/0 |  == Built-in Line Input == | Core Audio |   0.0141 |   0.0039 |   0.1000 |   0.0100 |
|   |   1 | 2/0 |  == Built-in Digital Input == | Core Audio |   0.0130 |   0.0037 |   0.1000 |   0.0100 |
|   |   2 | 0/2 |  == Built-in Output == | Core Audio |   0.1000 |   0.0100 |   0.0141 |   0.0039 |
|   |   3 | 0/2 |  == Built-in Line Output == | Core Audio |   0.1000 |   0.0100 |   0.0141 |   0.0039 |
|   |   4 | 0/2 |  == Built-in Digital Output == | Core Audio |   0.1000 |   0.0100 |   0.0141 |   0.0039 |
| X |   5 | 1/0 |  == System Audio [Loopback] == | Mac ScreenCaptureKit |   0.0000 |   0.0000 |   0.0000 |   0.0000 |

and I used it , I got this error: Error opening InputStream: Can't write to a callback stream [PaErrorCode -9976]

@jkarthic
Copy link
Author

I got this error: Error opening InputStream: Can't write to a callback stream [PaErrorCode -9976]

So, I see the device is being listed correctly.
Which app are you using to test audio capture? Could you please provide a sample app code, with which you are seeing this error. Just to clarify, this is a output stream(similar to a Mic stream) and not a input stream. You will have to read from it, and not write into it(as your error suggests). Also please note that this doesn't yet support callback(as mentioned in the PR description). You will have to use the blocking API a.k.a., Pa_ReadStream.

@fightingforyou46
Copy link

I use it with python sounddevice. I just replace libportaudio.dylib in sounddevice/portaudio_binary/ then use it with my code

@RossBencina RossBencina marked this pull request as draft April 19, 2024 22:30
@RossBencina
Copy link
Collaborator

Thanks for your contribution. This seems to be at the proof-of-concept stage. Are you planning on fleshing this out and maintaining it? For now I'm marking this as draft. We're not going to merge this until it is more complete. The following issues are apparent:

  1. The CI is failing
  2. No PortAudio callback API support
  3. No stereo capture

The first two definitely need to be addressed. The last would need to be justified.

@philburk
Copy link
Collaborator

A callback API should be possible. Screen Capture Kit API doc says:

When a stream captures a new audio or video sample buffer, it calls the stream output’s stream:didOutputSampleBuffer:ofType: method, passing it the captured data and an indicator of its type.

@RossBencina
Copy link
Collaborator

@jkarthic could you please rebase this on the latest master, since we've fixed the issue that was causing Mac CI to fail.

Also, awaiting your response to our other comments. Thank you.

@jkarthic jkarthic force-pushed the mac_screen_capture_kit branch from d55b1b0 to 151b8f8 Compare May 12, 2024 11:37
@jkarthic jkarthic force-pushed the mac_screen_capture_kit branch from 151b8f8 to 28acd77 Compare May 12, 2024 11:39
@jkarthic
Copy link
Author

jkarthic commented May 12, 2024

@RossBencina
Sorry for the delayed reply. And thanks for reviewing this PR. Actually I am not looking to maintain this feature actively, as the changes I have done so far seems to suffice my usecase so far. I just wanted to ensure that the work I had done so far benefits the very nice PortAudio community as well.
My goal was to just push a rudimentary version of Mac loopback capture working so that the community can take it forward from there, based on the contributor's requirements and priorities.
Nevertheless I have rebased and added the callback API support as it seems to be the minimum requirement for adding a device API in PortAudio. But I am not planning to add the stereo support, as it is not part of my current requirements.

@jkarthic
Copy link
Author

Also all the CI tests seems to passing for the dummy PR I created in my repo. get-wrecked#1

@RossBencina
Copy link
Collaborator

Thanks for clarifying and for your contribution. Right now we don't think it is ready to merge. We don't want to discard your good work, so we will keep this PR around as a draft.

Right now we're not going to merge this because:

  1. It is unfinished (e.g. no stereo, possibly other things) and there is no current prospect of it being bought up to standard.
  2. No one has yet stepped up to maintain it and I'm not sure there is anyone who will.

If anyone would like to step up and complete and maintain this we would be happy for this new host API to reach our master branch when it's ready.

- Changed the sample rate supported to only 48000. Other sampling rates seem to have some issue.
- Reduced the sleep time for lower latency
- Reduced the buffer size for lower latency
@DonaldTrump-2020
Copy link

@jkarthic I have tested the newest commit of you. here is the resuilt:

[100%] Built target paloopback
|_. Bad Default Latency? |_. Device Number |_. I/O Channels |_. Device Name |_. Host API |_. Default High Input Latency |_. Default Low Input Latency |_. Default High Output Latency |_. Default Low Output latency |
|   |   0 | 2/0 |  == Built-in Line Input == | Core Audio |   0.0141 |   0.0039 |   0.1000 |   0.0100 |
|   |   1 | 2/0 |  == Built-in Digital Input == | Core Audio |   0.0130 |   0.0037 |   0.1000 |   0.0100 |
|   |   2 | 0/2 |  == Built-in Output == | Core Audio |   0.1000 |   0.0100 |   0.0141 |   0.0039 |
|   |   3 | 0/2 |  == Built-in Line Output == | Core Audio |   0.1000 |   0.0100 |   0.0141 |   0.0039 |
|   |   4 | 0/2 |  == Built-in Digital Output == | Core Audio |   0.1000 |   0.0100 |   0.0141 |   0.0039 |
| X |   5 | 1/0 |  == System Audio [Loopback] == | Mac ScreenCaptureKit |   0.0000 |   0.0000 |   0.0000 |   0.0000 |

@arkadijs
Copy link
Contributor

Curious, as if macOS could indicate that only audio is captured and not the screen?

Screenshot 2024-08-28 at 7 28 19 PM

@jkarthic jkarthic force-pushed the mac_screen_capture_kit branch from b7d03e9 to 7349eb7 Compare October 11, 2024 15:21
@philburk philburk added the P4 Priority: Low label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P4 Priority: Low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants