Playing wav files in C language in Linux

Source: Internet
Author: User
# Include <unistd. h>
# Include <fcntl. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <sys/IOCTL. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>
# Include <Linux/Soundcard. h>

# Define open_dsp_failed 0x00000001/* failed to open DSP! */
# Define samplerate_status 0x00000002/* samplerate status failed */
# Define set_samplerate_failed 0x00000003/* Set samplerate failed */
# Define channels_status 0x00000004/* channels status failed */
# Define set_channels_failed 0x00000005/* set channels failed */
# Define fmt_status 0x00000006/* FMT status failed */
# Define set_fmt_failed 0x00000007/* Set FMT failed */
# Define open_file_failed 0x00000008/* opem filed failed */

Int p8100_audio_play (char * pathname, int nsamplerate, int nchannels, int FMT)
{
Int dsp_fd, mix_fd, status, ARG;
Dsp_fd = open ("/dev/DSP", o_rdwr);/* Open DSP */
If (dsp_fd <0)
{
Return open_dsp_failed;
}
Arg = nsamplerate;
Status = IOCTL (dsp_fd, sound_pcm_write_rate, & Arg);/* Set samplerate */
If (status <0)
{
Close (dsp_fd );
Return samplerate_status;
}
If (Arg! = Nsamplerate)
{
Close (dsp_fd );
Return set_samplerate_failed;
}
Arg = nchannels;/* set channels */
Status = IOCTL (dsp_fd, sound_pcm_write_channels, & Arg );
If (status <0)
{
Close (dsp_fd );
Return channels_status;
}
If (Arg! = Nchannels)
{
Close (dsp_fd );
Return set_channels_failed;
}
Arg = FMT;/* Set bit FMT */
Status = IOCTL (dsp_fd, sound_pcm_write_bits, & Arg );
If (status <0)
{
Close (dsp_fd );
Return fmt_status;
}
If (Arg! = FMT)
{
Close (dsp_fd );
Return set_fmt_failed;
}/* All DSP parameters have been set here */
File * file_fd = fopen (pathname, "R ");
If (file_fd = NULL)
{
Close (dsp_fd );
Return open_file_failed;
}
Int num = 3 * nchannels * nsamplerate * FMT/8;
Int get_num;
Char Buf [num];
While (feof (file_fd) = 0)
{
Get_num = fread (BUF, 1, num, file_fd );
Write (dsp_fd, Buf, get_num );
If (get_num! = Num)
{
Close (dsp_fd );
Fclose (file_fd );
Return 0;
}
}
Close (dsp_fd );
Fclose (file_fd );
Return 0;
}
/*
* Test
*/
Int main ()
{
Int value;

Value = p8100_audio_play ("/Windows/C/Windows/Media/Windows startup.wav", 44100,2, 16); // pay attention to the playback file path !!
Fprintf (stderr, "value is % d", value );
Return 0;
}

 

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.