Re: Applying FFT to a .wav

keghn <[email protected]> Wed, 26 Jun 2013 14:02:31 -0700 (PDT)
Newsgroups gmane.comp.lib.openal
Message-ID <[email protected]>
hello,

 I use Linux and I use this to display my wave files:


/*
http://www.youtube.com/user/thecplusplusguy
Playing sound with OpenAL.
Compile with:

g++ wave_head_data_player.cpp -lalut -lopenal -o wave_head_data_player

./wave_head_data_player cross.wav
 or
./wave_head_data_player tonewave.wav

plays a mono 8bit or 16 bit mono wave file. It display the information in
the 44 byte data head
and print some of the sound as numbers.
 Dose not play longer than three seconds.
 One channel =3D mono, two channel =3D stereo.

 8bit wavs are unsigned, a sine wave bouncing between 20 and -20 will be
displayed
jumping between 148 and 108. 128 is the center line.

16bit wave are signed and will be displayed as bounce between  5120 and -
5120 decimal. zero is the center line.

resources:
https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
http://www.dunsanyinteractive.com/blogs/oliver/?p=3D72%20wave%20openal%20=
sound
http://www.ypass.net/blog/2010/01/pcm-audio-part-3-basic-audio-effects-vo=
lume-control/
*/

#include <iostream>
#include <fstream>
#include <cstring>
#include <AL/al.h>
#include <AL/alc.h>
#include <unistd.h>

/*
for windows ?
//#include "stdafx.h"
//#include <windows.h>
//#include <al.h>
//#include <alc.h>
*/


   //sleep(1); is one second.

//	short int dataarry[1024*100];

bool isBigEndian()
{
	int a=3D1;
	return !((char*)&a)[0];
}

int convertToInt(char* buffer,int len)
{
	int a=3D0;
	if(!isBigEndian())
		for(int i=3D0;i<len;i++)
			((char*)&a)[i]=3Dbuffer[i];
	else
		for(int i=3D0;i<len;i++)
			((char*)&a)[3-i]=3Dbuffer[i];=09
	return a;
}


//-----------------------------------------------------------------------=
-------------------------

