bastibe/PySoundCard

Passing additional arguments to callback function?

Closed this issue · 2 comments

MrMho commented

Hi,

first off, thanks for this awesome contribution. It makes real-time audio analysis very comfortable.

However, I am wondering, when using callback mode, is it possible to pass arguments other than those given in the signature to the callback function?

Callback function signature from the documentation of pysoundcard.Stream:
"callback(input, output, time, status) -> flag"

Thanks in advance!

No, that is not possible. However, if you want to refer to other stuff from within the callback function, just use a closure or a functor. Any callable will do as callback.

MrMho commented

Thanks! Closures did the trick for me.

In case somebody is curious how it works, here is some example code:

from pysoundcard import Stream, continue_flag
import time

def make_callback(test_argument):
    def callback(in_data, out_data, time_info, status):
        print test_argument
        return continue_flag
    return callback

test_argument = 5 # argument which should be passed to the callback function 
callback = make_callback(test_argument)
s = Stream(samplerate=44100, blocksize=8192, callback=callback)
s.start()
time.sleep(5)
s.stop()