Linux audio device driver problems

Source: Internet
Author: User
Linux audio device driver-Linux general technology-Linux programming and kernel information. For more information, see the following section. # Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define BUF_LENGTH 2000
# Define CHANNELS 0/* 0 -- single channel; 2 -- double channel */
# Define SAMPLE_RATE 8000

Int set_fmt (int audio_fd, int bits, int rate, int channel)
{
Int status =-1;

If (-1 = ioctl (audio_fd, SNDCTL_DSP_SAMPLESIZE, & bits ))
{
Perror ("ioctl SNDCTL_DSP_SAMPLESIZE ");
Exit (1 );
}

If (-1 = ioctl (audio_fd, SNDCTL_DSP_SPEED, & rate ))
{
Perror ("ioctl SNDCTL_DSP_SPEED ");
Exit (1 );
}

If (-1 = ioctl (audio_fd, SNDCTL_DSP_CHANNELS, & channel ))
{
Perror ("ioctl SNDCTL_DSP_CHANNELS ");
Exit (1 );
}

Return (0 );
}

Main ()
{
Int audio_fd = 0;
Int bytes = 0;
Int I = 0;
Char buf [BUF_LENGTH];


Audio_fd = open ("/dev/dsp", O_RDWR );
If (audio_fd <0)
{
Perror ("open/dev/dsp ");
Exit (1 );
}

If (-1 = (set_fmt (audio_fd, AFMT_U8, SAMPLE_RATE, CHANNELS )))
{
Perror ("set_fmt ");
Exit (1 );
}

For (I = 0; I <5000; I ++)
{
If (-1 = (bytes = read (audio_fd, buf, BUF_LENGTH )))
{
Perror ("read ");
Exit (1 );
}
If (-1 = write (audio_fd, buf, bytes ))
{
Perror ("write ");
Exit (1 );
}

}

Printf ("It'll be closed \ n ");
Close (audio_fd );
}
It cannot be used after compilation. There is no error message, but no sound. Why ??
Related Article

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.