amsehili / auditok

An audio/acoustic activity detection and audio segmentation tool
MIT License
745 stars 96 forks source link

convert stereo to mono audioregion by mixing? #28

Closed ma7555 closed 4 years ago

ma7555 commented 4 years ago

any ideas on mixing a stereo AudioRegion into single mono channel?

my trials with region.samples[0] + region.samples[1] is failing..

amsehili commented 4 years ago

Hi,

You can do this using pydub (pip install pydub) or using standard python module audioop.

pydub will work with variable number of channels whereas the latter method can only convert stereo (2-channel) to mono.


from pydub import AudioSegment                                                                                                                                                                    
from auditok import AudioRegion                                                                                                                                                                   

region = AudioRegion.load("stereo_file.wav")                                                                                                               
segment = AudioSegment(data=bytes(region), sample_width=region.sw, frame_rate=region.sr, channels=region.ch)                                                                                      
segment.channels = 1                                                                                                                                                                              
region_mono = AudioRegion(segment._data, sample_width=region.sw, sampling_rate=region.sr, channels=1)

or

import audioop                                                                                                                                                                                    
data = audioop.tomono(bytes(region), region.sample_width, 0.5, 0.5)
region_mono = AudioRegion(data, sample_width=region.sw, sampling_rate=region.sr, channels=1)
ma7555 commented 4 years ago

thanks!