<gh_stars>0
#!/usr/bin/env python

import asyncio
import queue
import sys

import numpy as np
import sounddevice as sd
import pyqtgraph as pg

from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import pyqtgraph as pg
from time import perf_counter

app = pg.mkQApp("ImageItem Example")

## Create window with GraphicsView widget
win = pg.GraphicsLayoutWidget()
win.show()  ## show widget alone in its own window
win.setWindowTitle('pyqtgraph example: ImageItem')
view = win.addViewBox()

## lock the aspect ratio so pixels are always square
view.setAspectLocked(True)

## Create image item
img = pg.ImageItem(border='w')
view.addItem(img)

## Set initial view bounds
view.setRange(QtCore.QRectF(0, 0, 600, 600))

## Create random image
data = np.random.normal(size=(15, 600, 600), loc=1024, scale=64).astype(np.uint16)
i = 0

updateTime = perf_counter()
elapsed = 0

timer = QtCore.QTimer()
timer.setSingleShot(True)

async def inputstream_generator(channels=1, **kwargs):
    """Generator that yields blocks of input data as NumPy arrays."""
    q_in = asyncio.Queue()
    loop = asyncio.get_event_loop()

    def callback(indata, frame_count, time_info, status):
        loop.call_soon_threadsafe(q_in.put_nowait, (indata.copy(), status))

    stream = sd.InputStream(callback=callback, channels=channels, **kwargs)
    with stream:
        while True:
            indata, status = await q_in.get()
            yield indata, status


async def stream_generator(blocksize, *, channels=1, dtype='float32',
                           pre_fill_blocks=10, **kwargs):
    """Generator that yields blocks of input/output data as NumPy arrays.

    The output blocks are uninitialized and have to be filled with
    appropriate audio signals.

    """
    assert blocksize != 0
    q_in = asyncio.Queue()
    q_out = queue.Queue()
    loop = asyncio.get_event_loop()

    def callback(indata, outdata, frame_count, time_info, status):
        loop.call_soon_threadsafe(q_in.put_nowait, (indata.copy(), status))
        outdata[:] = q_out.get_nowait()

    # pre-fill output queue
    for _ in range(pre_fill_blocks):
        q_out.put(np.zeros((blocksize, channels), dtype=dtype))

    stream = sd.Stream(
        blocksize=blocksize,
        callback=callback,
        dtype=dtype,
        channels=channels,
        **kwargs
    )
    with stream:
        while True:
            indata, status = await q_in.get()
            outdata = np.empty((blocksize, channels), dtype=dtype)
            yield indata, outdata, status
            q_out.put_nowait(outdata)


def updateData():
    global img, data, i, updateTime, elapsed

    ## Display the data
    img.setImage(data[i])
    i = (i + 1) % data.shape[0]

    timer.start(1)
    now = perf_counter()
    elapsed_now = now - updateTime
    updateTime = now
    elapsed = elapsed * 0.9 + elapsed_now * 0.1
# end updateData()

async def print_input_infos(**kwargs):
    """Show minimum and maximum value of each incoming audio block."""
    async for indata, status in inputstream_generator(**kwargs):
        if status:
            print(status)
        print('min:', indata.min(), '\t', 'max:', indata.max(), f"indata: {indata.shape}")

        # updateData()
    # end for

async def wire_coro(**kwargs):
    """Create a connection between audio inputs and outputs.

    Asynchronously iterates over a stream generator and for each block
    simply copies the input data into the output block.

    """
    async for indata, outdata, status in stream_generator(**kwargs):
        if status:
            print(status)
        outdata[:] = indata


async def main(device=None, **kwargs):

    print('Some informations about the input signal:')
    try:
        await asyncio.wait_for(print_input_infos(device=device), timeout=None)
    except asyncio.TimeoutError as error:
        print(f"error: {error}")
        pass
    # end try

    return

    print('\nEnough of that, activating wire ...\n')
    audio_task = asyncio.create_task(wire_coro(device=device, **kwargs))

    for i in range(20, 0, -1):
        print(i)
        await asyncio.sleep(1)
    audio_task.cancel()

    try:
        await audio_task
    except asyncio.CancelledError:
        print('\nwire was cancelled')


if __name__ == "__main__":
    device = 2

    try:
        asyncio.run(main(blocksize=512, device=device))
    except KeyboardInterrupt:
        sys.exit('\nInterrupted by user')
#!/usr/bin/env python

import asyncio
import queue
import sys

import numpy as np
import sounddevice as sd
import pyqtgraph as pg

from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import pyqtgraph as pg
from time import perf_counter

app = pg.mkQApp("ImageItem Example")

