0

How can I use width 2 to get result without error?

The width is 2, but I will get error.

If I change it to 1, I can get a result from the code but its incorrect.

import wave
import contextlib
import audioop

fname = "some.wav"

with contextlib.closing(wave.open(fname,'r')) as f:

    frames = f.getnframes()
    rate = f.getframerate()
    duration = frames / float(rate)
    width = f.getsampwidth()
    channel = f.getnchannels()
    size = width*channel
    # f.rewind()
    wav = f.readframes(f.getnframes())
    # print(duration)

print(audioop.rms(str(wav).encode("ascii"),2))

"audioop.error: not a whole number of frames"

BenT
  • 3,172
  • 3
  • 18
  • 38
Jennifer
  • 19
  • 2
  • 6

2 Answers2

0

I don't use Python so let me know if one of these possible option fixes it...

Option 1:

According to Audioop docs:

"It operates on sound fragments consisting of signed integer samples 8, 16 or 32 bits wide."

Consider the following:

  • f.getsampwidth() gives width in bytes length, so you have 2 bytes.

  • 1 byte contains 8-bits, so 2 bytes will be 16-bits wide.

Try as:

print(audioop.rms(str(wav).encode("ascii"),16))

Option 2:

"If I change it to 1, I can get a result from the code but it's incorrect."

Maybe the original line: print(audioop.rms(str(wav).encode("ascii"),2)) is correct and the real issue is not enough bytes to divide by 2 (of width).

Make sure the WAV'a audio format really is 16-bit and/or Stereo.

VC.One
  • 14,790
  • 4
  • 25
  • 57
0

Might be a bit late, but I came across an answer on another question: audioop.rms() - why does it differ from normal RMS?

Maybe try using:

audioop.rms(wav, 2)

Where the 2 represents the number of bytes (in this case int16)

khi48
  • 51
  • 1
  • 1