Decibel Value from an audio source or built-in microphone

Discussion in 'Mac Programming' started by david.cherrie, Jul 26, 2015.

  1. david.cherrie, Jul 26, 2015
    Last edited: Jul 30, 2015

    david.cherrie macrumors newbie

    Joined:
    Jun 17, 2015
    #1
    Hi everyone,

    I'm trying to get a decibel value from an audio source of the built-in microphone for a Mac. I know there are so many different iOS apps and OS X apps but they don't allow me to actually get a live export of that number.

    The ideal scenario is that we have some way of getting a numerical value in a decibel range into a program we use via AppleScript. We just need to convert what a microphone hears to a decibel numerical value.

    I have found a few websites that are on topic but I have no idea how to use Xcode as its not as simple as AppleScript.

    http://b2cloud.com.au/tutorial/obtaining-decibels-from-the-ios-microphone/

    http://stackoverflow.com/questions/9247255/am-i-doing-the-right-thing-to-convert -decibel-from-120-0-to-0-120

    Any help is appreciated!

    Cheers,

    David
     
  2. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #2
    It's going to depend on the microphone you use. The only reason they can do this on the iPhone is that they have calibrated the readings for the iPhone microphone.
     
  3. david.cherrie thread starter macrumors newbie

    Joined:
    Jun 17, 2015
    #3
    It doesn't need to be accurate. There are apps that you can download from the App Store for your Mac that are decibel readers. I just need a numerical value that increases when the sound gets louder and drops when it gets quiet. It could be a value between 0.000 and 1.000. I can then use a maths equation to then get up to a range from 10db to 140db. I just need a changing value that changes because of how loud and soft it gets that I can then put into Apple Script.
     
  4. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #4
    It sounds like you're just looking for the raw PCM data in the audio. You can get the amplitude of the sound (if it's 16-bit sound it will be +32768/-32767) if you hunt around in AudioFileReadPacketData
     
  5. david.cherrie, Jul 28, 2015
    Last edited: Jul 30, 2015

    david.cherrie thread starter macrumors newbie

    Joined:
    Jun 17, 2015
    #5
    Problem is we need a live value from a microphone or input. It can't be from a prerecorded file.
     
  6. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #6
    I have never done audio programming on the Mac. But according to the CoreAudio docs for the Mac, the appropriate type of object seems to be an AudioQueue to record sound from a microphone. There is an AudioQueueSetParameters function which allows the volume to be modified. The docs also talk about a property called "kAudioQueueProperty_CurrentLevelMeterDB" that can be queried that returns RMS power which seems to be what the O.P. is looking for. So if you can figure out how to get audio into a program using an AudioQueue, it seems pretty easy to query the sound level.
     
  7. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #7
    Hi,

    I think that if you're wanting to stay with AppleScript and out of Xcode then your only chance is to find an AppleScriptable audio recording app with support for the feature you're asking for. It may exist, but I think it's a longshot.

    However, if you're willing to dip your toes into Xcode and Objective-C then its relatively straightforward. I managed to knock up an extremely decibel meter from scratch using mostly code from this tutorial:

    http://www.whatsoniphone.com/blog/tutorial-detecting-when-a-user-blows-into-the-mic/

    ...I know it's iOS, but it worked fine on OS X. I have no idea how accurate it is (I suspect not very!), but it certainly goes up and down with volume. Happy to share my Xcode project with you if that's any help.
     
  8. subsonix, Jul 29, 2015
    Last edited: Jul 31, 2015

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    It doesn't need to be a prerecorded file. You will not be able to do this from Applescript though afaik. For the scale, just don't use a dB scale but any arbitrary scale say from 0 - 100 and skip the units. You'd probably still need to calibrate it so that 100 is very hard to reach but not impossible though. All of this this is also going to depend on where you place the mic and any amplification before the computer obviously.

    You could use dBfs for a relative scale, which for a 24 bit sample would go from -144 to 0 dBfs, each bit representing 6 dB. But for your application that wouldn't make much sense, so just go for an arbitrary scale perhaps from "rediculous" to "insane" or something like that. :D
     

Share This Page