Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Input Sound Frequency

Input Sound Frequency
Thread Tools
ameat
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
Jun 8, 2005, 02:07 PM
 
Hey folks

Wondering if anyone could lead me in the right direction for working with a sound input device to determine the frequency; basically for instrument tuning type purposes. Additionally, how much of a factor is something like background noise? And what type of filter options (using CoreAudio?) are available to gather the best source for frequency polling?

thanks for any help.
     
parallax
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jun 13, 2005, 01:36 PM
 
Background noise will generally not impact your measurements. White noise will add a low-level spread across the entire spectrum. In the time domain this alters the wave, but in the frequency domain the frequency you're looking for will not be shifted or anything.

If you just execute a Discrete Fourier Transform on your sound sample this should be sufficient.

Sorry, what are you looking for additionally?
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
ameat  (op)
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
Jun 13, 2005, 02:24 PM
 
Specifically, which API toolset to even begin learning and where to search in the documentation would be helpful. I've done lots of little cocoa demo projects and such, but nothing dealing at all with audio IO.

thanks.
     
parallax
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jun 14, 2005, 02:49 AM
 
The last time I was hacking with CoreAudio stuff was a long time ago and documentation was scarce/none at that point. :-) However, collected from some old source, here are some things you may want to check out:

Callback you need to write:

Code:
OSStatus inputIOProc (AudioDeviceID inDevice, const AudioTimeStamp* inNow, const AudioBufferList* inInputData, const AudioTimeStamp* inInputTime, AudioBufferList* outOutputData, const AudioTimeStamp* inOutputTime, void* defptr) { float *in = inInputData->mBuffers[0].mData; int numSamples = def->deviceBufferSize / def->deviceFormat.mBytesPerFrame; for (int i = 0; i < numSamples; ++i) { float sample = *in++; // now go to town with your sample } }
Setting up the device:

Code:
AudioDeviceID inputDevice; UInt32 count = sizeof(inputDevice); AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &count, &inputDevice);
You'll need

Code:
UInt32 deviceBufferSize; count = sizeof(deviceBufferSize); err = AudioDeviceGetProperty(inputDevice, 0, false, kAudioDevicePropertyBufferSize, &count, &deviceBufferSize); AudioStreamBasicDescription deviceFormat; count = sizeof(deviceFormat); err = AudioDeviceGetProperty(inputDevice, 0, false, kAudioDevicePropertyBufferSize, &count, &deviceBufferSize);
to be able to read properties of the device.

Starting the callback:

Code:
err = AudioDeviceAddIOProc(inputDevice, inputIOProc, (void *) def); if (err != kAudioHardwareNoError) return false; err = AudioDeviceStart(inputDevice, inputIOProc);
Stopping the callback:

Code:
AudioDeviceStop(inputDevice, inputIOProc);
(void *)def are the defs of your class. This is if you want to directly access Objective-C class fields from your C callback. (in inputIOProc)

Code:
typedef struct { @defs(WhateverYourClassIs); } classdef; classdef *def = (classdef *)self;
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
ameat  (op)
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
Jun 14, 2005, 09:37 AM
 
Fun... thanks. Hopefully some of the docs have been beefed up since then, or the API is more object oriented, but guess I just need to dig in.
     
ameat  (op)
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
Jun 14, 2005, 09:52 AM
 
     
ameat  (op)
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
Jun 14, 2005, 10:09 AM
 
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:03 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,