Last active
December 24, 2024 01:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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