# 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;
}