Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7652909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:53:28+00:00 2026-05-31T11:53:28+00:00

I am writing a python function to return the loudness of a .wav file.

  • 0

I am writing a python function to return the loudness of a .wav file. RMS seems to be best the metric for this, Detect and record a sound with python.
audioop.rms() does the trick, but I’d like to avoid audioop as a dependency, and I already import numpy. but I’m not getting the same RMS values, and would appreciate help in understanding what is going on.

From the audioop page, it says that the rms calculation is just what you’d expect, namely sqrt(sum(S_i^2)/n), where, S_i is the i-th sample of the sound. Seems like its not rocket science.

To use numpy, I first convert the sound to a numpy array, and always see identical min / max, and the same length of the data (so the conversion seems fine).

>>> d = np.frombuffer(data, np.int16)
>>> print (min(d), max(d)), audioop.minmax(data,2)
(-2593, 2749) (-2593, 2749)

but I get very different RMS values, not even ball-park close:

>>> numpy_rms = np.sqrt(sum(d*d)/len(d))
>>> print numpy_rms, audioop.rms(data, 2)
41.708703254716383, 120

The difference between them is variable, no obvious pattern I can see, eg, I also get:

63.786714248938772, 402
62.779300661773405, 148

My numpy RMS code gives identical output to the one here: Numpy Root-Mean-Squared (RMS) smoothing of a signal

I don’t see where I am going wrong, but something is off. Any help much appreciated.


EDITED / UPDATE:

In case its useful, here’s the code I ended up with. Its not quite as fast as audioop but is still plenty fast, good enough for my purpose. Of note, using np.mean() makes it MUCH faster (~100x) than my version using python sum().

def np_audioop_rms(data, width):
    """audioop.rms() using numpy; avoids another dependency for app"""
    #_checkParameters(data, width)
    if len(data) == 0: return None
    fromType = (np.int8, np.int16, np.int32)[width//2]
    d = np.frombuffer(data, fromType).astype(np.float)
    rms = np.sqrt( np.mean(d**2) )
    return int( rms )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:53:29+00:00Added an answer on May 31, 2026 at 11:53 am

    Perform calculations using double as in audioop.rms() code:

    d = np.frombuffer(data, np.int16).astype(np.float)
    

    Example

    >>> import audioop, numpy as np
    >>> data = 'abcdefgh'
    >>> audioop.rms(data, 2)
    25962
    >>> d = np.frombuffer(data, np.int16)
    >>> np.sqrt((d*d).sum()/(1.*len(d)))
    80.131142510262507
    >>> d = np.frombuffer(data, np.int16).astype(np.float)
    >>> np.sqrt((d*d).sum()/len(d))
    25962.360851817772
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a native function that will return multiple Python objects PyObject *V
I'm writing a little Python extension in C/C++, and I've got a function like
Writing a python program, and I came up with this error while using the
I'm writing a Python backup script and I need to find the oldest file
I am writing a simple Python program. My program seems to suffer from linear
Possible Duplicate: What's the best way to implement an 'enum' in Python? I’m writing
I am trying to write a python wrapper for a C function. After writing
I am writing python scripts and execute them in a Makefile. The python script
I have been writing Python code for only a couple of weeks, so I'm
I am writing Python code in Visual Studio 2010 using the excellent Python Tools

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.