char* loadWAV(const char* fn,int& chan,int& samplerate,int& bps,int& size=
)
{
	unsigned int rr,rrr,rrrr;
	int r1,r2;
	short int dataarry[1024*100];
	unsigned short blockalign;
	char buffer[4];
	std::ifstream in(fn,std::ios::binary);
	in.read(buffer,4);
	std::cout<<"\n\n     Print out of wave head then play file.\n";=20
	std::cout<<"0   4   ChunkID should have riff____________letter are now
"<<buffer[0]<<" "<<buffer[1]<<" "<<buffer[2]<<" "<<buffer[3]<<"\n";

	if(strncmp(buffer,"RIFF",4)!=3D0)
	{
		std::cout << "this is not a valid WAVE file"  << std::endl;
		return NULL;
	}
	in.read(buffer,4);	//4         4   ChunkSize
	samplerate=3DconvertToInt(buffer,4);
	std::cout<<"4   4   ChunkSize  is the size of the file in
bytes_____________and is "<<samplerate<<" bytes\n";

	in.read(buffer,4);	//8         4   Format "WAVE"
	std::cout<<"8   4   Format should say wave________and we have
"<<buffer[0]<<" "<<buffer[1]<<" "<<buffer[2]<<" "<<buffer[3]<<"\n";

	in.read(buffer,4);	//12        4   Subchunk1ID "fmt "
	std::cout<<"12  4   Subchunk1ID should say fmt ___________and we have
"<<buffer[0]<<" "<<buffer[1]<<" "<<buffer[2]<<" "<<buffer[3]<<"\n";

	in.read(buffer,4);	//16        4   Subchunk1Size 16
	std::cout<<"16  4   Subchunk1Size should have decimal 16__________and is
"<<std::dec<<int(buffer[0])<<"\n";
=09
	in.read(buffer,2);	//20        2   AudioFormat      PCM =3D1
	std::cout<<"20  2   AudioFormat PCM should be 1____________and is
"<<std::dec<<int(buffer[0])<<"\n";

	in.read(buffer,2);	//22        2   NumChannels      Mono =3D 1, Stereo =3D=
 2,
etc.
	chan=3DconvertToInt(buffer,2);
	std::cout<<"22  2   NumChannels      Mono =3D 1, Stereo =3D 2,
etc.______________"<<std::dec<<int(buffer[0])<<" chanels\n";

	in.read(buffer,4);	//24        4   SampleRate       8000, 4410=09
	samplerate=3DconvertToInt(buffer,4);
	std::cout<< "24  4   SampleRate 8000, 22500, 44100, etc. ______but is "<=
<
std::dec<<samplerate<<"\n";

	in.read(buffer,4);	//28        4   ByteRate         =3D=3D SampleRate *
NumChannels * BitsPerSample/8
	rr=3DconvertToInt(buffer,4);
	std::cout<<"28  4   ByteRate_______"<<std::dec<<int(rr)<<"\n";  =09

	in.read(buffer,2);	//32        2   BlockAlign       =3D=3D NumChannels *
BitsPerSample/8
//	blockalign =3D short(buffer[0]);
	blockalign =3D short(buffer[0]);
	std::cout<<"32  2   BlockAlign mono 8 is 1, stereo 8 is 2, mono 16 is 2,
stereo 16 is 4_______is now "<<std::dec<<int(buffer[0])<<" \n";

	in.read(buffer,2);	//34        2   BitsPerSample    8 bits =3D 8, 16 bit=
s =3D
16, etc.
	bps=3DconvertToInt(buffer,2);
	std::cout<<"34  2   BitsPerSample   is 8 bits =3D 8, 16 bits =3D 16, etc.
_______________is now "<<bps<<"\n";


	in.read(buffer,4);	//36        4   Subchunk2ID      Contains the letters
"data"
	std::cout<<"36  4   Subchunk2ID Contains the letters data_________is now
"<<buffer[0]<<" "<<buffer[1]<<" "<<buffer[2]<<" "<<buffer[3]<<"\n";

	in.read(buffer,4);	//40        4   Subchunk2Size    =3D=3D NumSamples *
NumChannels * BitsPerSample/8


	size=3DconvertToInt(buffer,4);
	std::cout<<"40  4   Subchunk2Size =3D=3D NumSamples * NumChannels *
BitsPerSample/8   but  size of data is___________"<<std::dec<<size<<"
bytes\n\n";

	char* data=3Dnew char[size];
	in.read(data,size);
	rrr =3D 0;
	if (bps =3D=3D 16 && chan =3D=3D 1)//mono_16
		{///////////11111111111111111111111
	  	  for (rr =3D 0; rr < size/200; rr =3D rr + 2)// just to show a fract=
ion of
the wav
			 {//2
			   r1 =3D data[rr + 1];
			   r2 =3D r1;
			   r2 =3D r2 << 8;
			   r1 =3D 0x00ff & data[rr];
			   r2 =3D r2 | r1;
			   if (rrr =3D=3D 20){std::cout<<"\n"; rrr =3D 0;}//to print 36 charat=
ers and
then print a new line
			   std::cout<<"  "<<std::dec<<r2;
			   rrr =3D rrr +1;
			  }
	  	  std::cout<<"     mono_16\n\n\n";
//440 sine wave has =E2=89=88 50.113636 sampeles at 22050 for a one compl=
et sine
wave.
//25 positive value and 25 negitive values for 440hz and 22050 sample rat=
e
		 }


	if (bps =3D=3D 8 && chan =3D=3D 1)//mono_8 bit
		{///////////2222222222222222
		 for (rr =3D 0; rr < size/400; rr =3D rr + 1)// just to show a fraction=
 of the
wav
										{
									  	  rrrr =3D data[rr];
									  	  rrrr =3D 0xff & rrrr;
//										  std::cout<<std::dec<<r1<<"+";//8bit
										  if (rrr =3D=3D 36){std::cout<<"\n"; rrr =3D 0;}//line return =
for
dispaly purpose's
										  std::cout<<" "<<std::dec<<rrrr;
										  rrr =3D rrr +1;
										 }
	std::cout<<"     mono_8\n\n\n";
		 }//////////222222222222222222222
	return data;
}

int main(int argc,char** argv)
{

	int channel,sampleRate,bps,size;
	char* fffffile =3D argv[1];
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
	//char* data=3DloadWAV("cross.wav",channel,sampleRate,bps,size);
	//char* data=3DloadWAV("Capture.wav",channel,sampleRate,bps,size);
	char* data=3DloadWAV(fffffile,channel,sampleRate,bps,size);
///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
	ALCdevice* device=3DalcOpenDevice(NULL);
	if(device=3D=3DNULL)
	{
		std::cout << "cannot open sound card" << std::endl;
		return 0;
	}
	ALCcontext* context=3DalcCreateContext(device,NULL);
	if(context=3D=3DNULL)
	{
		std::cout << "cannot open context" << std::endl;
		return 0;
	}
	alcMakeContextCurrent(context);

	unsigned int bufferid,format;
	alGenBuffers(1,&bufferid);
	if(channel=3D=3D1)
	{
		if(bps=3D=3D8)
		{
			format=3DAL_FORMAT_MONO8;
		}else{
			format=3DAL_FORMAT_MONO16;	=09
		}
	}else{
		if(bps=3D=3D8)
		{
			format=3DAL_FORMAT_STEREO8;
		}else{
			format=3DAL_FORMAT_STEREO16;	=09
		}=09
	}
	alBufferData(bufferid,format,data,size,sampleRate);
	unsigned int sourceid;
	alGenSources(1,&sourceid);
	alSourcei(sourceid,AL_BUFFER,bufferid);
	alSourcePlay(sourceid);
	sleep(3);

	alDeleteSources(1,&sourceid);
	alDeleteBuffers(1,&bufferid);
=09
	alcDestroyContext(context);
	alcCloseDevice(device);
	delete[] data;
	return 0;
}





--
View this message in context: http://openal.996291.n3.nabble.com/Applying=
-FFT-to-a-wav-tp5521p5522.html
Sent from the OpenAL - User mailing list archive at Nabble.com.

_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal