Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to split channels wav python

import wave
import numpy as np

def save_wav_channel(fn, wav, channel):
    '''
    Take Wave_read object as an input and save one of its
    channels into a separate .wav file.
    '''
    # Read data
    nch   = wav.getnchannels()
    depth = wav.getsampwidth()
    wav.setpos(0)
    sdata = wav.readframes(wav.getnframes())

    # Extract channel data (24-bit data not supported)
    typ = { 1: np.uint8, 2: np.uint16, 4: np.uint32 }.get(depth)
    if not typ:
        raise ValueError("sample width {} not supported".format(depth))
    if channel >= nch:
        raise ValueError("cannot extract channel {} out of {}".format(channel+1, nch))
    print ("Extracting channel {} out of {} channels, {}-bit depth".format(channel+1, nch, depth*8))
    data = np.fromstring(sdata, dtype=typ)
    ch_data = data[channel::nch]

    # Save channel to a separate file
    outwav = wave.open(fn, 'w')
    outwav.setparams(wav.getparams())
    outwav.setnchannels(1)
    outwav.writeframes(ch_data.tostring())
    outwav.close()

wav = wave.open(WAV_FILENAME)
save_wav_channel('ch1.wav', wav, 0)
save_wav_channel('ch2.wav', wav, 1)
Comment

PREVIOUS NEXT
Code Example
Python :: modify dict key name python 
Python :: import file to colab 
Python :: how to square each term of numpy array python 
Python :: skimage image read 
Python :: extract text from a pdf python 
Python :: increase contrast cv2 
Python :: python nCr n choose r function 
Python :: get all type of image in folder python 
Python :: python print error traceback 
Python :: wordle hints 
Python :: django jinja subset string 
Python :: sort list of dictionaries python by value 
Python :: how to get the user ip in djagno 
Python :: how to add row to the Dataframe in python 
Python :: python decimal number into 8 bit binary 
Python :: how to add an active class to current element in navbar in django 
Python :: python plot_confusion_matrix 
Python :: p-norm of a vector python 
Python :: matplotlib plot dpi 
Python :: day difference between two dates in python 
Python :: find geomean of a df 
Python :: olst = [] a = int(input()) b = int(input()) for ele in range(a,b+1): if ele%2 != 0: olst.append(ele) print(olst[::-1]) 
Python :: prekladac 
Python :: how to take a screenshot using python 
Python :: change name of column pandas 
Python :: pandas timedelta to seconds 
Python :: x= [10] def List_ex(): x.append(20) def add_list(): x=[30,40] x.append(50) print (x) List_ex() print (x) add_list() print (x) 
Python :: how to lock writing to a variable thread python 
Python :: how to sort a dictionary by value in python 
Python :: radix sort python 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =