4 Replies - 15320 Views - Last Post: 05 August 2010 - 11:05 AM Rate Topic: -----

#1 sm5312   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 106
  • Joined: 09-April 09

getting Microphone input

Posted 05 August 2010 - 07:33 AM

im thinking of creating a simple tuner program inorder to get some experience. for that i need to get involved with the microphone so that i can track the input sound. I dont have any clue of doing this.
anyone here could tell me how to start it? or a very simple working code? ( i prefer c++ but doesnt matter even C# which iam learnig these days)
Is This A Good Question/Topic? 0
  • +

Replies To: getting Microphone input

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12566
  • View blog
  • Posts: 45,688
  • Joined: 27-December 08

Re: getting Microphone input

Posted 05 August 2010 - 07:37 AM

I found an Audio I/O Library in C++ you may find useful.

View Postsm5312, on 05 August 2010 - 10:33 AM, said:

or a very simple working code? ( i prefer c++ but doesnt matter even C# which iam learnig these days)

You've been around here long enough to know the rules. We are happy to help you with code you wrote, but we will not give you the code. :)
Was This Post Helpful? 0
  • +
  • -

#3 sm5312   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 106
  • Joined: 09-April 09

Re: getting Microphone input

Posted 05 August 2010 - 08:08 AM

View Postmacosxnerd101, on 05 August 2010 - 06:37 AM, said:

I found an Audio I/O Library in C++ you may find useful.

oh that seems pretty complex though! isnt that?

This post has been edited by sm5312: 05 August 2010 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12566
  • View blog
  • Posts: 45,688
  • Joined: 27-December 08

Re: getting Microphone input

Posted 05 August 2010 - 08:16 AM

It looks like it comes with documentation containing tutorials and samples. Try following those. :)

Honestly, I have no idea about complexity as I don't know C++. I just Googled for an Audio I/O API in C++. Though from looking at the documentation, it doesn't look too bad from what I can gather.
Was This Post Helpful? 1
  • +
  • -

#5 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: getting Microphone input

Posted 05 August 2010 - 11:05 AM

OpenAL is a good audio library, arguably the best. As for tutorials, there is a lack of them on on OpenAL to be honest, but google will help you. The following program (assuming you have learnt how to link the OpenAL library etc) will open a microphone and output the sound is (kind of) real time, although, there is some latency.

Just make sure you have learnt hot OpenAL works first, before you try to compile it.

#include <stdio.h>
#include <windows.h>
#include <al.h>
#include <alc.h>

int main()
{
	ALCdevice *dev[2];
	ALCcontext *ctx;
	ALuint source, buffers[3];
	char data[5000]; 
	ALuint buf;
	ALint val;

	dev[0] = alcOpenDevice(NULL);
	ctx = alcCreateContext(dev[0], NULL);
	alcMakeContextCurrent(ctx);

	alGenSources(1, &source);
	alGenBuffers(3, buffers);

	/* Setup some initial silent data to play out of the source */
	alBufferData(buffers[0], AL_FORMAT_MONO16, data, sizeof(data), 22050);
	alBufferData(buffers[1], AL_FORMAT_MONO16, data, sizeof(data), 22050);
	alBufferData(buffers[2], AL_FORMAT_MONO16, data, sizeof(data), 22050);
	alSourceQueueBuffers(source, 3, buffers);

	/* If you don't need 3D spatialization, this should help processing time */
	alDistanceModel(AL_NONE); 

	dev[1] = alcCaptureOpenDevice(NULL, 22050, AL_FORMAT_MONO16, sizeof(data)/2);

	/* Start playback and capture, and enter the audio loop */
	alSourcePlay(source);
	alcCaptureStart(dev[1]);

	while(1) 
	{
		/* Check if any queued buffers are finished */
		alGetSourcei(source, AL_BUFFERS_PROCESSED, &val);
		if(val <= 0)
			continue;

		/* Check how much audio data has been captured (note that 'val' is the
		* number of frames, not bytes) */
		alcGetIntegerv(dev[1], ALC_CAPTURE_SAMPLES, 1, &val);

		/* Read the captured audio */
		alcCaptureSamples(dev[1], data, val);

		/* Pop the oldest finished buffer, fill it with the new capture data,
		then re-queue it to play on the source */
		alSourceUnqueueBuffers(source, 1, &buf);
		alBufferData(buf, AL_FORMAT_MONO16, data, val*2 /* bytes here, not
		frames */, 22050);
		alSourceQueueBuffers(source, 1, &buf);

		/* Make sure the source is still playing */
		alGetSourcei(source, AL_SOURCE_STATE, &val);
		if(val != AL_PLAYING)
		{
			alSourcePlay(source);
		}
	}

	/* Shutdown and cleanup */
	alcCaptureStop(dev[1]);
	alcCaptureCloseDevice(dev[1]);

	alSourceStop(source);
	alDeleteSources(1, &source);
	alDeleteBuffers(3, buffers);

	alcMakeContextCurrent(NULL);
	alcDestroyContext(ctx);
	alcCloseDevice(dev[0]); 

	return 0;
} 

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1