Audio sample for torizon, vs code extension

Hey there :slight_smile:

just started out with torizon and toradex got stuck with the audio example How to play audio on Torizon OS using Alsa and C/C++ | Toradex Developer Center.

I can’t find any section in my torizon vs code extension that match either “extrapackages”, “custom properties”, “devices”. “devpackages” etc.

I only have: “Network Devices” and “Connected Devices” as sections in the vs-code extension tab, that’s it.

Did I configure sth wrong?

best regards
Ludwig

Greetings @Ludwig,

That article was written for an older version of our VSCode extension where things were much different. It has not been updated yet for the current version of our extension.

If you want to add packages to your project you should look at this article here: Add Packages, Libraries and Tools | Toradex Developer Center

For other configurations you’ll want to modify your project’s docker-compose.yml file to add things like external devices and such.

Best Regards,
Jeremias

1 Like

This also confused me!
There is no automatic detection (at least not on my PC).
I used PUTTY
image
Connect via ssh to your board.
Use “ip a” to find your IP


Than you have to manually add it via
image

If you wannt to debug , or flash your app to your board, you have to set “Set Default”

1 Like

thank you Jeremias! I followed your advice!

I added every package necessary to the torizonPackages.json, adjusted the docker-compose to add access to /dev/snd but it still doesn’t work aka I don’t hear anything through my headphones. When running the sample application, the logs say:

amixer: Unable to find simple control 'Headphone',0

So I followed this article here: Audio (Linux) | Toradex Developer Center but something strange happens: my /proc/asound directory is empty. I am not that familiar with low level linux in combination with docker, so I may have overlooked sth but shouldn’t it at least tell me what my output options are? Or did I forget /misunderstand sth entirely?

I am on an apalis imx8 with an Ixora carrier board.
Best regards and thank you again!

but something strange happens: my /proc/asound directory is empty

Where are you looking at /proc/asound? If you look at this inside the container then it would be empty, but if you look at it on the host outside of the container then it should have contents, which is what matters. What version of the OS are you running?

I did a quick test with this myself and was able to get it working. Here’s what I did:

  • Create new Makefile based project with our extension
  • Copy source code from from sample to main.cpp: https://raw.githubusercontent.com/toradex/torizon-samples/bookworm/alsa/alsa-example.cpp
    • Change the value of SoundCardPortName to static const char SoundCardPortName[] = "hw:2,0"; This is because on Apalis i.MX8 the actual sound card is this device not the first/default one.
    • Change the amixer line to this: system("amixer -c 2 set Headphone unmuted 50%"); Again we do this to point to the actual sound card device.
  • Update torizonPackages.json in my project to look like this:
{
    "deps": [
        "alsa-utils",
        "libasound2"
    ],
    "devDeps": [
        "libasound2-dev",
        "alsa-utils"
    ]
}
  • Update Makefile to link libasound library
  • Download your audio Wav file of choice to your project folder. I put it in ./src so it’s next to main.cpp
  • Update Dockerfile.debug to copy your Wav file into your application container. I just added a line like this for example (this copies the Wav file into the same directory as the compiled application):
COPY --chown=$SSHUSERNAME:$SSHUSERNAME ./src/StarWars60.wav ${APP_ROOT}
  • Finally update launch.json so that the debug task for Torizon ARMv8 has filename of your Wav file in the args property. Like this for example:
"args": ["StarWars60.wav"],

Once I did all that I started the debug process and was able to hear my Wav file playing the respective audio on the headphones I connected to the Ixora board.

Best Regards,
Jeremias

Thank you for the fast reply!

Yes, you are right: from outside the container /proc/asound is accessible, whereas from inside the container it is not.

I am using Torizon core 5.7.2+build.20.

I followed all your steps and also had to adjust docker-compose.yml by adding

   devices:
      - "/dev/snd"

(otherwise it would complain “Can’t open audio hw:2,0: No such file or directory”).

I also used LDFLAGS := -L/lib/aarch64-linux-gnu/ -lasound to link libasound, which seemed to work alright.

However, the program terminated unsuccessful with “Can’t set sound parameters: Invalid argument”, line 312 in the provided code. I used the provided wav file from Ryan Andersen and the program has access to it (I checked). Since you probably have a very similiar configuration which doesn’t fail, I suspect that its still some configuration issue - do you have an idea what step I could have missed?

Thanks again a lot!
Ludwig

I am working on this as well, I haven’t gotten to where you are yet, but I’ve noticed when I downloaded the music file, it is mp3 file. Did you have a chance to convert it to wav file?

@Ludwig I did some further testing and I believe I know what the issue is. It seems what card number the audio codec is, is not consistent across boots. On one boot I observed the card number for the proper audio codec is card 2:

torizon@apalis-imx8-06738453:~$ cat /proc/asound/cards
 0 [imxspdif       ]: imx-spdif - imx-spdif
                      imx-spdif
 1 [imxaudiohdmitx ]: imx-audio-hdmi- - imx-audio-hdmi-tx
                      imx-audio-hdmi-tx
 2 [apalisimx8qmsgt]: simple-card - apalis-imx8qm-sgtl5000
                      apalis-imx8qm-sgtl5000

Which is what I based my code changes on. However, on another boot the same audio codec is now card number 0:

torizon@apalis-imx8-06738453:~$ cat /proc/asound/cards
 0 [apalisimx8qmsgt]: simple-card - apalis-imx8qm-sgtl5000
                      apalis-imx8qm-sgtl5000
 1 [imxaudiohdmitx ]: imx-audio-hdmi- - imx-audio-hdmi-tx
                      imx-audio-hdmi-tx
 2 [imxspdif       ]: imx-spdif - imx-spdif
                      imx-spdif

In the case where the audio codec enumerated itself as card 0, I got the same error as you since my code was expecting it as card 2.

The proper way to handle this would be to use the audio codec name instead of the card number as this should be consistent across boots. To do this let’s use aplay like so:

root@9ddc62c7d762:/# aplay -L
null
    Discard all samples (playback) or generate zero samples (capture)
hw:CARD=apalisimx8qmsgt,DEV=0
    apalis-imx8qm-sgtl5000, 59050000.sai-sgtl5000 sgtl5000-0
    Direct hardware device without any conversions
plughw:CARD=apalisimx8qmsgt,DEV=0
    apalis-imx8qm-sgtl5000, 59050000.sai-sgtl5000 sgtl5000-0
    Hardware device with all software conversions
default:CARD=apalisimx8qmsgt
    apalis-imx8qm-sgtl5000, 59050000.sai-sgtl5000 sgtl5000-0
    Default Audio Device
sysdefault:CARD=apalisimx8qmsgt
    apalis-imx8qm-sgtl5000, 59050000.sai-sgtl5000 sgtl5000-0
    Default Audio Device
dmix:CARD=apalisimx8qmsgt,DEV=0
    apalis-imx8qm-sgtl5000, 59050000.sai-sgtl5000 sgtl5000-0
    Direct sample mixing device
hw:CARD=imxaudiohdmitx,DEV=0
    imx-audio-hdmi-tx, i.MX HDMI i2s-hifi-0
    Direct hardware device without any conversions
plughw:CARD=imxaudiohdmitx,DEV=0
    imx-audio-hdmi-tx, i.MX HDMI i2s-hifi-0
    Hardware device with all software conversions
default:CARD=imxaudiohdmitx
    imx-audio-hdmi-tx, i.MX HDMI i2s-hifi-0
    Default Audio Device
sysdefault:CARD=imxaudiohdmitx
    imx-audio-hdmi-tx, i.MX HDMI i2s-hifi-0
    Default Audio Device
dmix:CARD=imxaudiohdmitx,DEV=0
    imx-audio-hdmi-tx, i.MX HDMI i2s-hifi-0
    Direct sample mixing device
hw:CARD=imxspdif,DEV=0
    imx-spdif, S/PDIF PCM snd-soc-dummy-dai-0
    Direct hardware device without any conversions
plughw:CARD=imxspdif,DEV=0
    imx-spdif, S/PDIF PCM snd-soc-dummy-dai-0
    Hardware device with all software conversions
default:CARD=imxspdif
    imx-spdif, S/PDIF PCM snd-soc-dummy-dai-0
    Default Audio Device
sysdefault:CARD=imxspdif
    imx-spdif, S/PDIF PCM snd-soc-dummy-dai-0
    Default Audio Device
dmix:CARD=imxspdif,DEV=0
    imx-spdif, S/PDIF PCM snd-soc-dummy-dai-0
    Direct sample mixing device

Here we see the name for the device is default:CARD=apalisimx8qmsgt. Use this name in the amixer command and in the SoundCardPortName[] variable instead of the card number as we were before. After doing this change audio plays for me no matter which card number the audio codec enumerates with.

So now the code changes should be something like this:

static const char		SoundCardPortName[] = "default:CARD=apalisimx8qmsgt";
...
system("amixer -D default:CARD=apalisimx8qmsgt set Headphone unmuted 50%");

Best Regards,
Jeremias

1 Like

@srzm,

Are you talking about the audio file in our samples repo? That should be a wav file. Though in any case you can just use any wav audio file there’s plenty of sample files online.

Best Regards,
Jeremias

Hello @jeremias.tx

Where did you see the audio file? I got the file from the link that was provided (How to play audio on Torizon OS using Alsa and C/C++ | Toradex Developer Center), and it was mp3, which I had to convert using VLC.

Regardless, you’re right, it can be any wav file, I was wondering if he converted but it appears that hopefully you’ve provided a solution (which I’m happy to see, the path is getting clearer for me!)

Hey there :slight_smile:

worked out perfectly, thanks a lot!

best regards
Ludwig

if you follow the link to github, torizon-samples/alsa at bookworm · toradex/torizon-samples · GitHub, its one of the files (not the link in the readme) :wink:

1 Like

@Ludwig, Glad I was able to help!

Hello @Ludwig and @jeremias.tx

Using both of your steps, I was able to finally play the audio.

@jeremias.tx - I wouldn’t have known to add a library in Makefile - it was not mentioned here: Add Packages, Libraries and Tools | Toradex Developer Center

Is it mentioned anywhere so I can read up on it?

Thanks,
Sara

Is it mentioned anywhere so I can read up on it?

I’m not sure if I understand, linking your libraries whether in the Makefile or elsewhere is common procedure when compiling C/C++ applications. This isn’t something that is unique to Toradex.

Best Regards,
Jeremias

I misunderstood - thank you for clarifying this.

Glad to help clears things up.