Description
Consider this simple use of soundfile:
import soundfile as sf
SAMPLE_RATE = 48000
SHRT_MAX = 32767
with sf.SoundFile("testfile2.wav", mode = 'w', samplerate=SAMPLE_RATE, channels=2, subtype='PCM_16') as outfile:
frames = []
for i in range(SAMPLE_RATE):
frames.append([i%SHRT_MAX, SHRT_MAX-(i%SHRT_MAX)])
outfile.write(frames)
I expect this to create a wav containing a very slow sawtooth wave (it will not be audible but the waveform will be visible in an editor such as audacity) with a DC offset. Instead, it creates a wav in which all values are zero.
If I pass the values in as numpy rather than as python lists, it works and creates a usable, correct wav file:
import numpy as np
import soundfile as sf
SAMPLE_RATE = 48000
SHRT_MAX = 32767
with sf.SoundFile("testfile2.wav", mode = 'w', samplerate=SAMPLE_RATE, channels=2, subtype='PCM_16') as outfile:
frames = []
for i in range(SAMPLE_RATE):
frames.append([i%SHRT_MAX, SHRT_MAX-(i%SHRT_MAX)])
array = np.array(frames, dtype=np.int16)
outfile.write(array)
print(array)
It is possible that when using non-numpy input, Soundfile is expecting the values in some format (floats?) other than ints. If so, this is not documented. I assume PCM_16 values because I have specified PCM_16 subtype.
I have a non-numpy app in which I adopted python-soundfile because it had the ability to append to wavs. I understand python-soundfile is mostly intended for use with numpy, but it does advertise a non-numpy interface. Using without numpy, I encountered several inconvences, but this was the biggest problem because I could not resolve it by consulting the documentation. I ultimately had to just add numpy to my program.