2
$\begingroup$

I read an audio file (an instrumental music file that is mono and 16000) with wav.read (read function from scipy.io.wavfile.) This function gives me a data file in int16 format. When I change the type to float and write it again I hear a lot of jitter. Why does that happen?

Here is my script in python 3.6. This is the music file.

import scipy.io.wavfile as wav
from scipy.io.wavfile import write
rate, music = wav.read("music.wav")
music = np.expand_dims(music, -1)
music_float = music.astype('float')
write("float_music", rate, music_float)
$\endgroup$
7
  • 1
    $\begingroup$ short answer: there's no jitter that gets produced. It's not clear what you observe. Do you mean noise? Are you familiar with the concept of quantization noise? $\endgroup$ Jul 8, 2022 at 9:29
  • 1
    $\begingroup$ which write function is it that you call there? it's not wav.write, it seems! $\endgroup$ Jul 8, 2022 at 9:40
  • 1
    $\begingroup$ @MarcusMüller Hi. Thanks for your reply. What I mean by jitter is a non constant noise. And yes I used wav.write. I updated my wuestion and put importing part as well. $\endgroup$ Jul 8, 2022 at 10:56
  • 1
    $\begingroup$ noise is never constant, so I'm not sure what you're referring to. $\endgroup$ Jul 8, 2022 at 11:26
  • 3
    $\begingroup$ The link somehow doesn't work for me, so I'll have to take your word for it. But reading the documentation of scipy.io.wavfile.write (as you've probably done): You didn't adhere to the admissible input value range, did you? I see no scaling! $\endgroup$ Jul 8, 2022 at 12:00

2 Answers 2

4
$\begingroup$

The reason for this is that wavfile.write supports different number ranges for different data types.

The Valid ranges are

WAV format Min Max NumPy dtype
32-bit floating-point -1.0 +1.0 float32
32-bit PCM -2147483648 +2147483647 int32
16-bit PCM -32768 +32767 int16
8-bit PCM 0 255 uint8

(source https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html)

This is why Peters solution works.

$\endgroup$
3
$\begingroup$

As Marcus and you say, try normalizing the float version before writing.

Changing

write("float_music", rate, music_float)

to

write("unit_flow_music.wav", rate, music_float/max(np.abs(music_float)))

seems to do the trick for me.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.