Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

c - ALSA Api: How to play two wave files simultaneously?

What is the required API configuration/call for playing two independent wavefiles overlapped ? I tried to do so , I am getting resource busy error. Some pointers to solve the problem will be very helpful.

Following is the error message from snd_pcm_prepare() of the second wavefile

"Device or resource busy"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can configure ALSA's dmix plugin to allow multiple applications to share input/output devices.

An example configuration to do this is below:

pcm.dmixed {
    type dmix
    ipc_key 1024
    ipc_key_add_uid 0
    slave.pcm "hw:0,0"
}
pcm.dsnooped {
    type dsnoop
    ipc_key 1025
    slave.pcm "hw:0,0"
}

pcm.duplex {
    type asym
    playback.pcm "dmixed"
    capture.pcm "dsnooped"
}

# Instruct ALSA to use pcm.duplex as the default device
pcm.!default {
    type plug
    slave.pcm "duplex"
}
ctl.!default {
    type hw
    card 0
}

This does the following:

  • creates a new device using the dmix plugin, which allows multiple apps to share the output stream
  • creates another using dsnoop which does the same thing for the input stream
  • merges these into a new duplex device that will support input and output using the asym plugin
  • tell ALSA to use the new duplex device as the default device
  • tell ALSA to use hw:0 to control the default device (alsamixer and so on)

Stick this in either ~/.asoundrc or /etc/asound.conf and you should be good to go.

For more information see http://www.alsa-project.org/main/index.php/Asoundrc#Software_mixing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...