bastibe / python-soundfile

SoundFile is an audio library based on libsndfile, CFFI, and NumPy
BSD 3-Clause "New" or "Revised" License
699 stars 107 forks source link

Extended data structure for subtypes #384

Open mcavalca95 opened 1 year ago

mcavalca95 commented 1 year ago

Soundfile defines formats and subtypes with str, and it is really easy to read and understand. However, it becomes a bit uncomfortable when using the module with other Python libraries. For example, I was recently exploring the PyAudio module, and the function in charge of opening the audio stream requires the "format" argument:

file = soundfile.SoundFile("120_E_Violin1_38_189_SP.wav")

# Instantiate PyAudio and initialize PortAudio system resources (1)
p = pyaudio.PyAudio()

# Open stream (2)
stream = p.open(
    format=p.get_format_from_width(4),
    channels=file.channels,
    rate=file.samplerate,
    output=True
)

The required format is an internal value of PyAudio. The user can specify the number of bytes of the format and the module will automatically get the correct value. The issue with Soundfile comes right here: the info about format and subtype are in str format, so I would need to write an if block to convert the str format into the corresponding int number, or get it directly from the str with a regular expression and some conversions. It's not impossible, but since those infos are fixed for each subtype it would be nice to have a sort of data structure that easily gives you the required data. It can be a dict or a custom Enum class, and an example of the stored data can be the following:

The previous code would be something like this:

file = soundfile.SoundFile("120_E_Violin1_38_189_SP.wav")

# Instantiate PyAudio and initialize PortAudio system resources (1)
p = pyaudio.PyAudio()

# Open stream (2)
stream = p.open(
    format=p.get_format_from_width(file.subtype.bytes),
    channels=file.channels,
    rate=file.samplerate,
    output=True
)

This is the idea in my mind, of course I don't know if it is possible or not, or if I am missing an already existing solution. However, I'm not the only programmer that came across this issue. What do you think?

P.S.: sorry for my bad english

bastibe commented 1 year ago

I would recommend using something modern instead of PyAudio, something that can natively play/record numpy arrays. Soundfile's companion SoundCard would do that, or if you'd like to stay closer to PyAudio and PortAudio, use SoundDevice.

If this does not address your question, please let me know. It seemed to me that this was the underlying issue.