Python Sounddevice implementation sounds choppy

64 views Asked by At

I am currently playing around with the python library sounddevice. Now I have the problem that with the following implementation the sound sounds choppy, as if there was an underflow or clipping. Here is my code:

from queue import Queue


class Buffer:
    def __init__(self):
        self.q = Queue()

    def get_q(self):
        return self.q
from audio.buffer.buffer import Buffer

import sounddevice as sd


class Input:
    def __init__(self, input_device):
        self.input_device = input_device
        self.device_settings = sd.query_devices(input_device)
        self.input_stream = sd.InputStream(device=input_device, callback=self.read_data, blocksize=32768, dtype="int16")
        self.buffer = Buffer()

    def read_data(self, indata, frames, time, status):
        if status:
            print(status, flush=True)
        print(f"indata: {indata}")
        self.buffer.get_q().put(indata)

    def start_stream(self):
        self.input_stream.start()

    def stop_stream(self):
        self.input_stream.stop()

import sounddevice as sd


class Output:
    def __init__(self, output_device):
        self.output_device = output_device
        self.device_settings = sd.query_devices(output_device)
        self.input_stream = sd.OutputStream(device=output_device, callback=self.write_data, blocksize=32768, dtype="int16")
        self.output_buffer = None

    def write_data(self, outdata, frames, time, status):
        if status:
            print(status, flush=True)
        q = self.output_buffer.get_q()
        if q.empty():
            print("Q empty", flush=True)
        else:
            data = q.get_nowait()
            print(f"outdata: {data}")
            outdata[:] = data

    def start_stream(self, output_buffer):
        self.output_buffer = output_buffer
        self.input_stream.start()

    def stop_stream(self):
        self.input_stream.stop()
        self.output_buffer = None
from audio.input.input import Input
from audio.output.output import Output
import sounddevice as sd


input1 = Input(2)
output1 = Output(2)

input1.start_stream()
output1.start_stream(input1.buffer)

Debug output:

indata: [[ 0  0]
 [ 0  0]
 [ 0  0]
 ...
 [-2 -3]
 [-3 -5]
 [-5 -6]]
outdata: [[-2 -3]
 [ 2  3]
 [ 3  3]
 ...
 [-2 -3]
 [-3 -5]
 [-5 -6]]
indata: [[-2 -3]
 [ 2  3]
 [ 3  3]
 ...
 [ 2  2]
 [ 1  0]
 [ 0  1]]
outdata: [[ 1  2]
 [-1 -1]
 [-6 -7]
 ...
 [ 2  2]
 [ 1  0]
 [ 0  1]]
Q empty
indata: [[ 1  2]
 [-1 -1]
 [-6 -7]
 ...
 [ 2  3]
 [ 1  2]
 [ 3  4]]
indata: [[ 1  2]
 [ 3  4]
 [ 2  1]
 ...
 [-3 -2]
 [-3 -2]
 [ 0  0]]
outdata: [[-3 -3]
 [-7 -6]
 [-4 -4]
 ...
 [-3 -2]
 [-3 -2]
 [ 0  0]]
indata: [[-3 -3]
 [-7 -6]
 [-4 -4]
 ...
 [ 4  5]
 [ 4  6]
 [ 4  4]]
outdata: [[6 6]
 [3 4]
 [5 5]
 ...
 [4 5]
 [4 6]
 [4 4]]
outdata: [[6 6]
 [3 4]
 [5 5]
 ...
 [4 5]
 [4 6]
 [4 4]]
indata: [[ 6  6]
 [ 3  4]
 [ 5  5]
 ...
 [-5 -5]
 [-5 -4]
 [-4 -3]]
indata: [[   -5    -4]
 [    2     2]
 [   -2    -2]
 ...
 [-4256 -3531]
 [-3562  -270]
 [-5817 -2898]]
outdata: [[-6996 -4607]
 [-4653 -2403]
 [ -950   288]
 ...
 [-4256 -3531]
 [-3562  -270]
 [-5817 -2898]]
outdata: [[-6996 -4607]
 [-4653 -2403]
 [ -950   288]
 ...
 [-4256 -3531]
 [-3562  -270]
 [-5817 -2898]]
indata: [[-6996 -4607]
 [-4653 -2403]
 [ -950   288]
 ...
 [22415 20869]
 [22785 20992]
 [22681 20857]]
indata: [[ 22843  20948]
 [ 23259  21106]
 [ 23064  20686]
 ...
 [-32411 -24239]
 [-32322 -22860]
 [-32252 -21027]]
outdata: [[-32166 -19012]
 [-32095 -17178]
 [-32006 -15459]
 ...
 [-32411 -24239]
 [-32322 -22860]
 [-32252 -21027]]
outdata: [[-32166 -19012]
 [-32095 -17178]
 [-32006 -15459]
 ...
 [-32411 -24239]
 [-32322 -22860]
 [-32252 -21027]]
indata: [[-32166 -19012]
 [-32095 -17178]
 [-32006 -15459]
 ...
 [ -3367    451]
 [ -4995  -3081]
 [ -6701  -6331]]
indata: [[ -6691  -8941]
 [ -6266 -10015]
 [ -5759 -10552]
 ...
 [ 14335  13256]
 [ 13517  12118]
 [ 12756  11777]]
outdata: [[12683 11770]
 [12776 11501]
 [12607 11241]
 ...
 [14335 13256]
 [13517 12118]
 [12756 11777]]
indata: [[12683 11770]
 [12776 11501]
 [12607 11241]
 ...
 [-1871 -1134]
 [-2522  -886]
 [-3541  -801]]
