Exemple #1
0
def demo_playback(signal, sr, ignore_warning=False):
    """Demo audio playback with pyaudio.

  Args:
    signal (array, optional): Signal containing waveform data.
    sr (int, optional): Sampling rate of the input signal.
    ignore_warning (bool, optional): Determines if audio signals will be played
      (using pyaudio). NOTE: Be careful with the volume when using playback,
      things can get *very loud*.

  Returns:
    None
  """
    # get a signal if one isn't provided
    if signal is None:
        signal, signal_params = make_harmonic_stack()
        sr = signal_params['sr']
    else:
        assert sr is not None

    # audio playback
    pyaudio_params = {
        'channels': utils.get_channels(signal),
        'rate': sr,
        'output': True,
        'output_device_index': 1
    }
    print(signal.shape)
    utils.play_array(signal,
                     pyaudio_params=pyaudio_params,
                     ignore_warning=ignore_warning)
Exemple #2
0
def demo_invert_cochleagram(signal=None, sr=None, n=None, playback=False):
    """Demo that will generate a cochleagram from a signal, then invert this
  cochleagram to produce a waveform signal.

  Args:
    signal (array, optional): Signal containing waveform data.
    sr (int, optional): Sampling rate of the input signal.
    n (int, optional): Number of filters to use in the filterbank.
    playback (bool, optional): Determines if audio signals will be played
      (using pyaudio). If False, only plots will be created. If True, the
      original signal and inverted cochleagram signal will be played. NOTE:
      Be careful with the volume when using playback, things can get
      *very loud*.

  Returns:
    None
  """
    # get a signal if one isn't provided
    if signal is None:
        signal, signal_params = make_harmonic_stack()
        sr = signal_params['sr']
        n = signal_params['n']
        low_lim = signal_params['low_lim']
        hi_lim = signal_params['hi_lim']
    else:
        assert sr is not None
        assert n is not None
        low_lim = 50  # this is the default for cochleagram.human_cochleagram
        hi_lim = 20000  # this is the default for cochleagram.human_cochleagram

    # generate a cochleagram from the signal
    sample_factor = 2  # this is the default for cochleagram.human_cochleagram
    coch = demo_human_cochleagram_helper(signal,
                                         sr,
                                         n,
                                         sample_factor=sample_factor)
    print('Generated cochleagram with shape: ', coch.shape)

    # invert the cochleagram to get a signal
    coch = np.flipud(
        coch)  # the ouput of demo_human_cochleagram_helper is flipped
    inv_coch_sig, inv_coch = cgram.invert_cochleagram(coch,
                                                      sr,
                                                      n,
                                                      low_lim,
                                                      hi_lim,
                                                      sample_factor,
                                                      n_iter=10,
                                                      strict=False)

    print('Generated inverted cochleagram')
    print('Original signal shape: %s, Inverted cochleagram signal shape: %s' %
          (signal.shape, inv_coch_sig.shape))

    plt.subplot(211)
    plt.title('Cochleagram of original signal')
    utils.cochshow(coch, interact=False)  # this signal is already flipped
    plt.ylabel('filter #')
    plt.xlabel('time')
    plt.gca().invert_yaxis()

    plt.subplot(212)
    plt.title('Cochleagram of inverted signal')
    utils.cochshow(inv_coch, interact=False)  # this signal needs to be flipped
    plt.ylabel('filter #')
    plt.xlabel('time')
    plt.gca().invert_yaxis()
    plt.tight_layout()
    plt.show()

    if playback:
        print('playing original signal...')
        utils.play_array(signal,
                         pyaudio_params={'rate': sr},
                         ignore_warning=True)
        sleep(1)
        print('playing inverted cochleagram signal...')
        utils.play_array(inv_coch_sig,
                         pyaudio_params={'rate': sr},
                         ignore_warning=True)