bls
Posts: 449
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: RPi4 : How to record desktop audio ?

Sat Feb 15, 2020 4:31 pm

Yes, you said that you didn't want to use pulseaudio. I went down that path first because it seemed slightly easier than sussing out the oddities of alsa, especially since there are only 5 beings in the universe who understand it (per @lordrunningclam) :lol:

I did get alsa-only recording working, but thus far, it's jittery. Posting here for others to have a go at, and hopefully de-jitterize. This is largely based on the first response at https://unix.stackexchange.com/question ... undrc-file

First, install the snd_aloop module: sudo modprobe snd_aloop

Create ~/.asoundrc with the contents:

Code: Select all

pcm.!default {
  type asym
  playback.pcm "LoopAndReal"
  #capture.pcm "looprec"
  capture.pcm "hw:0,0"
}

pcm.looprec {
    type hw
    card "Loopback"
    device 1
    subdevice 0
}


pcm.LoopAndReal {
  type plug
  slave.pcm mdev
  route_policy "duplicate"
}


pcm.mdev {
  type multi
  slaves.a.pcm pcm.MixReale
  slaves.a.channels 2
  slaves.b.pcm pcm.MixLoopback
  slaves.b.channels 2
  bindings.0.slave a
  bindings.0.channel 0
  bindings.1.slave a
  bindings.1.channel 1
  bindings.2.slave b
  bindings.2.channel 0
  bindings.3.slave b
  bindings.3.channel 1
}


pcm.MixReale {
  type dmix
  ipc_key 1024
  slave {
    pcm "hw:0,0"
    rate 48000
    #rate 44100
    periods 128
    period_time 0
    period_size 2048 # must be power of 2
    buffer_size 32768
  }
}

pcm.MixLoopback {
  type dmix
  ipc_key 1025
  slave {
    pcm "hw:Loopback,0,0"
    rate 48000
    #rate 44100
    periods 128
    period_time 0
    period_size 2048 # must be power of 2
    buffer_size 32768
  }
}
Then run adacity, and select Loopback: PCM (hw:1,1) for input and start recording your PC sound (e.g., web page, etc.)

Hopefully someone can sort out the jitters.

bls
Posts: 449
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: RPi4 : How to record desktop audio ?

Sat Feb 15, 2020 8:38 pm

Here's another .asoundrc, which seems to eliminate the jitter. Only issue I've found is that it doesn't seem to play the audio being recorded on the speaker (yes, I added one) at the same time it's being recorded. Still need to modprobe snd_aloop. I'm hoping to get simultaneous playback working.

Code: Select all

# 
# default device
pcm.!default {
    type hw
    card 0
}

ctl.!default {
    type hw
    card 0
}
#pcm.!default {
#    type plug
#    slave.pcm "loopout"
#}
#pcm.!default {
#    type plug
#    slave.pcm "loop"
#}

# output device
pcm.loopout {
    type dmix
    ipc_key 328211
    slave.pcm "hw:Loopback,0,0"
}

# input device
pcm.loopin {
    type dsnoop
    ipc_key 686592
    slave.pcm "hw:Loopback,1,0"
}

# duplex plug device
pcm.loop {
    type plug
    slave {
        pcm {
         type asym
         playback.pcm "loopout"
         capture.pcm "loopin"
        }
    }
}


User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Sun Feb 16, 2020 6:19 am

Thanks for your proposal.
I'll give a try ASAP and report here

BR
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Tue Feb 18, 2020 7:47 pm

@bls :
I didn't succeed with Audacity + sudo modprobe snd_aloop + second .asoundrc file.
This gave me the Loopback choices but whatever choice I made, nothing was recorded (flat display on Audacity)
I had the bad idea to try to select dmix in the right hand chooser and this crashed Audacity
After killing it, and even reboot, Audacity doesn't display any choice anymore
It seems I have to restore a backup ... :?

@tvjon :
You solution is easiest than I was thinking. There's no need to reboot to make a change.
The file .asoundrc can be renamed .asoundrc_off to stop recording.
I think I will create a simple GTK interface to change the name "off" or "on" with the mouse.
I haven't found the modification I must do to have a correct speed. I will try to find it.

BR
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

bls
Posts: 449
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: RPi4 : How to record desktop audio ?

Tue Feb 18, 2020 10:16 pm

