Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct WAVE_head
- {
- char riff_signature[4];
- unsigned long int riff_length:32;
- char wave_signature[4];
- char format[4];
- unsigned long int wave_length:32;
- unsigned short int type:16;
- unsigned short int channels:16;
- unsigned long int rate:32;
- unsigned long int bytes:32;
- unsigned short int align:16;
- unsigned short int bits:16;
- char sample_signature[4];
- unsigned long int sample_length:32;
- };
- class SDGF_Sound
- {
- private:
- int mixer;
- int sound;
- public:
- SDGF_Sound();
- ~SDGF_Sound();
- void set_volume(int volume);
- int get_volume();
- bool write_data(unsigned char *data, unsigned long int length);
- SDGF_Sound* get_handle();
- };
- SDGF_Sound::SDGF_Sound()
- {
- int setting;
- sound=open("/dev/dsp",O_RDWR|O_EXCL|O_NONBLOCK,S_IRWXU|S_IRWXG|S_IRWXO);
- if (sound==-1)
- {
- puts("Can't get access to sound card");
- exit(EXIT_FAILURE);
- }
- mixer=open("/dev/mixer",O_RDWR,S_IRWXU|S_IRWXG|S_IRWXO);
- if (mixer==-1)
- {
- puts("Can't get access to sound mixer");
- exit(EXIT_FAILURE);
- }
- setting=0xffff;
- if(ioctl(mixer,SOUND_MIXER_WRITE_MIC,&setting)==-1)
- {
- puts("Can't configure sound mixer");
- exit(EXIT_FAILURE);
- }
- setting=AFMT_S16_LE;
- if(ioctl(sound,SNDCTL_DSP_SETFMT,&setting)==-1)
- {
- puts("Can't set sound format");
- exit(EXIT_FAILURE);
- }
- setting=2;
- if(ioctl(sound,SNDCTL_DSP_CHANNELS,&setting)==-1)
- {
- puts("Can't set number of audio channels");
- exit(EXIT_FAILURE);
- }
- setting=44100;
- if(ioctl(sound,SNDCTL_DSP_SPEED,&setting)==-1)
- {
- puts("Can't configure sound card");
- exit(EXIT_FAILURE);
- }
- if(ioctl(sound,SNDCTL_DSP_NONBLOCK,NULL)==-1)
- {
- puts("Can't set sample rate");
- exit(EXIT_FAILURE);
- }
- }
- SDGF_Sound::~SDGF_Sound()
- {
- close(sound);
- close(mixer);
- }
- void SDGF_Sound::set_volume(int volume)
- {
- int level;
- if(volume<0) volume=0;
- if(volume>255) volume=255;
- level=(volume << 8)|volume;
- if(ioctl(mixer,SOUND_MIXER_WRITE_VOLUME,&level)==-1)
- {
- puts("Can't set volume level");
- exit(EXIT_FAILURE);
- }
- }
- int SDGF_Sound::get_volume()
- {
- int left,right,volume;
- left=0;
- right=0;
- if(ioctl(mixer,SOUND_MIXER_READ_VOLUME,&volume)>=0)
- {
- left=volume & 0xff;
- right=(volume >> 8) & 0xff;
- }
- return left|right;
- }
- bool SDGF_Sound::write_data(unsigned char *data, unsigned long int length)
- {
- bool result;
- result=true;
- if(write(sound,data,length)<=0)
- {
- if((errno==EAGAIN)||(errno==EBUSY)) result=false;
- }
- return result;
- }
- SDGF_Sound* SDGF_Sound::get_handle()
- {
- return this;
- }
- class SDGF_Player
- {
- private:
- SDGF_Sound *Sound;
- unsigned char *data;
- unsigned long int sample;
- unsigned long int index;
- unsigned long int length;
- public:
- SDGF_Player();
- ~SDGF_Player();
- void unload();
- void load(unsigned char *audio,unsigned long int total,unsigned long int sample_length);
- void initialize(SDGF_Sound *sound);
- bool play();
- void rewind_audio();
- };
- SDGF_Player::SDGF_Player()
- {
- Sound=NULL;
- data=NULL;
- sample=0;
- index=0;
- length=0;
- }
- SDGF_Player::~SDGF_Player()
- {
- Sound=NULL;
- if(data!=NULL) free(data);
- }
- void SDGF_Player::unload()
- {
- sample=0;
- index=0;
- length=0;
- if(data!=NULL) free(data);
- }
- void SDGF_Player::load(unsigned char *audio,unsigned long int total,unsigned long int sample_length)
- {
- this->unload();
- data=audio;
- length=total;
- sample=sample_length;
- }
- void SDGF_Player::initialize(SDGF_Sound *sound)
- {
- Sound=sound;
- }
- bool SDGF_Player::play()
- {
- bool result;
- unsigned long int piece;
- piece=sample;
- result=false;
- if(index<length)
- {
- if(piece>length-index) piece=length-index;
- if(Sound->write_data(&data[index],piece)==true)
- {
- index+=piece;
- result=true;
- }
- }
- return result;
- }
- void SDGF_Player::rewind_audio()
- {
- index=0;
- }
- class SDGF_Audio
- {
- public:
- void load_wave(const char *name,SDGF_Player &player);
- };
- void SDGF_Audio::load_wave(const char *name,SDGF_Player &player)
- {
- FILE *target;
- WAVE_head head;
- unsigned char *data;
- target=fopen(name,"rb");
- if(target==NULL)
- {
- puts("Can't open a sound file");
- exit(EXIT_FAILURE);
- }
- fread(&head,44,1,target);
- if((strncmp(head.riff_signature,"RIFF",4)!=0)&&(strncmp(head.wave_signature,"WAVE",4)!=0))
- {
- puts("Incorrect sound format");
- exit(EXIT_FAILURE);
- }
- if((head.type!=1)&&(head.bits!=16))
- {
- puts("Incorrect sound format");
- exit(EXIT_FAILURE);
- }
- data=(unsigned char*)calloc(head.sample_length,1);
- if(data==NULL)
- {
- puts("Can't allocate memory for sound buffer");
- exit(EXIT_FAILURE);
- }
- fread(data,head.sample_length,1,target);
- fclose(target);
- player.load(data,head.sample_length,head.bytes);
- }
Advertisement
Add Comment
Please, Sign In to add comment