Solves the full duplex problem of the mini2440 sound card and enables simultaneous recording and playback.

Source: Internet
Author: User

# Include <unistd. h> <br/> # include <fcntl. h> <br/> # include <sys/types. h> <br/> # include <sys/IOCTL. h> <br/> # include <stdlib. h> <br/> # include <stdio. h> <br/> # include <Linux/Soundcard. h> <br/> # include <pthread. h> <br/> # define length 3 <br/> # define rate 8000 <br/> # define size 8 <br/> # define Channels 1 <br/> unsigned char buf [length * rate * size * channels/8]; <br/> int main () <br/> {<br/> int fd_r, fd_w; <Br/> int ARG; <br/> int status; <br/> pid_t PID; <br/> int recing = 1, playing = 0; <br/> pid = fork (); <br/> If (PID <0) {<br/> perror ("error in fork/N "); <br/>}< br/> If (pid = 0) {<br/> fd_r = open ("/dev/DSP", o_rdonly ); <br/> If (fd_r <0) {<br/> perror ("open read of/dev/DSP failed"); <br/> exit (1 ); <br/>}</P> <p> Arg = size; <br/> Status = IOCTL (fd_r, sound_pcm_write_bits, & Arg ); <br/> If (status =-1) <br/> Per Ror ("sound_pcm_write_bits IOCTL failed"); <br/> If (Arg! = Size) <br/> perror ("unable to set sample size"); </P> <p> Arg = channels; <br/> Status = IOCTL (fd_r, sound_pcm_write_channels, & Arg); <br/> If (status =-1) <br/> perror ("sound_pcm_write_channels IOCTL failed"); <br/> If (Arg! = Channels) <br/> perror ("unable to set number of channels"); </P> <p> Arg = rate; <br/> Status = IOCTL (fd_r, sound_pcm_write_rate, & Arg); <br/> If (status =-1) <br/> perror ("sound_pcm_write_write IOCTL failed "); </P> <p> while (1) <br/> {<br/> If (playing = 0) {<br/> recing = 1; <br/> printf ("Say something:"); <br/> Status = read (fd_r, Buf, sizeof (BUF )); <br/>/* recording */<br/> If (status! = Sizeof (BUF) <br/> perror ("read wrong number of bytes"); <br/> recing = 0; <br/>}< br/> else {<br/> fd_w = open ("/dev/DSP", o_wronly ); <br/> If (fd_w <0) <br/> {<br/> perror ("Open write of/dev/DSP failed "); <br/> exit (1); <br/>}</P> <p> Arg = size; <br/> Status = IOCTL (fd_w, sound_pcm_write_bits, & Arg); <br/> If (status =-1) <br/> perror ("sound_pcm_write_bits IOCTL failed"); <br/> If (Arg! = Size) <br/> perror ("unable to set sample size"); </P> <p> Arg = channels; <br/> Status = IOCTL (fd_w, sound_pcm_write_channels, & Arg); <br/> If (status =-1) <br/> perror ("sound_pcm_write_channels IOCTL failed"); <br/> If (Arg! = Channels) <br/> perror ("unable to set number of channels"); </P> <p> Arg = rate; <br/> Status = IOCTL (fd_w, sound_pcm_write_rate, & Arg); <br/> If (status =-1) <br/> perror ("sound_pcm_write_write IOCTL failed"); <br/> while (1) <br/> {<br/> If (recing = 0) {<br/> playing = 1; <br/> Status = write (fd_w, Buf, sizeof (BUF); <br/>/* playback */<br/> If (status! = Sizeof (BUF) <br/> perror ("wrote wrong number of bytes"); <br/> Status = IOCTL (fd_w, sound_pcm_sync, 0 ); <br/> If (status =-1) <br/> perror ("sound_pcm_sync IOCTL failed"); <br/> playing = 0; <br/>}< br/> 

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.