bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
680 stars 69 forks source link

error during recording and playback #36

Closed Warniz closed 5 years ago

Warniz commented 5 years ago

Trying to record or playback spams this error: error during recording: -10863 and then: /Users/CWT/Documents/VÆRKER/Sound+probe/Main.py:15: RuntimeWarning: invalid value encountered in true_divide default_speaker.play(data/numpy.max(data), samplerate=48000) /anaconda3/lib/python3.6/site-packages/SoundCard-0.2.2-py3.6.egg/soundcard/coreaudio.py:366: RuntimeWarning: invalid value encountered in greater data[data>1] = 1 /anaconda3/lib/python3.6/site-packages/SoundCard-0.2.2-py3.6.egg/soundcard/coreaudio.py:367: RuntimeWarning: invalid value encountered in less data[data<-1] = -1

I can see that error code -10863 = kAudioUnitErr_CannotDoInCurrentContext. But haven't been able to find out more.

bastibe commented 5 years ago

Can you show me a code example of what you are running when the error happens?

The RuntimeWarnings are likely a result of the error.

Warniz commented 5 years ago

I'm just running the demo code:

import soundcard as sc
import numpy`

speakers = sc.all_speakers()

default_speaker = sc.default_speaker()

mics = sc.all_microphones()

default_mic = sc.default_microphone()

data = default_mic.record(samplerate=48000, numframes=48000)

default_speaker.play(data/numpy.max(data), samplerate=48000)

The -10863 error seems to be connected to the data = default_mic.record(samplerate=48000, numframes=48000) And the runtime error connected the default_speaker.play function

bastibe commented 5 years ago

What version of macOS are you running, and what kind of sound card is your default_microphone?

Warniz commented 5 years ago

I'm running 10.14.1 Mojave and i'm just using the built-in mic. So my macbooks built-in soundcard. Which is a macbook pro 2012.

bastibe commented 5 years ago

Sorry, one more question: What version of SoundCard are you running? I'm afraid I am not able to reproduce your issue on my Mac, and macOS is terribly flickle with its audio system for all kinds of cases. Are you running any audio plugins like SoundFlower?

Warniz commented 5 years ago

Running 0.2.2 and yeah I do have SoundFlower installed. But the default is set to not be a SoundFlower device.

bastibe commented 5 years ago

I have tried a different Mac, and am seeing your issue now. It appears that something broke in the update to Mojave. I am not sure, what, though.

However, it seems that the sample rate is what is causing the error message. On my computer, it runs fine if I use 44100 as the sample rate. For now, you can try using 44100 as a workaround.

I won't have access to a Mac in the next few weeks, so I won't be able to fix this in the near future. But I'll try to get this issue fixed in January.

rostyboost commented 5 years ago

FYI I hit the same issue. Mac book air 2012, High Sierra. Universal Audio Apollo Twin 2. Setting up the sample rate of the sound card to 44.1KHz and calling .record with samplerate=44100 worked. Any other sample rate fails (I tried 48KHz and 96KHz). So I don't think the bug is coming from Mojave update.

bastibe commented 5 years ago

Well, it did work for all sample rates the last time I checked, but that was pre-Mojave, and possibly pre-High-Sierra. It seems to be related to the sample rate converter unit used for recording at non-default sample rates. (Oh, if only core audio would convert sample rates automatically, like any sane audio API, and indeed their playback units... but I digress).

I currently don't have a Mac available at home, and won't have access to my work Mac until the second week of January. So any fixes will have to wait until then. Of course, if any one of you would like to take a stab at it, I will try to help you as much as I can, and gladly merge a pull request!

melvyn2 commented 5 years ago

I've tried mic.record(samplerate=44100, numframes=44100), and it's failing with this message for me.

bastibe commented 5 years ago

@melvyn2 does it work for some other sampling rate? (Are you using a special sound card?)

melvyn2 commented 5 years ago

I've only tried 44.1, 48, and 96 kHz, and none work. I've been using stream() for what I need to do and it works.

bastibe commented 5 years ago

What do you mean by

I've been using stream()?

melvyn2 commented 5 years ago

Instead of using sc.mic.record() or whatever, I've been using sd.Stream() for what I needed to do.

bastibe commented 5 years ago

This issue is not about SoundDevice, but SoundCard, a different library.

melvyn2 commented 5 years ago

whoops, sorry. I consfused the two. my original issue still stands, though.

daverz commented 5 years ago

Only settings of 44100 and 88200 in Audio MIDI Setup work for me.

OS X 10.13.4 on a MacBook Pro (Retina, 15-inch, Late 2013).

From the anaconda package listing:

anaconda 2018.12 py37_0
cffi 1.11.5 py37h6174b99_1
python 3.7.1 haf84260_7
soundcard 0.2.2 pypi_0 pypi

For the internal mic, I get the errror when sample rates of 48000 and 96000 are set in Audio MIDI Setup, but not when 44100 or 88200 are set.

Unfortunately my Umik-1 mic only offers a sample rate of 48000.

import soundcard
mic = soundcard.default_microphone()
print(mic)
data = mic.record(samplerate=48000, numframes=1024)
<Microphone Umik-1  Gain: 18dB   (2 channels)>
error during recording: -10863
error during recording: -10863
error during recording: -10863
bastibe commented 5 years ago

To be honest, I don't know what is causing this problem. It used to work fine. I'd be grateful for anyone to try to dig into the horrible, undocumented mess that is CoreAudio, to try to find the cause of this.

stswong commented 5 years ago

Any progress on this problem?

I have V0.3 installed running with Python 3.7, Mojave 10.14.4, 15" 2018 MacBook Pro. No recording works, at any sample rate, even for builtin microphone. I got the same error -10863.

When I tried it on my Windows 10 VM with Parallel on the same computer, Recording worked just fine.

bastibe commented 5 years ago

No progress yet. I didn't have time to look into it, yet. Any help would be welcome!

bastibe commented 5 years ago

I just tried to reproduce the issue on a work MacBook, and it worked without issue. (Built-in Python 3.6.1 on macOS 10.14.4). I will now try again with a more recent version of Python.

bastibe commented 5 years ago

While I was unable to reproduce your error messages on my Mac, I did find a few errors in the macOS implementation. Could you check if the latest version fixed your issues on macOS?

stswong commented 5 years ago

I did try, but still the same issue. When recording it timed out and returned with error -10863. I checked and the code means CannotDoInCurrentContext.

I tried with the Mac builtin mic and also tried with Dante Virtual Soundcard which has an ASIO driver. Builtin mic has 1 channel. I tried the ASIO drive with 2 channels and also with 8 channels.

I tried both 48kHz and 44.1kHz.

Thanks.

From: Bastian Bechtold notifications@github.com Reply-To: bastibe/SoundCard reply@reply.github.com Date: Monday, May 13, 2019 at 6:52 AM To: bastibe/SoundCard SoundCard@noreply.github.com Cc: Sam Wong sam@bbinet.com, Comment comment@noreply.github.com Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

While I was unable to reproduce this exact issue, I did find a few errors in the macOS implementation. Could you check if the latest version fixed your issues on macOS?

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bastibe/SoundCard/issues/36?email_source=notifications&email_token=ADNMHL6S2VEZ6DJXOU2KICDPVFW77A5CNFSM4GKVHO72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVIL55I#issuecomment-491831029, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADNMHL3VPSETWNSHDUNGZCTPVFW77ANCNFSM4GKVHO7Q.

bastibe commented 5 years ago

OK, that's disappointing. Now we'll have to find the difference between our systems.

Could you try again with the SoundFlower virtual sound card, to try to make this as reproducible as possible?

When recording it timed out and returned with error -10863. I checked and the code means CannotDoInCurrentContext.

Yeah, that's what core audio always says if it doesn't like anything. I wish it told us why it can't do it in the current context on your machine, but apparently has no such qualms on my Mac. Context here typically means "during playback" or "in the stream callback". Probably, it means "can not call AudioUnitRender in the RenderCallback" (line 801 in coreaudio.py).

I'd be grateful for any information on this. Could you try adding some debugging code to the input_callback and print the values of its arguments? Whether it's the first call that fails or a later one?

Are you running this from a bare Python prompt, or as part of an IDE of some kind? Are you playing any other audio in the background? Are you using the built-in system Python or another version? If so, what version of Python are you using? Also, my computer told me it couldn't do block sizes larger than 512, but one example up above seemed to have used 1024 without issue.

I would really like to find and fix this bug.

stswong commented 5 years ago

Here is a screen copy. Seems like we are passing a NULL pointer to the AudioUnitRender…

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)

[Clang 6.0 (clang-600.0.57)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

import soundcard as sc

mic = sc.get_microphone("Dante")

mic

<Microphone Dante Virtual Soundcard (8 channels)>

data = mic.record(samplerate=48000,numframes=16)

userdata = <cdata 'void *' NULL>

actionflags = <cdata 'unsigned int *' 0x70000e2dfc90>

timestamp = <cdata 'AudioTimeStamp *' 0x1103a00a0>

busnumber = 1

numframes = 512

bufferlist = <cdata 'AudioBufferList *' owning 24 bytes>

error during recording: -10863

From: Bastian Bechtold notifications@github.com Reply-To: bastibe/SoundCard reply@reply.github.com Date: Monday, May 13, 2019 at 10:54 AM To: bastibe/SoundCard SoundCard@noreply.github.com Cc: Sam Wong sam@bbinet.com, Comment comment@noreply.github.com Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

OK, that's disappointing. Now we'll have to find the difference between our systems.

Could you try again with the SoundFlower virtual sound card, to try to make this as reproducible as possible?

When recording it timed out and returned with error -10863. I checked and the code means CannotDoInCurrentContext.

Yeah, that's what core audio always says if it doesn't like anything. I wish it told us why it can't do it in the current context on your machine, but apparently has no such qualms on my Mac. Context here typically means "during playback" or "in the stream callback". Probably, it means "can not call AudioUnitRender in the RenderCallback" (line 801 in coreaudio.py).

I'd be grateful for any information on this. Could you try adding some debugging code to the input_callback and print the values of its arguments? Whether it's the first call that fails or a later one?

Are you running this from a bare Python prompt, or as part of an IDE of some kind? Are you playing any other audio in the background? Are you using the built-in system Python or another version? If so, what version of Python are you using? Also, my computer told me it couldn't do block sizes larger than 512, but one example up above seemed to have used 1024 without issue.

I would really like to find and fix this bug.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bastibe/SoundCard/issues/36?email_source=notifications&email_token=ADNMHL73DZ5RVAQTIFA22LLPVGTMLA5CNFSM4GKVHO72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVJCJCQ#issuecomment-491922570, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADNMHL33GJG3BFJCWF6TTHDPVGTMLANCNFSM4GKVHO7Q.

stswong commented 5 years ago

userdata is not used. So it isn’t the problem, I guess. I don’t know much about CFFI. Does actionflags look suspicious to you? 12 hex digits? How do you print out the value of timestamp, actionflags, etc, instead of printing out the point address?

From: Sam Wong sam@bbinet.com Date: Monday, May 13, 2019 at 12:13 PM To: bastibe/SoundCard reply@reply.github.com, bastibe/SoundCard SoundCard@noreply.github.com Cc: Comment comment@noreply.github.com Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

Here is a screen copy. Seems like we are passing a NULL pointer to the AudioUnitRender…

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)

[Clang 6.0 (clang-600.0.57)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

import soundcard as sc

mic = sc.get_microphone("Dante")

mic

<Microphone Dante Virtual Soundcard (8 channels)>

data = mic.record(samplerate=48000,numframes=16)

userdata = <cdata 'void *' NULL>

actionflags = <cdata 'unsigned int *' 0x70000e2dfc90>

timestamp = <cdata 'AudioTimeStamp *' 0x1103a00a0>

busnumber = 1

numframes = 512

bufferlist = <cdata 'AudioBufferList *' owning 24 bytes>

error during recording: -10863

From: Bastian Bechtold notifications@github.com Reply-To: bastibe/SoundCard reply@reply.github.com Date: Monday, May 13, 2019 at 10:54 AM To: bastibe/SoundCard SoundCard@noreply.github.com Cc: Sam Wong sam@bbinet.com, Comment comment@noreply.github.com Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

OK, that's disappointing. Now we'll have to find the difference between our systems.

Could you try again with the SoundFlower virtual sound card, to try to make this as reproducible as possible?

When recording it timed out and returned with error -10863. I checked and the code means CannotDoInCurrentContext.

Yeah, that's what core audio always says if it doesn't like anything. I wish it told us why it can't do it in the current context on your machine, but apparently has no such qualms on my Mac. Context here typically means "during playback" or "in the stream callback". Probably, it means "can not call AudioUnitRender in the RenderCallback" (line 801 in coreaudio.py).

I'd be grateful for any information on this. Could you try adding some debugging code to the input_callback and print the values of its arguments? Whether it's the first call that fails or a later one?

Are you running this from a bare Python prompt, or as part of an IDE of some kind? Are you playing any other audio in the background? Are you using the built-in system Python or another version? If so, what version of Python are you using? Also, my computer told me it couldn't do block sizes larger than 512, but one example up above seemed to have used 1024 without issue.

I would really like to find and fix this bug.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bastibe/SoundCard/issues/36?email_source=notifications&email_token=ADNMHL73DZ5RVAQTIFA22LLPVGTMLA5CNFSM4GKVHO72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVJCJCQ#issuecomment-491922570, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADNMHL33GJG3BFJCWF6TTHDPVGTMLANCNFSM4GKVHO7Q.

bastibe commented 5 years ago

Thank you! I will compare these to the numbers I get on my computer when I get back to work tomorrow (that's where the computer is at).

Just to make sure, could you show me actionflags[0] and timestamp[0] (dereference the pointers)?

stswong commented 5 years ago

Here are the lines added:

        print("channels = ", channels)
        print("userdata = ", userdata)
        print("actionflags[0] = ", actionflags[0])
        print("timestamp[0] = ", timestamp[0])
        print("busnumber = ", busnumber)
        print("numframes = ", numframes)
        print("bufferlist.mNumberBuffers = ", bufferlist.mNumberBuffers)
        print("bufferlist.mBuffers[0].mNumberChannels = ", bufferlist.mBuffers[0].mNumberChannels)
        print("bufferlist.mBuffers[0].mDataByteSize = ", bufferlist.mBuffers[0].mDataByteSize)

Here are the outputs:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)

[Clang 6.0 (clang-600.0.57)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

import soundcard as sc

mic = sc.get_microphone("Dante")

mic.record(samplerate=48000,numframes=16)

channels = 8

userdata = <cdata 'void *' NULL>

actionflags[0] = 0

timestamp[0] = <cdata 'AudioTimeStamp &' 0x11e9430a0>

busnumber = 1

numframes = 512

bufferlist.mNumberBuffers = 1

bufferlist.mBuffers[0].mNumberChannels = 8

bufferlist.mBuffers[0].mDataByteSize = 16384

error during recording: -10863

array([[-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.],

   [-0., -0., -0., -0., -0., -0., -0., -0.]])

From: Bastian Bechtold notifications@github.com Reply-To: bastibe/SoundCard reply@reply.github.com Date: Monday, May 13, 2019 at 1:37 PM To: bastibe/SoundCard SoundCard@noreply.github.com Cc: Sam Wong sam@bbinet.com, Comment comment@noreply.github.com Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

Thank you! I will compare these to the numbers I get on my computer when I get back to work tomorrow (that's where the computer is at).

Just to make sure, could you show me actionflags[0] and timestamp[0] (dereference the pointers)?

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bastibe/SoundCard/issues/36?email_source=notifications&email_token=ADNMHL3ZFTRYYNGE4LPYCL3PVHGP3A5CNFSM4GKVHO72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVJPP5I#issuecomment-491976693, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADNMHL5RYLFXFLXEHMVNTOTPVHGP3ANCNFSM4GKVHO7Q.

bastibe commented 5 years ago

Thank you. This is frustrating, your example works on my machine:

>>> soundcard.get_microphone("Soundflower64").record(samplerate=48000, channels=8, numframes=16)
channels =  8
userdata =  <cdata 'void *' NULL>
actionflags[0] =  0
timestamp[0] =  <cdata 'AudioTimeStamp &' 0x10c2150a0>
busnumber =  1
numframes =  512
bufferlist.mNumberBuffers =  1
bufferlist.mBuffers[0].mNumberChannels =  8
bufferlist.mBuffers[0].mDataByteSize =  16384

(I used Soundflower, as this is my only 8-channel device. But it works on my computer for every other sound card as well.)

bastibe commented 5 years ago

Does it work if you return 0 instead of the status code from AudioUnitRender? Perhaps the error only happens once, and we just have to ignore that initial error, or something like that.

stswong commented 5 years ago

I installed SoundFlower and it works with SoundFlower but not native Macbook Pro mic nor with Dante Virtual Soundcard (a ASIO drive, I believe)

From: Bastian Bechtold notifications@github.com Reply-To: bastibe/SoundCard reply@reply.github.com Date: Monday, May 13, 2019 at 11:47 PM To: bastibe/SoundCard SoundCard@noreply.github.com Cc: Sam Wong sam@bbinet.com, Comment comment@noreply.github.com Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

Does it work if you return 0 instead of the status code from AudioUnitRender? Perhaps the error only happens once, and we just have to ignore that initial error, or something like that.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bastibe/SoundCard/issues/36?email_source=notifications&email_token=ADNMHL5JFEKLDGH2L3XQM3DPVJN7PA5CNFSM4GKVHO72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVKN5FY#issuecomment-492101271, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADNMHLYKTRAYODNE6TOQAR3PVJN7PANCNFSM4GKVHO7Q.

stswong commented 5 years ago

Can you try the builtin MBP mic to see if it worked for you? It generated more errors when I ignored the -10863 error and returned 0.

From: Sam Wong Sent: Tuesday, May 14, 2019 12:13 AM To: bastibe/SoundCard; bastibe/SoundCard Cc: Comment Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

I installed SoundFlower and it works with SoundFlower but not native Macbook Pro mic nor with Dante Virtual Soundcard (a ASIO drive, I believe)

From: Bastian Bechtold notifications@github.com Reply-To: bastibe/SoundCard reply@reply.github.com Date: Monday, May 13, 2019 at 11:47 PM To: bastibe/SoundCard SoundCard@noreply.github.com Cc: Sam Wong sam@bbinet.com, Comment comment@noreply.github.com Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

Does it work if you return 0 instead of the status code from AudioUnitRender? Perhaps the error only happens once, and we just have to ignore that initial error, or something like that.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bastibe/SoundCard/issues/36?email_source=notifications&email_token=ADNMHL5JFEKLDGH2L3XQM3DPVJN7PA5CNFSM4GKVHO72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVKN5FY#issuecomment-492101271, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADNMHLYKTRAYODNE6TOQAR3PVJN7PANCNFSM4GKVHO7Q.

bastibe commented 5 years ago

The MacBook Pro Mic works for me. However, I connected a USB sound card, and finally, I get the error as well!

Thank you very much for helping me get to this point! Now that I can reproduce the error, I can get started looking for causes.

bastibe commented 5 years ago

I think I found the issue: Could you check the value of actionflags[0] after the call to AudioUnitRender?

On my machine, it is 16, which corresponds to _cac.kAudioUnitRenderAction_OutputIsSilence. So it's not actually an error, but simply a lack of input, which we could ignore and return zeros.

Would this make sense in your case?

bastibe commented 5 years ago

The latest commit fixes the OutputIsSilence issue. Can you confirm that this solves your problem?

stswong commented 5 years ago

I did a print after the AudioUnitRender call and actionflags[0] always return 16 and status is the user error. It just always return an array of zeros.

From: Bastian Bechtold [mailto:notifications@github.com] Sent: Wednesday, May 15, 2019 6:04 AM To: bastibe/SoundCard Cc: Sam Wong; Comment Subject: Re: [bastibe/SoundCard] error during recording and playback (#36)

The latest commit fixes the OutputIsSilence issue. Can you confirm that this solves your problem?

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/bastibe/SoundCard/issues/36?email_source=notifications&email_token=ADNMHL6REKTN447R4OVTUZ3PVQC4HA5CNFSM4GKVHO72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVOSYNI#issuecomment-492645429, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADNMHLYTRO3AFSLP3TXGKLDPVQC4HANCNFSM4GKVHO7Q.