Sound Card programming in Linux (recording/releasing audio files)

Source: Internet
Author: User

From: http://www.linuxidc.com/Linux/2009-08/21427.htm

Reference material: <Linux Programming Technology details> edited by Du Hua

Page number: p186

You can write an audio file into the device file of the sound card to play the audio file. Using the READ function to read the content in the sound card device file can enable the recording function. The program code below realizes the recording function of the sound card device in Linux.

The Code is as follows:

// P6.8.c sound card recording function # include <unistd. h> # include <fcntl. h> # include <sys/types. h> # include <sys/IOCTL. h> # include <stdlib. h> # include <stdio. h> # include <Linux/Soundcard. h> // recording time # define length 3 // sampling frequency # define rate 8000 // quantize bits # define size 16 // number of audio channels http://ubuntuone.cn/#define channels 2 // Save the recording audio data unsigned char Buf [length * rate * size * channels/8]; int main (void) {// file descriptor of the sound device int FD; int ARG; // used to save the int status returned by IOCTL; // enable the sound Device FD = open ("/dev/DSP", o_rdwr); If (FD <0) {perror ("cannot open/dev/DSP device"); return 1 ;} // set the following sound card parameters // set the quantified bits for sampling: Arg = size; status = IOCTL (FD, sound_pcm_write_bits, & Arg); If (status =-1) {perror ("cannot set sound_pcm_write_bits"); return 1;} // set the number of sample channels Arg = channels; status = IOCTL (FD, sound_pcm_write_channels, & Arg ); if (status =-1) {perror ("cannot set sound_pcm_write_channels"); return 1 ;}// set the sampling frequency ar G = rate; status = IOCTL (FD, sound_pcm_write_rate, & Arg); If (status =-1) {perror ("cannot set sound_pcm_write_rate"); return 1 ;} // keep recording until you press "control-c" to stop while (1) {printf ("recording...: \ n "); status = read (FD, Buf, sizeof (BUF); If (status =-1) {perror ("read wrong number of bytes");} printf ("play...: \ n "); status = write (FD, Buf, sizeof (BUF); If (status! = Sizeof (BUF) perror ("wrote wrong number of bytes"); // wait until the playback ends before resuming the recording. Status = IOCTL (FD, sound_pcm_sync, 0 ); if (status =-1) perror ("cannot set sound_pcm_sync");} return 0 ;}

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.