bastibe / SoundCard

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

provide alternative to context manager style stream #55

Closed irmen closed 5 years ago

irmen commented 5 years ago

I'm attempting to integrate soundcard as a backend library in my own sound api. This means I'm wrapping various classes in my own.

Recorder and Stream are only context managers and this is a little problematic, because that doesn't mix well with code that is using them over a longer period of time and across many scopes; I don't have a single localized place where I can use a context manager for a short time. I want to be able to keep the backend stream open all the time (until my api is closed)

I can do this by manually calling __enter__() and __exit__() but it would be nice if there also is a more traditional init() / close() method pattern or something similar.

edit: Actually, I managed to solve this without having to jump through some hoops, by using a loop that pulls data from elsewhere , running within the context manager, in a thread. But it still is a bit convoluted

bastibe commented 5 years ago

I would recommend using __enter__ and __exit__. That's what they are meant for. Adding an additional set of non-underscored enter and exit functions that called the existing functions would not add any useful functionality, but merely duplicate existing code. Such wrappers are better implemented in client-side code instead of the SoundCard API.

irmen commented 5 years ago

I can agree with that