bastibe/PySoundCard

Writing Audio Output To File

Opened this issue · 3 comments

I found this library when searching for a way to capture audio output from speakers that would happen from listening to say a song or video on the pc and send it to another pc via a file. How could I edit the callback example to write to a file because when I attempt to write to a .raw file like so,
outFile = open("rawOut.raw",mode="w")
out_data[:] = in_data
outFile.write(in_data)
I get an error,
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cffi/model.py:532: UserWarning: 'enum PaHostApiTypeId' has no values explicitly defined; guessing that it is equivalent to 'unsigned int'
% self._get_c_name())
From cffi callback <function Stream.init..callback_wrapper at 0x1068d06a8>:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pysoundcard.py", line 685, in callback_wrapper
return callback(idata, odata, _time2dict(time), status)
File "loopback.py", line 8, in callback
outFile.write(in_data)
TypeError: write() argument must be str, not numpy.ndarray
Thanks in advance!

I would use SoundFile to write audio data to files.

If you don't want to do that for some reason, you'd have to use in_data.tostring(), since, as the error message says, "write() argument must be str, not numpy.ndarray".

I'll give SoundFile a look but if I were to use the in_data.tostring() approach wouldn't the data still not be in the .raw format? It would just be a sequence of arrays that couldn't be played right?

That depends on your raw file format. By default, PySoundCard will give you 64 bit floating point data. RAW is not a predefined format, it is whatever you write into it. Your above example will produce a 64 bit floating point raw file.

RAW files can never be played directly, without specifying the format.