# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %matplotlib inline

# Note: if plots don't show, there is a recent matplotlib issue that may be related:
# https://github.com/matplotlib/matplotlib/issues/18396

#
# # Add Noise to Speech at Specific SNR Levels
#
#
# Use soundpy to add noise at specific SNR levels to speech signals.
#
# To see how soundpy implements this, see `soundpy.dsp.add_backgroundsound`.
#
#

# to be able to import soundpy from parent directory:
import os
package_dir = '../'
os.chdir(package_dir)

# Let's import soundpy, assuming it is in your working directory:
#
#

import soundpy as sp;
import IPython.display as ipd

# # Define the speech and noise data samples
#
# These come included in soundpy example data.

# ### Speech sample:
#
#

# use `soundpy.utils.string2pathlib` to convert string to pathlib object
speech_sample = sp.string2pathlib('audiodata/python.wav')
# with pathlib object, can do the following: 
word = speech_sample.stem
word

# ### Noise sample:
#
#

# use `soundpy.utils.string2pathlib` to convert string to pathlib object
noise_sample = 'audiodata/background_samples/cafe.wav'
noise_sample = sp.string2pathlib(noise_sample)
# with pathlib object, can do the following: 
noise = noise_sample.stem
noise

# ## Hear and see what the speech looks like with SNR level of 20
#
#

sr = 48000
noisyspeech_20snr, snr20 = sp.dsp.add_backgroundsound(speech_sample,
                                           noise_sample,
                                           sr = sr,
                                           snr = 20)
ipd.Audio(noisyspeech_20snr,rate=sr)

# ## Hear and see what the speech looks like with SNR level of 5
#
#

noisyspeech_5snr, snr5 = sp.dsp.add_backgroundsound(speech_sample,
                                           noise_sample,
                                           sr = sr,
                                           snr = 5)
ipd.Audio(noisyspeech_5snr,rate=sr)

# # Visualize the Audio Samples
#

# ## Speech audio sample in its raw signal
#
#

sp.plotsound(speech_sample, feature_type='signal', 
               title = 'Speech: ' + word.upper())

# ## Noise audio sample in its raw signal
#
#

sp.plotsound(noise_sample, feature_type='signal',
               title = 'Noise: ' + noise.upper())

# ## The sounds added together at SNR level 20
#
#

sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'signal',
               title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()))

# ## The sounds added together at SNR level 5
#
#

sp.plotsound(noisyspeech_5snr, sr = sr, feature_type = 'signal',
               title = '"{}" with {} noise at SNR 5'.format(word.upper(), noise.upper()))

# ## Let's visualize the power spectrum: pure speech
#
#

sp.plotsound(speech_sample, sr = sr, feature_type = 'powspec',
               title = 'Clean speech: {}'.format(word),
               energy_scale = 'power_to_db')

# ## Let's visualize the power spectrum: SNR 20
#
#

sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'powspec',
               title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()),
               energy_scale = 'power_to_db')

# ## Let's visualize the power spectrum: SNR 5
#
#

sp.plotsound(noisyspeech_5snr,sr = sr, feature_type = 'powspec',
               title = '"{}" with {} noise at SNR 5'.format(word.upper(), noise.upper()),
               energy_scale = 'power_to_db')

# # More Functionality
#
#

# ### Make longer and shorter
#
#

# ## Delay the speech and lengthen the total signal
#
#

noisyspeech_20snr, snr20 = sp.dsp.add_backgroundsound(speech_sample,
                                           noise_sample,
                                           sr = sr,
                                           snr = 20,
                                           pad_mainsound_sec = 1,
                                           total_len_sec = 4)

sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'signal',
               title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()))
ipd.Audio(noisyspeech_20snr,rate=sr)

# ## Shorten the total signal
#
#

noisyspeech_20snr, snr20 = sp.dsp.add_backgroundsound(speech_sample,
                                           noise_sample,
                                           sr = sr,
                                           snr = 20,
                                           total_len_sec = 0.5)

sp.plotsound(noisyspeech_20snr, sr = sr, feature_type = 'signal',
               title = '"{}" with {} noise at SNR 20'.format(word.upper(), noise.upper()))
ipd.Audio(noisyspeech_20snr,rate=sr)

StarcoderdataJupyterScriptsDedupFiltered