Pulsar33 wrote:
Tue Feb 18, 2020 7:47 pm
@bls :
I didn't succeed with Audacity + sudo modprobe snd_aloop + second .asoundrc file.
This gave me the Loopback choices but whatever choice I made, nothing was recorded (flat display on Audacity)
I had the bad idea to try to select dmix in the right hand chooser and this crashed Audacity
After killing it, and even reboot, Audacity doesn't display any choice anymore
It seems I have to restore a backup ... :?
Huh, very strange, since I was on a vanilla Raspbian. I haven't given up on getting play while recording to work, and will post that here once I knock that off.

tvjon
Posts: 751
Joined: Mon Jan 07, 2013 9:11 am

Re: RPi4 : How to record desktop audio ?

Wed Feb 19, 2020 12:23 pm

Pulsar33 ... wrote: 1° With a raspbian_full distribution, you need to install the "lame frontend" to get the "lame" command
2° With a raspbian_full distribution, I had no .asoundrc file in ~/ so I simply installed yours and reboot
3° Every time some sound is played through my HDMI 0 port, a mp3 file is created in /tmp with good naming convention, thanks to your rc file
4° This works for Chrome and for VLC as well, and confirms that any sound of the desktop is recorded

On the other hand, some problems remain to be solved if we want a generic and user friendly capability :
a) I had to post process the files with Audacity on my Desktop PC to increase the Speed by a 1.088 ratio (48000/44100)
=> Tempo and Height are too low according to this ratio, but the sampling rate must remain at 44.1 kHz
b) It would be nice to have a start/stop feature without rebooting for this tool (maybe as a service or a script ?)

Anyway, this is a good progress.
Best regards
Pulsar33
Thank you for your detailed & constructive feedback, which is always welcome.

I've been busy with mostly non Pi related stuff over the last few months, so didn't have much time to elaborate but I imagined you'd work out the details & reading points 1 & 2 I see you did.

I agree with your "generic" point. I hope your request for a good Alsa solution from the experts is forthcoming. With each new Raspbian version, I try a loopback approach, but just too many underrun errors are generated, the audio actually often *varies* in speed, culminating in recordings simply quitting.

Interesting point a) about Speed & Tempo too, hmmm...

Point b) - ah, that was why I mentioned omxplayer. I don't change anything, or reboot if I want to listen to a recording. If I use an alternative player, then I do as you're doing, rename the .asoundrc. Your suggestion of a simple toggle to enable or disable it sounds good.

omxplayer can bypass alsa, thus does *NOT* need to create a file whilst playing a recording.
In case you're unfamiliar with omxplayer, just execute it from a terminal, with no arguments, & it'll display a full page of useful info'. It's one of the strengths of Raspberry Pi IMHO.

I will be very interested to see how you get on with the tempo change problem.

bls
Posts: 449
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: RPi4 : How to record desktop audio ?

Thu Feb 20, 2020 12:30 am

Pulsar33 wrote:
Tue Feb 18, 2020 7:47 pm
@bls :
I didn't succeed with Audacity + sudo modprobe snd_aloop + second .asoundrc file.
This gave me the Loopback choices but whatever choice I made, nothing was recorded (flat display on Audacity)
I had the bad idea to try to select dmix in the right hand chooser and this crashed Audacity
After killing it, and even reboot, Audacity doesn't display any choice anymore
It seems I have to restore a backup ... :?

@tvjon :
You solution is easiest than I was thinking. There's no need to reboot to make a change.
The file .asoundrc can be renamed .asoundrc_off to stop recording.
I think I will create a simple GTK interface to change the name "off" or "on" with the mouse.
I haven't found the modification I must do to have a correct speed. I will try to find it.

BR
Pulsar33
What are you using as the source for the sound you want to record? Thx

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Thu Feb 20, 2020 12:45 am

To make tests, I use either Chromium on Youtube or VLC on local mp3 files, but what I'm looking for should not depend on the selected source.
BR
Pulsar33

PS : not sure I've understood your question. If you speak of Audacity, I tried every available source Loopbacks, Loop ...
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

bls
Posts: 449
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: RPi4 : How to record desktop audio ?

Thu Feb 20, 2020 12:54 am

You're correct that the source shouldn't matter. I wanted to check, though, because I found some references on some alsa web pages about some apps not picking up the default device correctly. I did my testing with Youtube on Chromium, and still mystified that it didn't work for you. Hoping to get back to it soon, sort out the play-while-recording question, and test it on more than one system :lol:

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Sat Feb 22, 2020 12:36 am

Hello

@tvjon : the option -s 48 in the lame command line create a 48kHz mp3 for Chromium with the good speed and heigth.
Unfortunately, it doesn't work for VLC and we get only noise :? I don't know why and if other software have the same problem at this time.

