Time interval between USB audio card detection and being able to play audio - linux

I'm working on a project where a USB audio card is unplugged and plugged arbitrarily from a computer that will be playing sounds. My software should be playing sound as soon as possible right after the card is plugged in.
In order to detect the USB being connected, I'm using libusb's LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED and LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT events, and it works flawlessly. As soon as the device is connected to the USB port, the event is triggered. Same when it's removed.
The problem I'm facing is that there's a time gap between the moment libusb detects the device, and the moment I can actually start working with the device to play sound. I'm using miniaudio to play a sinewave, but the issue is the same using Qt Multimedia, so I'm guessing it has to do with the backend (ALSA).
I've had to place an explicit wait of around 5/6 seconds after the device is detected by libusb to be able to safely open it with miniaudio, but I'd rather not have that magic number right there.
Is there any other way of knowing when the device is ready? I've tried checking udev but it gets activated at the same time as libusb.

Related

Audio IN over USB on STM32F4

I try to create a simple USB audio interface with audio IN and OUT on a custom board based on an STM32F412. The audio OUT (from host to target) is working, also with the help of the CubeMX setup for the audio device usb class. But somehow I can't figure out how the opposite way (from target to host) should work.
I see for audio out, AUDIO_PeriodicTC_FS gets called periodically (every 1ms) with the AUDIO_OUT_TC command. It never gets called with AUDIO_IN_TC. I tried to call HAL_PCD_EP_Transmit with some audio data, but the host doesn't get the input...
The descriptor should be right, at least I see both interfaces (in and out) show up on the host.
Is someone experienced in this or can provide some working examples?
Look here. Used and verified. It works. It may need some deeper modification if your chip doesn't have USB OTG
Ive created a simple USB headset on my stm32f4 discovery board. You can check it out, if you're interested:
https://github.com/ancher-bohdan/stm32_usb_interface
It is far from ideal, but it can play music from host and send recorded sound from analog mic to host

Connect Multiple Bluetooth Speakers with Raspberry Pi

I'm trying to connect several bluetooth devices with Raspberry PI to use them as speakers.
I'm using RetroPie as a distribution, because of the tests I've done it's the only one that matches and allows continuous synchronization with several bluetooth devices at the same time.
However, the system only detects the first device that connects as a sound card, the rest keep the bluetooth synchronization active, but it does not interpret them as audio cards even though it is indicated by blueman-manager.
Is there anything I can do to keep all devices synchronized and supported as audio cards?
Palm,
even so this a old post, it is not yet closed.
As I just got the same issue, here my answer.
What you try to do is to connect mutliple audio sreaming services (Speakers) to 1 audio bluetooth source, that is not possible like this. Therefore your PC only connects to the first box.
You could try multiple bluetooth dongles, but then you run into the timing issue with audio stream syncronization between the boxes.
The only solution is to use Bluetooth 4.1 upwards master-slave services.
Therefore the Speakers need to be connected to each other and seen as 1 device from the PC. In that mode the stream is skewed to ensure syncronous playing.
Many new Bluetooth speakers support this mode.
Hope that helped.

How to make A2DP and HSP work simultaneously

I have a bluetooth headset. When connecting it to Windows 10, it installs two profiles in Playback devices list:
Hands-Free. (HSP profile)
Stereo. (A2DP profile)
The Second one (Stereo) is set to be the "Default Device" and the "Default Communication Device" on the system.
When I start any program that uses the mic (recorder, chat, VoIP Calls, gaming, etc.) The sound suddenly stops working And I can only use the mic until I stop the recorder or the call.
To enable the sound again I need to make the Hands-Free (HSP) profile handle both input and output (sound and mic). Unfortunately, HSP gives really poor sound quality.
I want to know If there is a way, using code, I can change Bluetooth behavior so the two profiles work simultaneously. One handles the sound and one handles the mic so I can have high quality sound and use the mic at the same time.
You will probably never find a solution. I had the same problem (I was trying to create a walkie talkie with 2 headsets connected to the same smartphone).
On Windows (but also on Android) you can't access directly to a BT-microphone or BT-speaker because it is automatically detected as BT headset and the OS take the control of the device.
Your app can then access the OS-device and not directly the hardware device. The only OS able to do that was Symbian I think which had the most BT-protocols. On Windows you will probably never be able to do that and on Android you have to write your own A2DP-protocol if you want to access the device directly without OS interference.
So sad...
Luckily, under windows you can define different devices for communications and sound.
So, you have two choices:
Choosing Hands-free for both mic/speaker only for communication (which will switch back to A2DP after the call/teams).
Choosing another mic for communication which allow you to still use the speaker profile even in communication.
That is a bluetooth restriction : A2DP (high quality audio) cannot be use simultaneously with HFP (hands-free profile)

