blocking audio app for node.js and electron - node.js

I'm using nodejs and ubuntu 21.10. I have an electron app that has to play an mp3 file. Presently I do this by launching a system app to play the file. I want to know when the playback has finished. I have tried mplayer and mpg123. They seem to work similarly. I issue the command to play the file and the app I use, mplayer or mpg123, returns right away before the mp3 finished playing.
If you were to launch the player playing the file from the command line, the playback is acompanied by some changing textual display on the screen. I think these players play the track in a separate thread. I want something that blocks until the track is done.
The reason I want this is that I'm using the microphone immediately after the playback. I don't want the mic listening to the playback. Presently I use a timer to wait 3 seconds before going to the microphone, but that's not the best, I'm sure.
I think my code looks like this now:
const out = execSync("mpg123 ./sample.mp3");
Any help would be appreciated.

Related

Create dynamic audio broadcast stream (node, ffmpeg, ..?)

I have coded a videoboard. Like a soundboard but with video. You go to one URL that's just a black screen and another one which has a list of different videos (sender). When you click one of these videos it plays on the black screen (receiver). If you play 2 different videos at the same time both videos are shown next to each other on the receiver. That's working fine for several months now. It just creates multiple html video-elements with multiple source-tags (x265 mp4 and vp9 webm).
I recently made a discord bot which takes the webm, extracts the opus stream and plays its sound in the voice channel where the bot is connected. This has one disadvantage: It can only play one sound at a time. It happens a lot that there are multiple videos/sounds playing at the same time so this a bit of a bummer.
So I thought I should create a audiostream on the server which hosts the videoboard and just connect the bot to that stream. But I have no clue how to do this. All I know is that it's very likely going to involve ffmpeg.
What would be the best option here? What I think I would need is basically an infinite silence stream and the possibility to add a audio file onto that stream at any point which will play simultaneously with other audio files that were added before and have not ended payback yet. How is that possible? Somehow with m3u8 playlist-files or via rtsp protocol?
Thanks :)
I think it can be helpful for you https://bitbucket.org/kaleniuk_ihor/neuro_vision/src/db_watch/
Also this library was very useful for me https://github.com/kyriesent/node-rtsp-stream you can just install npm i node-rtsp-stream

How to make server play sound when receiving request

I want to make a server play a soundbyte every time it receives a request. Is there a way to do this if I'm using a Go based server? The idea would be the server is hosting a browser window, it receives a request and the browser goes 'ping!'.
It depends on which operating system you want the code to work. Afaik there is no generic cross-platform solution for playing sound from go:
On Linux you might need to rely on Pulse Audio with a package such as github.com/mesilliac/pulse-simple
On Windows and Mac you could use PortAudio with a package such as github.com/gordonklaus/portaudio
If you want a practical example there is a go-based multi-source music player project called "moggio" at github.com/mjibson/moggio that plays audio from multiple sources on Linux, Mac and Windows.
You can have a look at the github.com/mjibson/moggio/output package. There you will find the code that moggio used to play music on Linux, Windows and Mac.

Remote control audio player (e.g. Groove-Music)

I'd like to write an app for My Band2 that should be able to simply change the audio track when I hit a button on my Band2. I'll use this because my car can't Play Audio via Bluetooth. So I bought a Bluetooth dongle but I'm unable to change tracks or simply to pause it. So a 3 button app on my Band2 should do a good job.
I've found a lot of examples to do so with Background Audio Players in a self written app.
But I'd like to remote Control the standard player, which is Groove-Music for me.
Any suggestions to do so? Can you please give me a start on that?
Thanks,
Jo

ALSA, avoiding capture of sound before app starts

I'm working on an app that captures audio using the ALSA API.
A tester of the app noticed the following behavior. If a sound is playing
just before the app starts, and then the app is started, that sound
is captured when snd_pcm_readi() is called. Any sound playing within
about a second before starting the app is captured in this way.
Is this behavior expected?
What I want the app to do is capture only the sound that occurs after the
application starts. How can I assure that happens (snd_pcm_drop(), maybe?).