However, I've created a simple GTK GUI to use easily your solution. You will find it attached. You need to do the following
- install (if not already done) libgtk-3-dev
- unzip Asar-v1.0.zip and type make asar
- copy .asoundrc_on in your home
- if you want to keep you .asoundrc file, copy it to .asoundrc_off, else copy the empty file .asoundrc_off from the archive to your home
- create a launcher for asar
Asar-v1.0.png
Asar-v1.0.png (24.43 KiB) Viewed 697 times
Asar stands for Alsa Simple Audio Recorder
Asar-v1.0.zip
(4.59 KiB) Downloaded 14 times
Best Regards
Pulsar33

WARNING => changes are done immediately but the system will use the current configuration only the next time a player will start playing !!!
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Sat Feb 22, 2020 12:52 am

Arg, already seen 1 little bug but without consequence :oops:

Code: Select all

// V1.1   : don't copy .asoundrc_on if not found
BR
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Sun Feb 23, 2020 8:58 am

Hello,

Is there some feedback about Asar-v1.0 before I post v1.1 ?
Does someone know why -s 48 doesn't work for VLC ?

BR
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

tvjon
Posts: 751
Joined: Mon Jan 07, 2013 9:11 am

Re: RPi4 : How to record desktop audio ?

Sun Feb 23, 2020 9:33 am

I'm still rather busy I'm afraid.

I built it last night & it works fine thank you.

I'll try to take a look at vlc, -s 48k later today.

HTH

tvjon
Posts: 751
Joined: Mon Jan 07, 2013 9:11 am

Re: RPi4 : How to record desktop audio ?

Sun Feb 23, 2020 10:13 pm

@Pulsar33, is this the same kind of sound you here?

If not, post a short sample please.

What version of vlc?
Attachments
vlc.mp3.zip
(71.08 KiB) Downloaded 9 times

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Sun Feb 23, 2020 10:59 pm

@tvjon : yes it is
I don't understand why it's different when the source is Chromium or VLC if we record "the desktop" that is "all that is sent to audio out".
Moreover, the problem doesn't exist at 44.1Khz :shock:

VLC version is 3.0.8 Vetinari

Good night
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Mon Feb 24, 2020 7:20 am

Actually, with the -s 48 option, I'm afraid that only recording Chromium works
With Kaffeine and MPV I have the same problem as with VLC.

I must try with System Sounds and other audio sources
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

tvjon
Posts: 751
Joined: Mon Jan 07, 2013 9:11 am

Re: RPi4 : How to record desktop audio ?

Mon Feb 24, 2020 8:44 am

I use mplayer (sudo apt install) & omxplayer (mostly for radio station recording) without problems. For omxplayer I specifically tell it to use alsa, else (as mentioned already) it wonuldn't record.

tvjon
Posts: 751
Joined: Mon Jan 07, 2013 9:11 am

Re: RPi4 : How to record desktop audio ?

Mon Feb 24, 2020 7:13 pm

Pulsar33 wrote:
Mon Feb 24, 2020 7:20 am
...
I must try with System Sounds and other audio sources
Pulsar33

Can you record vlc playing a .wav file, then check the resulting mp3 please?

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Mon Feb 24, 2020 7:56 pm

Hello,

You're right, it works, but ...
1) The speed and height of the recorded mp3 are higher than the original (certainly with a 48/44.1 ratio)
2) Remember : The speed and height of a youtube video played with Chromium are lower than the original with a ratio of 44.1/48
3) This confirm what I thought when you said than it was ok for you with your players

In fact, the problem is not dependent of the software producing audio. It depends on the original data that are played.
So we have a problem because this is unpredictable AND different data can be played at the same time with different frequency parameters.
I don't know what to do ... I can add a Frequency Selector in Asar-v1.1 but this will work only for matching audio source at one time.

BR
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

tvjon
Posts: 751
Joined: Mon Jan 07, 2013 9:11 am

Re: RPi4 : How to record desktop audio ?

Mon Feb 24, 2020 9:49 pm

Pulsar33 wrote:
Mon Feb 24, 2020 7:56 pm
Hello,

...

In fact, the problem is not dependent of the software producing audio. It depends on the original data that are played.

So we have a problem because this is unpredictable AND different data can be played at the same time with different frequency parameters.

I don't know what to do ... I can add a Frequency Selector in Asar-v1.1 but this will work only for matching audio source at one time.

BR
Pulsar33
Exactly.

Likewise, but your fr. selector suggestion is good for the time being I think, as an aid to testing.

