Linux DSP plays audio file

Source: Internet
Author: User

#include <unistd.h>#include<fcntl.h>#include<sys/types.h>#include<sys/ioctl.h>#include<stdlib.h>#include<stdio.h>#include<linux/soundcard.h>/*The following three parameters are related to a specific file * cmd:file audio file * [file Pass.wav] =>> Pass.wav:RIFF (Little-endian) data, WAVE Audio, Microso FT PCM, + bit, mono 44100 Hz * 1. 16-bit * 2. Mono for the channel =1, stereo for stereo = 2 * 3. 44100HZ frequency This is what we all know, and the number of bits to capture or play audio for a second. */#defineRate 44100#defineSIZE 16#defineCHANNELS 1//1 for Mono, 2 for stereo/*/buffer * /unsignedCharBuff[rate * SIZE * CHANNELS/8];//The buff has exactly one second of audio in it.intMain () {intFD; intwavfd;/*a descriptor for a WAV file*/    intArg/*IOCTL Parameters*/    intRet/*return value*/    /*turn on the DSP audio device*/FD= Open ("/DEV/DSP", o_wronly); if(FD <0) {printf ("Open OF/DEV/DSP failed"); Exit (1); } wavfd= Open ("Pass.wav", o_rdonly); if(Wavfd <0) {printf ("Open of WAV failed");        Close (FD); Exit (1); }    /*Set bit*/Arg=SIZE; RET= IOCTL (FD, Sound_pcm_write_bits, &Arg); if(ret = =-1) perror ("sound_pcm_write_bits IOCTL failed"); if(Arg! =SIZE) perror ("Unable to set sample size"); /*Set Channels*/Arg=CHANNELS; RET= IOCTL (FD, Sound_pcm_write_channels, &Arg); if(ret = =-1) perror ("Sound_pcm_write_channels IOCTL failed"); if(Arg! =CHANNELS) perror ("Unable to set number of channels"); /*set up rate*/Arg=Rate ; RET= IOCTL (FD, Sound_pcm_write_rate, &Arg); if(ret = =-1) perror ("Sound_pcm_write_write IOCTL failed"); /*read the Buff-size content from the WAV file, and then write to/DEV/DSP until the end of the file*/    /*Q: Here I tried to play two times, do not know what the situation, if you have encountered please advise. */     while(ret = read (wavfd, Buff,sizeof(buff))) >0) {        //printf ("Read size =%d\n", ret);Write (FD, Buff,sizeof(Buff)); /*The following code is used to play back the contents of the buffer when changing the parameters of the playing file*/ret= IOCTL (FD, Sound_pcm_sync,0); if(ret = =-1) perror ("Sound_pcm_sync IOCTL failed");    } close (FD); Close (WAVFD);} The following is an encapsulated interface that can be used directly:voidPlay_audio (intRateintBitsintChannelsChar*filename) {    intFD; intwavfd;/*a descriptor for a WAV file*/    intArg/*IOCTL Arg*/    intRet/*return value*/unsignedCharBuff[rate * bits * channels/8];//The buff has exactly one second of audio in it.    /*Open Device*/FD= Open ("/DEV/DSP", o_wronly); if(FD <0) {printf ("Open OF/DEV/DSP failed"); Exit (1); } wavfd=open (filename, o_rdonly); if(Wavfd <0) {printf ("Open of WAV failed");        Close (FD); Exit (1); }    /*Set Bits*/Arg=bits; RET= IOCTL (FD, Sound_pcm_write_bits, &Arg); if(ret = =-1) perror ("sound_pcm_write_bits IOCTL failed"); if(Arg! =SIZE) perror ("Unable to set sample size"); /*Set Channels*/Arg=channels; RET= IOCTL (FD, Sound_pcm_write_channels, &Arg); if(ret = =-1) perror ("Sound_pcm_write_channels IOCTL failed"); if(Arg! =CHANNELS) perror ("Unable to set number of channels"); /*Set Rate*/Arg=Rate ; RET= IOCTL (FD, Sound_pcm_write_rate, &Arg); if(ret = =-1) perror ("Sound_pcm_write_write IOCTL failed"); /*read the Buff-size content from the WAV file, and then write to/DEV/DSP until the end of the file*/     while(ret = read (wavfd, Buff,sizeof(buff))) >0) {printf ("Read size =%d\n", ret); Write (fd, buff,sizeof(Buff)); /*The following code is used to play back the contents of the buffer when changing the parameters of the playing file*/ret= IOCTL (FD, Sound_pcm_sync,0); if(ret = =-1) perror ("Sound_pcm_sync IOCTL failed");    } close (FD); Close (wavfd);}

Linux DSP plays audio file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.