[linux-audio-dev] alsa-playback (in plain c)

kurmisk at inbox.lv kurmisk at inbox.lv
Fri Feb 6 01:11:51 UTC 2004


>   I wanted to do a simple task. I just want to playback a raw file (16BIT,
> stereo, 44100Hz). So I used the alsa_pcm_seq-howto and compied a bit of code
> from it. But it doesn't work. I get the device read for playing and I get
> sound... But it's just noise. I'm frustrated. I tried to copy the relevant
> part out of the aplay-sources, but they have so many config stuff... Can
> anyone help me with a bit of code or a place where I should just get some
> code
> for only:


Moin


>   1. opening a raw-file

Use functions
open() 
read() 
close()

or
fopen() 
fread() 
fclose()


Examples  deutsch
http://pronix.de/modules/C/openbook/
http://pronix.de/modules/C/openbook/c_programmierung_19_5.php#25

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef __unix__
      #include <unistd.h>
#elif __MSDOS__ || __WIN32__ || _MSC_VER
      #include <io.h>
#endif

int main()
{
   int fh;

   if((fh=open("adress.txt",O_RDWR|O_CREAT|O_TRUNC))==-1)
      {
         printf("Fehler bei open()…\n");
         exit (0);
      }
   close(fh);
   return 0;
}


2. setting up the alsa-driver for playbakc

sorry  This is for oss 

int audio_fd;  

/*----*/void open_sound_device(void){
/*----*/ int format;   int stereo = 1;   int rate = 44100;
/*----*/ if ((audio_fd = open("/dev/dsp", O_WRONLY , 0)) == -1)  
/*----*/   {  
/*----*/     perror("/dev/dsp");  
/*----*/     exit(1);  
/*----*/   }   
/*----*/ format = AFMT_S16_LE;  
/*----*/ if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format)==-1)  
/*----*/   {  
/*----*/     perror("SNDCTL_DSP_SETFMT");     exit(1);  
/*----*/   }
/*----*/ if (format != AFMT_S16_LE)  
/*----*/          {   fprintf(stderr,"Output format not set correctly");  }
/*----*/ if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo)==-1)
/*----*/   {
/*----*/     perror("SNDCTL_DSP_STEREO");     exit(1);
/*----*/   }
/*----*/ if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate)==-1)  
/*----*/  { /* Fatal error */  
/*----*/    perror("SNDCTL_DSP_SPEED");       exit(1);  
/*----*/  }  
/*----*/}   //  o s d   //

/*----*/void close_sound_device(void)
/*----*/{
/*----*/  close(audio_fd);
/*----*/}



3. Read data from the file and write it to the soundcard.

 status = read(fdi, adat , sizeof( adat )); /* record some sound */
///// wird etwas gemacht mit adat  z.b.  audio_buffer[i] = adat[i];
 write(audio_fd , audio_buffer , sizeof( audio_buffer  ) );


Ralfs K




More information about the Linux-audio-dev mailing list