As I get time, I'll investigate further, as I imagine you will, & of course, update this thread accordingly.

Bonne nuit.

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Tue Feb 25, 2020 9:24 am

Hello

The job is done ... BUT ... The problem remains
1) The sampling rate can be set to 48 khz or 44.1 khz
This gives too low, correct or too high speed and height according to the setting compliance with the original audio data.
2) Recording Chromium playing youtube videos or VLC playing wav audio is fine providing you set the right sampling rate
3) The Mode is now set to (s)imple stereo trying to avoid complexity of encoding and possible encoding problems
However, recording VLC playing mp3 files produces garbage as already detected.

You will find below the v1.1 version of Asar.
WARNING : .asoundrc_on has changed and MUST be updated in your /home directory.
Asar-v1.1.png
Asar-v1.1.png (42.62 KiB) Viewed 462 times
Asar-v1.1.zip
(4.96 KiB) Downloaded 12 times
(same procedure as before)

Have a nice day
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

tvjon
Posts: 751
Joined: Mon Jan 07, 2013 9:11 am

Re: RPi4 : How to record desktop audio ?

Tue Feb 25, 2020 2:00 pm

Pulsar33 wrote:
Tue Feb 25, 2020 9:24 am
...
Asar-v1.1.zip (same procedure as before)

Have a nice day
Pulsar33
Looks good!

I'm in the field currently, but it occurs to me that we could do with having a "no frequency change" field in your drop down sub menu. I can look at doing that tonight, but realistically it might save some time if you could do such a mod' if you have time, & feel it's acceptable, please?

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Tue Feb 25, 2020 3:13 pm

@tvjon :
Hum, if you can tell me what option to use, I'm sure I could do it quickly, but I've seen no such option in the man page. Or you think about removing the -s XX option if "No change" is selected, in which case it would be the same as your first proposal ?

I tested accidentally the System Sound capture and it worked : this was when I took a screenshot for v1.1 picture above.

In the meantime, I tried to use the --decode option to create a wav file. It does create a wav file but the same problem exists.
No other idea at this time ...
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

tvjon
Posts: 751
Joined: Mon Jan 07, 2013 9:11 am

Re: RPi4 : How to record desktop audio ?

Tue Feb 25, 2020 8:48 pm

Pulsar33 wrote:
Tue Feb 25, 2020 3:13 pm
@tvjon :
...
Or you think about removing the -s XX option if "No change" is selected, in which case it would be the same as your first proposal ?

..
Pulsar33
Sorry, yes. I did mean that, so I suppose a blank field above the frequency selectors. The idea being to be able to rapidly switch amongst the choices.

"I tested accidentally the System Sound capture and it worked : this was when I took a screenshot for v1.1 picture above."

Ok, good to know that.

I have had the mp3 conversion working, but alsa decided it wanted to only do it in mono, so more work still necessary I'm afraid. It would be nice to not have to cater for "special case" applications.

User avatar
Pulsar33
Posts: 174
Joined: Sun Aug 25, 2019 4:06 pm
Location: Bordeaux France

Re: RPi4 : How to record desktop audio ?

Wed Feb 26, 2020 11:54 am

Hello,

New version v1.2

Code: Select all

// V1.2   : initialise the GUI in accordance with the content of .asoundrc_on
// V1.2   : add "No change" Sampling Rate capability

//====================================================================================
// NOTA :                 .asoundrc modification process
//====================================================================================
// There could be other ways to overwrite .asoundrc file and content of .asoundrc_on
// The less simple way I choose is intended to :
// - switch between two user configurations .asoundrc_on and .asoundrc_off
// - use .asoundrc_off as the original user's .asoundrc file and preserve it
// - overwrite only some fields in .asoundrc_on, keeping all other fields unchanged
// - allow the user to define many other audio interfaces in the .asoundrc files
//====================================================================================
Asar-v1.2.png
Asar-v1.2.png (40.05 KiB) Viewed 401 times
Remark and question : The icon of the window differs when launch from the shell (left picture) or the taskbar shortcut (right picture)
However, the taskbar shortcut itself has the good icon ... :shock:
If someone know a cool and better way to include the icon as a resource, it would be a good thing ...
Asar-v1.2.zip
(5.49 KiB) Downloaded 12 times
(Same procedure as above)

Best regards
Pulsar33
Pi 4 with 4Gb memory, DVB TV pHAT ------------------- Desktop : Core i5 Linux MINT 19.3

Return to “Graphics, sound and multimedia”