Skip to content

Instantly share code, notes, and snippets.

@pushkarsingh019
Last active December 24, 2024 01:58
Show Gist options
  • Select an option

  • Save pushkarsingh019/1f68ebc23949fa1298fe24d435b95ba5 to your computer and use it in GitHub Desktop.

Select an option

Save pushkarsingh019/1f68ebc23949fa1298fe24d435b95ba5 to your computer and use it in GitHub Desktop.
The function create_single_channel_sound converts a monaural (single-channel) sound into a binaural (two-channel) sound. It places the original sound in either the left or right channel, as specified by the user, and fills the other channel with silence. This is useful for isolating a sound to a specific channel in a stereo format.
import numpy as np
import slab
def create_single_channel_sound(sound, channel):
"""
Pushkar Singh, 24/12/24 07:22
This functions take a monoaural sound and returns a single channel sound
Inputs
- sound : the monoaural sound object. The it should be a slab monoaural sound object.
- channel : "left" or "right" channel to be returned
Outputs
- single_channel_sound : the single channel sound object
"""
samples = sound.n_samples
sound_data = sound.data
if channel == "left":
single_channel_sound = slab.Binaural([sound_data, np.zeros(samples)])
elif channel == "right":
single_channel_sound = slab.Binaural([np.zeros(samples), sound_data])
else:
raise ValueError("Channel must be either 'left' or 'right'")
return single_channel_sound
# Example usage
monaural_sound = slab.Sound.tone() # Create a test tone
left_channel = create_single_channel_sound(monaural_sound, "left")
right_channel = create_single_channel_sound(monaural_sound, "right")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment