'Writing audio files with multiple channels in R/Python

I'd like to know if there's a good way to write audio objects with multiple channels (such as to .wav files) in R or Python. I use librosa, tuneR, and seeWave but can seemingly only write mono (single-channel) .wav files. The writing functions seem to coerce four channels successively, making a mono audio file that is 4x longer in duration than the original. Any tips on how to fix this? Thanks in advance.



Solution 1:[1]

Here's an example that creates a stereo Wave object in tuneR and saves it as a wav file.

library(tuneR)
x <- seq(0, 2*pi, length = 44100)    # make a second of x values
left <- round(32000 * sin(40 * x))   # a low frequency that shows up in the plot
right <- round(32000 * sin(80 * x))  # a different but still low frequency
Wobj <- Wave(left = left, right = right, samp.rate=44100, bit=16)  # make the Wave object
plot(Wobj)
str(Wobj)
writeWave(object = Wobj, filename = 'Wobj.wav')

This is the output of the str command showing that the left and right slots are different.

Formal class 'Wave' [package "tuneR"] with 6 slots
  ..@ left     : num [1:44100] 0 182 365 547 729 ...
  ..@ right    : num [1:44100] 0 365 729 1094 1458 ...
  ..@ stereo   : logi TRUE
  ..@ samp.rate: num 44100
  ..@ bit      : num 16
  ..@ pcm      : logi TRUE

And here is the plot.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew Chisholm