bastibe / PySoundCard

PySoundCard is an audio library based on PortAudio, CFFI and NumPy
BSD 3-Clause "New" or "Revised" License
87 stars 9 forks source link

Writing Audio Output To File #65

Open ghost opened 6 years ago

ghost commented 6 years ago

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!

bastibe commented 6 years ago

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".

ghost commented 6 years ago

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?

bastibe commented 6 years ago

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.