Guest User

Untitled

a guest
Jul 11th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. struct WAVE_head
  2. {
  3.  char riff_signature[4];
  4.  unsigned long int riff_length:32;
  5.  char wave_signature[4];
  6.  char format[4];
  7.  unsigned long int wave_length:32;
  8.  unsigned short int type:16;
  9.  unsigned short int channels:16;
  10.  unsigned long int rate:32;
  11.  unsigned long int bytes:32;
  12.  unsigned short int align:16;
  13.  unsigned short int bits:16;
  14.  char sample_signature[4];
  15.  unsigned long int sample_length:32;
  16. };
  17.  
  18. class SDGF_Sound
  19. {
  20.  private:
  21.  int mixer;
  22.  int sound;
  23.  public:
  24.  SDGF_Sound();
  25.  ~SDGF_Sound();
  26.  void set_volume(int volume);
  27.  int get_volume();
  28.  bool write_data(unsigned char *data, unsigned long int length);
  29.  SDGF_Sound* get_handle();
  30. };
  31.  
  32. SDGF_Sound::SDGF_Sound()
  33. {
  34.  int setting;
  35.  sound=open("/dev/dsp",O_RDWR|O_EXCL|O_NONBLOCK,S_IRWXU|S_IRWXG|S_IRWXO);
  36.  if (sound==-1)
  37.  {
  38.   puts("Can't get access to sound card");
  39.   exit(EXIT_FAILURE);
  40.  }
  41.  mixer=open("/dev/mixer",O_RDWR,S_IRWXU|S_IRWXG|S_IRWXO);
  42.  if (mixer==-1)
  43.  {
  44.   puts("Can't get access to sound mixer");
  45.   exit(EXIT_FAILURE);
  46.  }
  47.  setting=0xffff;
  48.  if(ioctl(mixer,SOUND_MIXER_WRITE_MIC,&setting)==-1)
  49.  {
  50.   puts("Can't configure sound mixer");
  51.   exit(EXIT_FAILURE);
  52.  }
  53.  setting=AFMT_S16_LE;
  54.  if(ioctl(sound,SNDCTL_DSP_SETFMT,&setting)==-1)
  55.  {
  56.   puts("Can't set sound format");
  57.   exit(EXIT_FAILURE);
  58.  }
  59.  setting=2;
  60.  if(ioctl(sound,SNDCTL_DSP_CHANNELS,&setting)==-1)
  61.  {
  62.   puts("Can't set number of audio channels");
  63.   exit(EXIT_FAILURE);
  64.  }
  65.  setting=44100;
  66.  if(ioctl(sound,SNDCTL_DSP_SPEED,&setting)==-1)
  67.  {
  68.   puts("Can't configure sound card");
  69.   exit(EXIT_FAILURE);
  70.  }
  71.  if(ioctl(sound,SNDCTL_DSP_NONBLOCK,NULL)==-1)
  72.  {
  73.   puts("Can't set sample rate");
  74.   exit(EXIT_FAILURE);
  75.  }
  76.  
  77. }
  78.  
  79. SDGF_Sound::~SDGF_Sound()
  80. {
  81.  close(sound);
  82.  close(mixer);
  83. }
  84.  
  85. void SDGF_Sound::set_volume(int volume)
  86. {
  87.  int level;
  88.  if(volume<0) volume=0;
  89.  if(volume>255) volume=255;
  90.  level=(volume << 8)|volume;
  91.  if(ioctl(mixer,SOUND_MIXER_WRITE_VOLUME,&level)==-1)
  92.  {
  93.   puts("Can't set volume level");
  94.   exit(EXIT_FAILURE);
  95.  }
  96.  
  97. }
  98.  
  99. int SDGF_Sound::get_volume()
  100. {
  101.  int left,right,volume;
  102.  left=0;
  103.  right=0;
  104.  if(ioctl(mixer,SOUND_MIXER_READ_VOLUME,&volume)>=0)
  105.  {
  106.   left=volume & 0xff;
  107.   right=(volume >> 8) & 0xff;
  108.  }
  109.  return left|right;
  110. }
  111.  
  112. bool SDGF_Sound::write_data(unsigned char *data, unsigned long int length)
  113. {
  114.  bool result;
  115.  result=true;
  116.  if(write(sound,data,length)<=0)
  117.  {
  118.   if((errno==EAGAIN)||(errno==EBUSY)) result=false;
  119.  }
  120.  return result;
  121. }
  122.  
  123. SDGF_Sound* SDGF_Sound::get_handle()
  124. {
  125.  return this;
  126. }
  127.  
  128. class SDGF_Player
  129. {
  130.  private:
  131.  SDGF_Sound *Sound;
  132.  unsigned char *data;
  133.  unsigned long int sample;
  134.  unsigned long int index;
  135.  unsigned long int length;
  136.  public:
  137.  SDGF_Player();
  138.  ~SDGF_Player();
  139.  void unload();
  140.  void load(unsigned char *audio,unsigned long int total,unsigned long int sample_length);
  141.  void initialize(SDGF_Sound *sound);
  142.  bool play();
  143.  void rewind_audio();
  144. };
  145.  
  146. SDGF_Player::SDGF_Player()
  147. {
  148.  Sound=NULL;
  149.  data=NULL;
  150.  sample=0;
  151.  index=0;
  152.  length=0;
  153. }
  154.  
  155. SDGF_Player::~SDGF_Player()
  156. {
  157.  Sound=NULL;
  158.  if(data!=NULL) free(data);
  159. }
  160.  
  161. void SDGF_Player::unload()
  162. {
  163.  sample=0;
  164.  index=0;
  165.  length=0;
  166.  if(data!=NULL) free(data);
  167. }
  168.  
  169. void SDGF_Player::load(unsigned char *audio,unsigned long int total,unsigned long int sample_length)
  170. {
  171.  this->unload();
  172.  data=audio;
  173.  length=total;
  174.  sample=sample_length;
  175. }
  176.  
  177. void SDGF_Player::initialize(SDGF_Sound *sound)
  178. {
  179.  Sound=sound;
  180. }
  181.  
  182. bool SDGF_Player::play()
  183. {
  184.  bool result;
  185.  unsigned long int piece;
  186.  piece=sample;
  187.  result=false;
  188.  if(index<length)
  189.  {
  190.   if(piece>length-index) piece=length-index;
  191.   if(Sound->write_data(&data[index],piece)==true)
  192.   {
  193.    index+=piece;
  194.    result=true;
  195.   }
  196.  
  197.  }
  198.  return result;
  199. }
  200.  
  201. void SDGF_Player::rewind_audio()
  202. {
  203.  index=0;
  204. }
  205.  
  206. class SDGF_Audio
  207. {
  208.  public:
  209.  void load_wave(const char *name,SDGF_Player &player);
  210. };
  211.  
  212. void SDGF_Audio::load_wave(const char *name,SDGF_Player &player)
  213. {
  214.  FILE *target;
  215.  WAVE_head head;
  216.  unsigned char *data;
  217.  target=fopen(name,"rb");
  218.  if(target==NULL)
  219.  {
  220.   puts("Can't open a sound file");
  221.   exit(EXIT_FAILURE);
  222.  }
  223.  fread(&head,44,1,target);
  224.  if((strncmp(head.riff_signature,"RIFF",4)!=0)&&(strncmp(head.wave_signature,"WAVE",4)!=0))
  225.  {
  226.   puts("Incorrect sound format");
  227.   exit(EXIT_FAILURE);
  228.  }
  229.  if((head.type!=1)&&(head.bits!=16))
  230.  {
  231.   puts("Incorrect sound format");
  232.   exit(EXIT_FAILURE);
  233.  }
  234.  data=(unsigned char*)calloc(head.sample_length,1);
  235.  if(data==NULL)
  236.  {
  237.   puts("Can't allocate memory for sound buffer");
  238.   exit(EXIT_FAILURE);
  239.  }
  240.  fread(data,head.sample_length,1,target);
  241.  fclose(target);
  242.  player.load(data,head.sample_length,head.bytes);
  243. }
Advertisement
Add Comment
Please, Sign In to add comment