outdata: [[-4660  -635]
 [-6320  -998]
 [-7844 -2366]
 ...
 [-1871 -1134]
 [-2522  -886]
 [-3541  -801]]
outdata: [[-4660  -635]
 [-6320  -998]
 [-7844 -2366]
 ...
 [-1871 -1134]
 [-2522  -886]
 [-3541  -801]]
indata: [[-4660  -635]
 [-6320  -998]
 [-7844 -2366]
 ...
 [-6790  2150]
 [-7303  2614]
 [-7760  3178]]
indata: [[-8136  3854]
 [-8446  4600]
 [-8715  5383]
 ...
 [  973   376]
 [  941   372]
 [  914   364]]
outdata: [[883 355]
 [853 347]
 [821 333]
 ...
 [973 376]
 [941 372]
 [914 364]]
outdata: [[883 355]
 [853 347]
 [821 333]
 ...
 [973 376]
 [941 372]
 [914 364]]
indata: [[  883   355]
 [  853   347]
 [  821   333]
 ...
 [   -3 -1137]
 [    4 -1142]
 [   17 -1152]]
indata: [[   29 -1150]
 [   37 -1146]
 [   45 -1143]
 ...
 [ 3967 -2368]
 [ 4042 -2401]
 [ 4111 -2441]]
outdata: [[ 4185 -2471]
 [ 4262 -2500]
 [ 4338 -2524]
 ...
 [ 3967 -2368]
 [ 4042 -2401]
 [ 4111 -2441]]
outdata: [[ 4185 -2471]
 [ 4262 -2500]
 [ 4338 -2524]
 ...
 [ 3967 -2368]
 [ 4042 -2401]
 [ 4111 -2441]]
indata: [[ 4185 -2471]
 [ 4262 -2500]
 [ 4338 -2524]
 ...
 [ 6557  8529]
 [ 6581  8635]
 [ 6606  8742]]
indata: [[ 6628  8840]
 [ 6656  8941]
 [ 6689  9039]
 ...
 [14234 11089]
 [14141 11118]
 [14049 11147]]
outdata: [[13961 11176]
 [13878 11204]
 [13798 11231]
 ...
 [14234 11089]
 [14141 11118]
 [14049 11147]]
outdata: [[13961 11176]
 [13878 11204]
 [13798 11231]
 ...
 [14234 11089]
 [14141 11118]
 [14049 11147]]
indata: [[13961 11176]
 [13878 11204]
 [13798 11231]
 ...
 [    3     3]
 [    4     2]
 [    4     2]]
indata: [[ 2  1]
 [-1 -2]
 [-4 -5]
 ...
 [-4 -4]
 [-9 -8]
 [-6 -7]]
outdata: [[-6 -7]
 [-7 -8]
 [-4 -6]
 ...
 [-4 -4]
 [-9 -8]
 [-6 -7]]
indata: [[-6 -7]
 [-7 -8]
 [-4 -6]
 ...
 [ 3  3]
 [ 3  3]
 [ 3  4]]
outdata: [[3 3]
 [3 4]
 [3 3]
 ...
 [3 3]
 [3 3]
 [3 4]]
outdata: [[3 3]
 [3 4]
 [3 3]
 ...
 [3 3]
 [3 3]
 [3 4]]
indata: [[ 3  3]
 [ 3  4]
 [ 3  3]
 ...
 [ 0  0]
 [ 4  4]
 [-1 -1]]
indata: [[-2 -1]
 [-6 -5]
 [-1  0]
 ...
 [ 1  2]
 [ 3  4]
 [ 3  2]]
outdata: [[2 2]
 [3 3]
 [3 3]
 ...
 [1 2]
 [3 4]
 [3 2]]
outdata: [[2 2]
 [3 3]
 [3 3]
 ...
 [1 2]
 [3 4]
 [3 2]]
indata: [[ 2  2]
 [ 3  3]
 [ 3  3]
 ...
 [-6 -7]
 [-4 -4]
 [-2 -2]]
indata: [[1 0]
 [2 1]
 [3 2]
 ...
 [2 4]
 [2 2]
 [3 3]]
outdata: [[-2 -1]
 [-3 -3]
 [-4 -3]
 ...
 [ 2  4]
 [ 2  2]
 [ 3  3]]
outdata: [[-2 -1]
 [-3 -3]
 [-4 -3]
 ...
 [ 2  4]
 [ 2  2]
 [ 3  3]]
indata: [[-2 -1]
 [-3 -3]
 [-4 -3]
 ...
 [ 1  3]
 [ 2  2]
 [ 1  1]]
indata: [[ 2  2]
 [ 3  3]
 [ 2  2]
 ...
 [-2 -2]
 [ 0  1]
 [-3 -2]]
outdata: [[-4 -3]
 [-1 -2]
 [-6 -5]
 ...
 [-2 -2]
 [ 0  1]
 [-3 -2]]
indata: [[-4 -3]
 [-1 -2]
 [-6 -5]
 ...
 [ 2  2]
 [ 1  1]
 [ 1  1]]

These repetitions on the outdata is what i mean with the choppy sound.

I have already tried the following: checked for underflow, checked for clipping, checked for cpu utilisation, checked for suitable samplerates. With the Input to Output Pass-Through example from the sounddevice documentation the whole thing works without any problems.

1

There are 1 answers

0
Blackyy On

I just solved my issue with:

    def read_data(self, indata, frames, time, status):
        if status:
            print(status, flush=True)
        q = self.buffer.get_q()
        q.put(indata.copy())