<jupyter_start><jupyter_text>Audio MonitorMonitors the audio by continuously recording and plotting the recorded audio.You will need to start osp running in a terminal.<jupyter_code># make Jupyter use the whole width of the browser
from IPython.display import Image, display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
# Pull in all the necessary python modules
import plotly.graph_objects as go
from IPython.display import Audio
from osp_control import OspControl
import os
import json
import numpy as np
import time
import ipywidgets as widgets

# Connect to OSP process
osp = OspControl()  # connects to a local process

# set the number of bands and mute mic
osp.send({"method": "set", "data": {"num_bands": 11}})
osp.send_chan({"alpha": 0})
def plot_fft(fig, title, out1, update=True):
    ftrans = np.abs(np.fft.fft(out1)/len(out1))
    outlen = len(out1)
    values = np.arange(int(outlen/2))
    period = outlen/32000
    frequencies = values/period

    if update:
        with fig.batch_update():
            fig.data[0].y = ftrans
            fig.data[0].x = frequencies
            fig.update_layout()
            return
        
   
    fig.add_trace(go.Scatter(x=frequencies, line_color='green', opacity=1, y=ftrans))

    fig.update_layout(title=title,
                    xaxis_title='Frequency',
                    yaxis_title='Amplitude',
                    template='plotly_white')
def plot_mono(fig, title, out1, lab1, rate=48000, update=True):

    # Create x values.
    x = np.array(range(len(out1)))/rate
    if update:
        with fig.batch_update():
            fig.data[0].y = out1
            fig.data[0].x = x
            fig.update_layout()
            return
    fig.add_trace(go.Scatter(x=x,
                             y=out1,
                             name=lab1,
                             opacity=1))
    fig.update_layout(title=title,
#                       yaxis_range=[-1,1],
                      xaxis_title='Time(sec)',
                      yaxis_title='Amplitude',
                      template='plotly_white')
def monitor(afig, ffig, interval=2):
    update=False
    while True:
        # set record file name
        filename = os.path.join(os.getcwd(), 'tmpaudio')
        osp.send_chan({'audio_rfile': filename})

        #start recording output
        osp.send_chan({"audio_record": 1})

        # record for {interval} seconds
        time.sleep(interval)

        #stop recording
        osp.send_chan({"audio_record": 0})

        # read the saved recording data
        with open(filename, "rb") as f:
            data = f.read()
        data = np.frombuffer(data, dtype=np.float32)
        data = np.reshape(data, (2, -1), 'F')
        
        # plot
        plot_mono(afig, '', data[0], 'out', update=update)
        plot_fft(ffig, '',  data[0], update=update)
        if update == False:
            update = True
audio_fig = go.FigureWidget()
fft_fig = go.FigureWidget()
widgets.VBox([audio_fig, fft_fig])
monitor(audio_fig, fft_fig)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11582381729200653, 'fraction_numerical': 0.01435562805872757, 'mean_word_length': 2.332608695652174, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '7966', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 978, 'n_tokens_neox': 941, 'n_words': 234, 'path': 'Sources/libosp/python/osp/monitor.ipynb', 'repo_name': 'nihospr01/OpenSpeechPlatform', 'size': '3065'}