sagemathinc / cocalc

CoCalc: Collaborative Calculation in the Cloud
https://CoCalc.com
Other
1.14k stars 207 forks source link

Interactive audio widget does not refresh audio #7608

Open Splines opened 4 weeks ago

Splines commented 4 weeks ago

Description

With from IPython.display import Audio, we can play audio constructed in code. For example, consider this simple notebook with the following code:

import numpy as np
from IPython.display import Audio

SAMPLE_RATE = float(25000)  # sample rate (samples per second, Hz)
T_sound = 1  # duration of sound (seconds)

@interact
def sin_wave(f=slider(0, 1400, 1, default=440),
        phi=slider(0,2*pi), A_0=slider(0,1,0.1, default=0.8)):

    # Plot the sine wave (via SageMath)
    A(t) = A_0 * np.sin(2*pi*f*t + phi)
    show(plot(A(t), (t, 0, 0.06),
            color="red", thickness=1.5),
            xmin=0, xmax=0.06, ymin=-1, ymax=1,
            figsize=(10, 4), axes_labels=["t", "A(t)"])

    # Generate audio file
    t_linspace = np.linspace(0, T_sound, int(T_sound*SAMPLE_RATE), endpoint=False)
    wave = A_0 * np.sin(2*np.pi*f*t_linspace + phi)
    display(Audio(wave, rate=SAMPLE_RATE, normalize=False))

It will plot a sine wave with adjustable parameters.

image

The actual problem: Audio does not update

This works fine locally inside VSCode in a Jupyter notebook with the SageMath 10.2 kernel. I've selected the same environment in CoCalc and it only creates the audio file once. Users can even hear the sound. But if they change a value of an interactive slider, the audio is not recreated and you still hear the initially rendered sound. E.g. the frequency does not change when you change the frequency slider. Locally, the audio file is refreshed every time I wiggle any slider.

Note that while the audio does not refresh, the visual representation does indeed refresh, e.g. we see more oscillations with a higher frequency.