bastibe / SoundCard

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

multithreading using soundcard #49

Closed mbatt closed 4 years ago

mbatt commented 5 years ago

I am new to GitHub, I wanted to share my experience regarding sounddevice python I use sounddevice python to control beihringer fca1616 sound card. I am able to send tone to any output in my card (device), but when trying to send a different frequency tone to each of my outputs( 8 outputs) I had to use multithreading which makes my pc crash. I used ASIO API and i could send a same tone to all 8 outputs but my goal is to send different tones to each device (output) any multithreading soundcard example ?

gonzalocasas commented 5 years ago

@mbatt which OS are you using? I use soundcard with mutiprocessing and works very well, nothing special in the setup thou, it's just regular multiprocessing.

mbatt commented 5 years ago

I use windows 10, finally I was able to send tones to differents devices (outputs) of my device in a separate python processes, but I faced a new weird issue. when I send a tone of 1khz , the frequency is doubled. I use spectralissime to view the signal it's 2khz.

bastibe commented 5 years ago

Did you set the samplerate? Could you show us a complete code example of what you were trying to do?

mbatt commented 5 years ago

Here is the code mport sounddevice as sd import soundfile as sf import numpy as np from audio_constants import from wasapi_device_index import

""" create a numpy array from a .wav file""" def wave2numpy(file): data,fs = sf.read(file, dtype = 'float32') return data

def open_output_stream(index): out_stream = sd.OutputStream( device=index, dtype="float32",channels = 2 ) out_stream.start() return out_stream

def play_audio_on_channel(audio_data, stream_object): stream_object.write(audio_data)

"""here i call the function""" wasapi_exclusive = sd.WasapiSettings(exclusive=True) play_audio_on_channel(wave2numpy("Tone_750Hz_44100_600s.wav"),open_output_stream(30))

This is my device querry output: ['28', 'FCA1616', 'Line', '7/8', '(Behringer', 'FCA1616', 'WDM),', 'Windows', 'WASAPI', '(0', 'in,', '2', 'out)'] ['29', 'FCA1616', 'Line', '1/2', '(Behringer', 'FCA1616', 'WDM),', 'Windows', 'WASAPI', '(0', 'in,', '2', 'out)'] ['30', 'FCA1616', 'Line', '3/4', '(Behringer', 'FCA1616', 'WDM),', 'Windows', 'WASAPI', '(0', 'in,', '2', 'out)'] ['32', 'FCA1616', 'Line', '5/6', '(Behringer', 'FCA1616', 'WDM),', 'Windows', 'WASAPI', '(0', 'in,', '2', 'out)']

mbatt commented 5 years ago

In the previous code I stream out a 750 Hz tone but it is played as a 1500Hz tone when I use the play function I get the right frequency

def play_tone(file,channel,loop): data,fs = sf.read(file,dtype = 'float32') sd.play(data,fs,loop=loop, blocking = False, device=channel,never_drop_input=None) status = sd.wait() sd.stop()

play_tone("Tone_440Hz_44100_600s.wav", 30,False)

bastibe commented 5 years ago

You are using SoundDevice. This issue board is about SoundCard. These libraries are not the same.