Makes changes to asound.conf effective without restarting services?

I am running a mopidy based spotify player on a raspberry pi - my goal is to utilise both the onboard analog audio as well as an external USB sound card, with a different set of speakers in different rooms plugged into each, creating a multi room system.
The key is to be able to switch / redirect audio on the fly ie without rebooting, stopping music.
This is the closest I have gotten:
sudo nano /etc/asound.conf
pcm.!default {
type hw
card 1
}
ctl.!default {
type hw
card 1
}
change card from 0 (onboard analog) to 1 (external usb)
then
killall -9 mopidy
/opt/startmopidy.sh
This does the job but killing mopidy stops the music (and editing asound.conf doesn't have a high WAF!) but i cant get the setting to take effect without reloading mopidy.
Someone suggested I try Pulse Audio instead of ALSA but before I do want to see if I can find a method using ALSA....suggestions??
ALSA does not allow changing the hardware device without actually opening the other device, which would require support by the application.
You need PulseAudio for that.

Change flash player audio output device

Is there a way to change flash players audio output device? if not, is there a swf player who has this possibility? Thanks!
I had an issue until a few minutes ago regarding this.
Two audio devices are available to my XP box: an iMic USB audio I/O device, in which I have permanently plugged my desktop speakers; and a pair of USB headphones with microphone that I plug in occasionally.
The USB headset would take precedence over the USB iMic for applications because apps appear to access the last device plugged in to a USB port. With this in mind, here was my issue:
I would be listening to some Internettings on my USB headset.
Later, I would want to use my desktop speakers for the Internettings.
This entailed unplugging my headset, shutting down Firefox and opening it up again. Because the desktop speakers are considered the most recently plugged in device, they would be default for plugins.
This is damned annoying, I said to myself, and decided a little hacker mode was in order.
Keeping Firefox open, I used Task Manager to kill the "plugin-container.exe" process. This showed a crash screen on any Flash Player applet in Firefox. Then I unplug the headset, and reload the Web page with the applet. Without restarting Firefox, Flash will then play through my desktop speakers.
If I wish to listen on the headset again, I plug the headset in, kill plugin-container.exe, and reload the page. Wham.
For as rare as I intend to switch audio devices, this will cover up part of the mess Adobe left.
I am 99% sure that setting the audio device used by the flash player is something you would need to do on an OS level. You can change the device that flash uses for microphone and video input from the player's settings, but I don't think you can change audio output.
I have found a solution, at least for the Firefox browser, to direct HTML5 audio to a specific audio device:
https://addons.mozilla.org/en-US/firefox/addon/chaudev/
Note: this is a new Firefox addon waiting to be reviewed developed by a friend of mine.
I have been waiting for this for years.
I wanted to use this when my wife is seeing YouTube on her Chrome browser and me seeing anything like Coursera online MOOC lectures (FF) on the TV which is connected to the pc. I wanted to hear my classes on the headset and my wife on the speakers for YouTube.
I have 2 mouses (one for me, wireless) and have installed a neat program called TeamPlayer which gives multiple cursors (each cursor for each mouse).
So I have now literally the capability of 2 persons working on 1 pc.
And on top of that it works seamlessly with "Enounce Myspeed" for speeding up the video lectures' playback.

Resources