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 = mono, two channel = 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=72%20wave%20openal%20sound
http://www.ypass.net/blog/2010/01/pcm-audio-part-3-basic-audio-effects-volume-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=1;
return !((char*)&a)[0];
}
int convertToInt(char* buffer,int len)
{
int a=0;
if(!isBigEndian())
for(int i=0;i<len;i++)
((char*)&a)[i]=buffer[i];
else
for(int i=0;i<len;i++)
((char*)&a)[3-i]=buffer[i];
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";
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)!=0)
{
std::cout << "this is not a valid WAVE file" << std::endl;
return NULL;
}
in.read(buffer,4); //4 4 ChunkSize
samplerate=convertToInt(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";
in.read(buffer,2); //20 2 AudioFormat PCM =1
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 = 1, Stereo = 2,
etc.
chan=convertToInt(buffer,2);
std::cout<<"22 2 NumChannels Mono = 1, Stereo = 2,
etc.______________"<<std::dec<<int(buffer[0])<<" chanels\n";
in.read(buffer,4); //24 4 SampleRate 8000, 4410
samplerate=convertToInt(buffer,4);
std::cout<< "24 4 SampleRate 8000, 22500, 44100, etc. ______but is "<<
std::dec<<samplerate<<"\n";
in.read(buffer,4); //28 4 ByteRate == SampleRate *
NumChannels * BitsPerSample/8
rr=convertToInt(buffer,4);
std::cout<<"28 4 ByteRate_______"<<std::dec<<int(rr)<<"\n";
in.read(buffer,2); //32 2 BlockAlign == NumChannels *
BitsPerSample/8
// blockalign = short(buffer[0]);
blockalign = 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 = 8, 16 bits =
16, etc.
bps=convertToInt(buffer,2);
std::cout<<"34 2 BitsPerSample is 8 bits = 8, 16 bits = 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 == NumSamples *
NumChannels * BitsPerSample/8
size=convertToInt(buffer,4);
std::cout<<"40 4 Subchunk2Size == NumSamples * NumChannels *
BitsPerSample/8 but size of data is___________"<<std::dec<<size<<"
bytes\n\n";
char* data=new char[size];
in.read(data,size);
rrr = 0;
if (bps == 16 && chan == 1)//mono_16
{///////////11111111111111111111111
for (rr = 0; rr < size/200; rr = rr + 2)// just to show a fraction of
the wav
{//2
r1 = data[rr + 1];
r2 = r1;
r2 = r2 << 8;
r1 = 0x00ff & data[rr];
r2 = r2 | r1;
if (rrr == 20){std::cout<<"\n"; rrr = 0;}//to print 36 charaters and
then print a new line
std::cout<<" "<<std::dec<<r2;
rrr = rrr +1;
}
std::cout<<" mono_16\n\n\n";
//440 sine wave has ≈ 50.113636 sampeles at 22050 for a one complet sine
wave.
//25 positive value and 25 negitive values for 440hz and 22050 sample rate
}
if (bps == 8 && chan == 1)//mono_8 bit
{///////////2222222222222222
for (rr = 0; rr < size/400; rr = rr + 1)// just to show a fraction of the
wav
{
rrrr = data[rr];
rrrr = 0xff & rrrr;
// std::cout<<std::dec<<r1<<"+";//8bit
if (rrr == 36){std::cout<<"\n"; rrr = 0;}//line return for
dispaly purpose's
std::cout<<" "<<std::dec<<rrrr;
rrr = 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 = argv[1];
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
//char* data=loadWAV("cross.wav",channel,sampleRate,bps,size);
//char* data=loadWAV("Capture.wav",channel,sampleRate,bps,size);
char* data=loadWAV(fffffile,channel,sampleRate,bps,size);
///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
ALCdevice* device=alcOpenDevice(NULL);
if(device==NULL)
{
std::cout << "cannot open sound card" << std::endl;
return 0;
}
ALCcontext* context=alcCreateContext(device,NULL);
if(context==NULL)
{
std::cout << "cannot open context" << std::endl;
return 0;
}
alcMakeContextCurrent(context);
unsigned int bufferid,format;
alGenBuffers(1,&bufferid);
if(channel==1)
{
if(bps==8)
{
format=AL_FORMAT_MONO8;
}else{
format=AL_FORMAT_MONO16;
}
}else{
if(bps==8)
{
format=AL_FORMAT_STEREO8;
}else{
format=AL_FORMAT_STEREO16;
}
}
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);
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.