Sound Card programming test in linux

Source: Internet
Author: User
Sound Card programming test in linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. Through the unremitting efforts of Xiaodi, he finally mastered sound card programming and realized a small recording.
  
Program, // hehe // admire
  
Of course it is still not perfect. I still have some details that I don't know very well. // shy. I will post the original code. Please help me.
  
Positive, // xixi
  
# Include
  
# Include
  
# Include
  
# Include
  
# Include
  
Main ()
  
{
  
Int id, fd, I, j;
  
Char testbuf [4096];
  
Id = open ("/dev/audio", O_RDWR );
  
Fd = open ("test.wav", O_RDWR );
  
If (id <0 | fd <0 ){
  
Printf ("error in open the device! \ N ");
  
Printf ("id % d \ t dsp % d \ t seq % d \ n", id, dsp, seq );
  
Exit (-1 );
  
}
  
Post = 0;
  
Ioctl (id, SNDCTL_DSP_RESET, (char *) & I );
  
Ioctl (id, SNDCTL_DSP_SYNC, (char *) & I );
  
I = 1;
  
Ioctl (id, SNDCTL_DSP_NONBLOCK, (char *) & I );
  
I = 8000;
  
Ioctl (id, SNDCTL_DSP_SPEED, (char *) & I );
  
I = 1;
  
Ioctl (id, SNDCTL_DSP_CHANNELS, (char *) & I );
  
I = 8;
  
Ioctl (id, SNDCTL_DSP_SETFMT, (char *) & I );
  
I = 3;
  
Ioctl (id, SNDCTL_DSP_SETTRIGGER, (char *) & I );
  
I = 3;
  
Ioctl (id, SNDCTL_DSP_SETFRAGMENT, (char *) & I );
  
I = 1;
  
Ioctl (id, SNDCTL_DSP_PROFILE, (char *) & I );
  
For (j = 0; j <10 ;){
  
I = read (id, testbuf, 4096 );
  
If (I> 0 ){
  
Write (fd, filebuf, I );
  
J ++;
  
}
  
}
  
Close (fd );
  
Close (id );
  
}
  
The program simply stores pure audio frequency data in test.wav and uses commands
  
Cat test.wav>/dev/audio can be played.
  
By the way, because the program needs to read and write the device files of the sound card, you must use the corresponding permissions, or
  
You are root.
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.