Background Audio and Remote Control Support using MPMusicPlayerController on iOS 4. Is this even possible?

I've spent two days on this and have gotten nowhere. I'm trying to use [MPMusicPlayerController applicationMusicPlayer] to play audio chosen from the user's iPod library and have it run in the background as well as support remote events. Now getting the music actually playing is the easy part. Get the instance, pick the songs, assign the music queue and play. Done and done. BUT... a) I can't get it to play in the background, and b) even when in the foreground I can't get the remote control events to work at all!
And before you ask, yes, I have set the plist entries, the audio session category, the call to say I'm interested in getting remote events and set up a first responder to listen for them, so please know, yes, I've read read every single document on the subject that I could find* (*a task I blame Apple for for not being clear at all on this topic, nor having ANY example code for it!) and I've watched every one of the WWDC videos relating to it (even freezing the screen to copy the code exactly from their example...) so unless I've missed something not in this list, replying with any of those answers is not going to help.
One more thing... I am explicitly talking about using the MPMusicPlayerController which according to the docs, never uses an application session. It always uses the system session. (Maybe that in itself answers my question, but the docs don't clearly say that so I'm not sure, hence this question.)
That said, after two days, my thoughts are this:
When using the MPMusicPlayerController, regardless of what methods you call or what plist entries you set, your app will never run in the background. Period. If you use the ipodMusicPlayer instance, the music keeps playing, but that's because it's the iPod that's playing, not your app. If you use the applicationMusicPlayer instance instead, when going to the background your music stops. In both cases, your app is suspended.
Regardless of your using the ipodMusicPlayer or applicationMusicPlayer instances, all remote events go to the iPod application itself, not yours, even if you've explicitly asked for them. If you are using the applicationMusicPlayer instance and you use the remote to select 'Play', the iPod app receives the command so your audio ducks out and is interrupted and playback begins in the iPod app. If you've chosen the ipodMusicPlayer instead, then of course it doesn't matter as you have explicitly said you're basically just interested in remotely controlling the iPod app which again, is what actually receives the remote events.
The icon in the quick-switch controls at the bottom never changes to your app's icon because again, your app is never actually set up to receive the events. The iPod application is, which is why its icon does appear there.
So what I want to know is... am I wrong here? Has anyone successfully been able to use MPMusicPlayerController and been able to intercept the remote events? While I'd prefer to use the applicationMusicPlayer with background music support so I don't muck with the user's iPod, the bigger thing is remote control notifications, meaning if I have to use the ipodMusicControl and keep my app in the foreground to intercept those messages, so be it. It's ugly that way, but at least it's something.
Code examples, or at least explicit steps against one of the built-in app templates would be GREATLY appreciated. (Don't even need the implementation... just the steps. Hopefully that will appease the inevitable 'It's still under NDA' thing that people keep answering questions with.)
Mark
I solved it. The info is in my other question over here...
Stack Overflow: Play iPod music while receiving remote control events
...but the short version is you have to use AVPlayer (but not AVAudioPlayer. No idea why that is!) with the asset URL from the MPMediaItem you got from the library, then set the audio session's category to Playable (do NOT enable mixable!) and add the appropriate keys to your info.plist file telling the OS your app wants to support background audio.
This lets you play items from your iPod library (except Audible.com files for some reason!) and still get remote events. Granted you have to do more work, and since this is your audio player which is separate from, and will interrupt the iPod app (which may or may not be desirable. And again, don't enable mixing or the iPod app will hijack the remote control events) but those are the breaks!
For anyone who wants to know, I found out to get the audio playing in the background, you have to set the audio session's category to Playable and then background audio works just fine. If you also want to play your own sounds at the same time, you have to mark the category as mixable. That solved the background music part. But what I've found out is any time the iPod is playing, it doesn't seem possible for you to get remote notifications.
Here's the updated thread...
How can you play music from the iPod app while still receiving remote control events in your app?
M

Resources