Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] "audio switching" with OpenSesame

edited September 2015 in OpenSesame

Dear fellow Coders,

I'm new to OpenSesame and have a question for the more experienced coders. To provide a little background: We employ auditory localization tasks in our experiments. Typically, two (different) sounds are simultaneously presented from two (out of x) speakers. The specific speakers are defined on a trial-by-trial basis. Therefore, I'm looking for a program that can "send" the sound signal to different speakers.

We previously had a hardware solution for the issue: Specific speakers were defined in another software (LiveCode) and transmitted to a (custom made) audio switch device via the COM-port. The audio switch device then send the left/right channel of a stereo file to the specified speakers. I wonder if it is possible to circumvent the audio switch device (which is no longer available) and directly specify sound channels (e.g. via a sound card or an audio interface) with OpenSesame.

Any ideas? Thanks in advance for any suggestions. I'm mainly interested if OpenSesame is capable of such an operation.

Cheers,
Malte

Edit: The feature we are (desperately) looking for is similar to the "Sound Mixer" implemented e.g. in the Presentation environment.

Comments

  • edited 4:46AM

    Hi Malte,

    If there is a Python package somewhere out there that can do this operation it will most likely also work from within OpenSesame. Unfortunately, I have no clue whether it exists, or anything specific about how to tackle the problem.

    Sorry,

    Eduard

    Buy Me A Coffee

  • edited 4:46AM

    Hi Eduard,

    it's a good thing to hear that it is (generally) possible. I came across a rather convenient solution for the "present from multiple speakers"-part of the problem: Following this post, it can be easily done for mono/stereo files by adding more entries to the channel_map in PyAudio. The (slightly modified) code looks like this:

    import pyaudio
    import wave
    import sys
    
    chunk = 4096
    
    PyAudio = pyaudio.PyAudio
    
    if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)
    
    wf = wave.open(sys.argv[1], 'rb')
    
    p = PyAudio()
    
    channel_map = (0, 1, -1, -1, -1, -1, -1, -1)
    
    try:
    stream_info = pyaudio.PaMacCoreStreamInfo(
    flags=pyaudio.PaMacCoreStreamInfo.paMacCorePlayNice, # default
    channel_map=channel_map)
    except AttributeError:
    print("Sorry, couldn't find PaMacCoreStreamInfo. Make sure that "
    "you're running on Mac OS X.")
    sys.exit(-1)
    
    print("Stream Info Flags:", stream_info.get_flags())
    print("Stream Info Channel Map:", stream_info.get_channel_map())
    print("channels",wf.getnchannels())
    
    print('sample width',wf.getsampwidth())
    
    stream = p.open(
    format=p.get_format_from_width(wf.getsampwidth()),
    channels=wf.getnchannels(),
    rate=wf.getframerate(),
    output=True,
    output_host_api_specific_stream_info=stream_info)
    
    data = wf.readframes(chunk)
    
    while data != '':
    stream.write(data)
    data = wf.readframes(chunk)
    
    stream.stop_stream()
    stream.close()
    
    p.terminate()
    

    Is it possible to implement PyAudio in OpenSesame by manually editing the code of e.g., a sampler item? Or would that conflict with the ways sounds are played by OpenSesame?

    If it is possible, can someone provide an example of how it can be done? I don't mind digging some more into libraries/modules but providing an example for the code in question would help me (as a novice) a lot.

    Thanks again!

    Malte

  • edited 4:46AM

    Is it possible to implement PyAudio in OpenSesame by manually editing the code of e.g., a sampler item? Or would that conflict with the ways sounds are played by OpenSesame?

    Sure, you can add arbitrary Python code using the inline_script item. If you search this forum for 'pyaudio', you'll find various examples of people using pyaudio this way.

    Cheers!
    Sebastiaan

  • edited 4:46AM

    Hi Sebastiaan,

    thanks for the quick reply! I will try to make the modified code from PyAudio work in OpenSesame. I will share any but (hopefully positive) outcome of it in the forum.

    Thanks again!

    Malte

  • edited December 2015

    Hello OpenSesameTeam,

    I m trying to implement an inline script (separate item) to play a wav file using the script that I found here http://people.csail.mit.edu/hubert/pyaudio/.
    I get an IO error in the following line:

    wf = wave.open('drip.wav', 'rb')

    (The drip.wav file is located on the same folder as my script.)

    My o/p:

    Error while executing inline script

    phase: run
    item: inline_script
    line: 159
    exception message:
    exception type: IOError

    Traceback:
    File "/Applications/OpenSesame.app/Contents/Resources/lib/python2.7/libopensesame/inline_script.py", line 173, in run
    File "/Applications/OpenSesame.app/Contents/Resources/lib/python2.7/libopensesame/python_workspace.py", line 111, in _exec
    Inline_script, line 20, in
    File "wave.pyc", line 509, in open
    File "wave.pyc", line 160, in init
    IOError: [Errno 2] No such file or directory: 'drip.wav'

    Any ideas how to resolve this issue of IOError?
    Thank you very much,
    V

  • edited 4:46AM

    Hi V,

    it would be help if you could provide the complete inline script that causes the error. At first glance, it seems that the file could not be retrieved. You might try to specify a path to "drip.wav".

    Cheers,

    Malte

  • edited 4:46AM

    Dear Malte,
    thank you very much for your response, I resolved the issue. It is necessary to specify the absolute path of the file.
    Take care!

Sign In or Register to comment.