## Create window with GraphicsView widget
win = pg.GraphicsLayoutWidget()
win.show()  ## show widget alone in its own window
win.setWindowTitle('pyqtgraph example: ImageItem')
view = win.addViewBox()

## lock the aspect ratio so pixels are always square
view.setAspectLocked(True)

## Create image item
img = pg.ImageItem(border='w')
view.addItem(img)

## Set initial view bounds
view.setRange(QtCore.QRectF(0, 0, 600, 600))

## Create random image
data = np.random.normal(size=(15, 600, 600), loc=1024, scale=64).astype(np.uint16)
i = 0

updateTime = perf_counter()
elapsed = 0

timer = QtCore.QTimer()
timer.setSingleShot(True)

async def inputstream_generator(channels=1, **kwargs):
    """Generator that yields blocks of input data as NumPy arrays."""
    q_in = asyncio.Queue()
    loop = asyncio.get_event_loop()

    def callback(indata, frame_count, time_info, status):
        loop.call_soon_threadsafe(q_in.put_nowait, (indata.copy(), status))

    stream = sd.InputStream(callback=callback, channels=channels, **kwargs)
    with stream:
        while True:
            indata, status = await q_in.get()
            yield indata, status


async def stream_generator(blocksize, *, channels=1, dtype='float32',
                           pre_fill_blocks=10, **kwargs):
    """Generator that yields blocks of input/output data as NumPy arrays.

    The output blocks are uninitialized and have to be filled with
    appropriate audio signals.

    """
    assert blocksize != 0
    q_in = asyncio.Queue()
    q_out = queue.Queue()
    loop = asyncio.get_event_loop()

    def callback(indata, outdata, frame_count, time_info, status):
        loop.call_soon_threadsafe(q_in.put_nowait, (indata.copy(), status))
        outdata[:] = q_out.get_nowait()

    # pre-fill output queue
    for _ in range(pre_fill_blocks):
        q_out.put(np.zeros((blocksize, channels), dtype=dtype))

    stream = sd.Stream(
        blocksize=blocksize,
        callback=callback,
        dtype=dtype,
        channels=channels,
        **kwargs
    )
    with stream:
        while True:
            indata, status = await q_in.get()
            outdata = np.empty((blocksize, channels), dtype=dtype)
            yield indata, outdata, status
            q_out.put_nowait(outdata)


def updateData():
    global img, data, i, updateTime, elapsed

    ## Display the data
    img.setImage(data[i])
    i = (i + 1) % data.shape[0]

    timer.start(1)
    now = perf_counter()
    elapsed_now = now - updateTime
    updateTime = now
    elapsed = elapsed * 0.9 + elapsed_now * 0.1
# end updateData()

async def print_input_infos(**kwargs):
    """Show minimum and maximum value of each incoming audio block."""
    async for indata, status in inputstream_generator(**kwargs):
        if status:
            print(status)
        print('min:', indata.min(), '\t', 'max:', indata.max(), f"indata: {indata.shape}")

        # updateData()
    # end for

async def wire_coro(**kwargs):
    """Create a connection between audio inputs and outputs.

    Asynchronously iterates over a stream generator and for each block
    simply copies the input data into the output block.

    """
    async for indata, outdata, status in stream_generator(**kwargs):
        if status:
            print(status)
        outdata[:] = indata


async def main(device=None, **kwargs):

    print('Some informations about the input signal:')
    try:
        await asyncio.wait_for(print_input_infos(device=device), timeout=None)
    except asyncio.TimeoutError as error:
        print(f"error: {error}")
        pass
    # end try

    return

    print('\nEnough of that, activating wire ...\n')
    audio_task = asyncio.create_task(wire_coro(device=device, **kwargs))

    for i in range(20, 0, -1):
        print(i)
        await asyncio.sleep(1)
    audio_task.cancel()

    try:
        await audio_task
    except asyncio.CancelledError:
        print('\nwire was cancelled')


if __name__ == "__main__":
    device = 2

    try:
        asyncio.run(main(blocksize=512, device=device))
    except KeyboardInterrupt:
        sys.exit('\nInterrupted by user')
#!/usr/bin/env python ## Create window with GraphicsView widget ## show widget alone in its own window ## lock the aspect ratio so pixels are always square ## Create image item ## Set initial view bounds ## Create random image Generator that yields blocks of input data as NumPy arrays. Generator that yields blocks of input/output data as NumPy arrays.      The output blocks are uninitialized and have to be filled with     appropriate audio signals. # pre-fill output queue ## Display the data # end updateData() Show minimum and maximum value of each incoming audio block. # updateData() # end for Create a connection between audio inputs and outputs.      Asynchronously iterates over a stream generator and for each block     simply copies the input data into the output block. # end try