Problem using ALSA through Go code

I am trying out some code to play a mp3 file in Go, but it is not working. I am pretty sure this is not a problem with the code or Go, but rather something with my machine.

The error message I get is:

ALSA lib pcm_dmix.c:1090:(snd_pcm_dmix_open) unable to open slave
2020/10/19 11:40:34 oto: ALSA error: No such file or directory

After googling for a while, I think I need a package called libasound2-dev. On this page, fatal error: alsa/asoundlib.h: No such file or directory · Issue #37 · hajimehoshi/go-mp3 · GitHub, I found a recommendation to install libasound2-dev, but I think that is the debian/ubuntu name, correct?

sudo apt-get install libasound2-dev

What would be the arch/manjaro name for that package?

Did you notice that the error in the linked github issue has nothing do do with your problem? Even the error message is different.

However the files of the mentioned dev package are part of the alsa-lib package. Please compare the files of both packages.

You are right, the error mesage is different. I focused a little bit too much on the No such file or directory part and the alsa part.

Comparing the files in alsa-lib and libasound2-dev doesn’t help me since I don’t know what I am looking at :slight_smile:

So I am back to square one then, the sound on my machine works perfectly otherwise, including playing mp3 using Clementine. And the sample code comes from the same GitHub page, so that probably works.

That error code probably means that the device you want to open does not exist. If you run aplay -D invalidname, you’ll get same “No such file or directory” error. If you were indeed missing the header files or the shared library, then there would be compilation or linker errors, respectively.

The file exists, I have checked it multiple times. It opens ok, when I debug the code, it is when it tries to play the file, that the error occurs.

Anyway, I thought that it was something I needed that was related to Manjaro, but maybe not. I will do some more debugging and then ask in the proper forum (github, stackoverflow, etc)…

So please edit & rename your topic title :+1: (before we do it :wink: )

You mean alsa device?

Ah, sorry, I misunderstood you, the actual MP3 file exists. If the ALSA device exists or not, I do not know.

If I run aplay -l, it returns :

**** List of PLAYBACK Hardware Devices ****
card 0: HDMI [HDA ATI HDMI], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA ATI HDMI], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA ATI HDMI], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA ATI HDMI], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA ATI HDMI], device 10: HDMI 4 [HDMI 4]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA ATI HDMI], device 11: HDMI 5 [HDMI 5]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Generic [HD-Audio Generic], device 0: ALC887-VD Analog [ALC887-VD Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Generic [HD-Audio Generic], device 1: ALC887-VD Digital [ALC887-VD Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

But again, I am not trying to open an ALSA device, I am just trying to play a MP3 file. If the code is trying to open a device behind the scene, that is another story. But I do not want to turn this thread into a programming thread so I will seek help elsewhere, or give up on this idea of mine all together.

Well, it’d certainly be helpful if you could provide (parts of) the source code. I’m currently checking out the go-mp3 code.

Sure, I can do that. But, as I said, I do not want to change this into a thread about programming. I thought I was missing some Manjaro package, but I was wrong.

The code below opens the file ok, but fails on oto.NewContext().

package main

import (
	"fmt"
	"github.com/hajimehoshi/go-mp3"
	"github.com/hajimehoshi/oto"
	"io"
	"log"
	"os"
)
func run() error {
	f, err := os.Open("/home/per/code/softmusic/assets/2cellos.mp3")
	if err != nil {
		return err
	}
	defer f.Close()

	d, err := mp3.NewDecoder(f)
	if err != nil {
		return err
	}

	c, err := oto.NewContext(d.SampleRate(), 2, 2, 8192)
	if err != nil {
		return err
	}
	defer c.Close()

	p := c.NewPlayer()
	defer p.Close()

	fmt.Printf("Length: %d[bytes]\n", d.Length())

	if _, err := io.Copy(p, d); err != nil {
		return err
	}
	return nil
}

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

I wasn’t sure what you wanted me to change the title to, so I changed it to “I do not need libasound2-dev anymore…” :slight_smile:

Feel free to change it to something better…

Seems like oto cannot open the “default” device. What’s the output of aplay -L? Do you have custom ALSA configuration? Does aplay -D default somefile.wav work? Do you run the program in some kind of containerized environment?

Do I have a custom ALSA configuration? I seriously doubt it, I don’t know anything about sound, so I basically have the default Manjaro Cinnamon setup.

I tried aplay -D default /usr/share/sounds/purple/alert.wav and that worked fine, played a sound and output:ed:

Playing WAVE '/usr/share/sounds/purple/alert.wav' : Signed 16 bit Little Endian, Rate 22050 Hz, Mono

The output of aplay -L, which does contain a “default” device:

null
    Discard all samples (playback) or generate zero samples (capture)
samplerate
    Rate Converter Plugin Using Samplerate Library
speexrate
    Rate Converter Plugin Using Speex Resampler
jack
    JACK Audio Connection Kit
oss
    Open Sound System
pipewire
    PipeWire Sound Server
pulse
    PulseAudio Sound Server
speex
    Plugin using Speex DSP (resample, agc, denoise, echo, dereverb)
upmix
    Plugin for channel upmix (4,6,8)
vdownmix
    Plugin for channel downmix (stereo) with a simple spacialization
default
    Default ALSA Output (currently PulseAudio Sound Server)
hdmi:CARD=HDMI,DEV=0
    HDA ATI HDMI, HDMI 0
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=1
    HDA ATI HDMI, HDMI 1
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=2
    HDA ATI HDMI, HDMI 2
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=3
    HDA ATI HDMI, HDMI 3
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=4
    HDA ATI HDMI, HDMI 4
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=5
    HDA ATI HDMI, HDMI 5
    HDMI Audio Output
usbstream:CARD=HDMI
    HDA ATI HDMI
    USB Stream Output
sysdefault:CARD=Generic
    HD-Audio Generic, ALC887-VD Analog
    Default Audio Device
front:CARD=Generic,DEV=0
    HD-Audio Generic, ALC887-VD Analog
    Front output / input
surround21:CARD=Generic,DEV=0
    HD-Audio Generic, ALC887-VD Analog
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Generic,DEV=0
    HD-Audio Generic, ALC887-VD Analog
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Generic,DEV=0
    HD-Audio Generic, ALC887-VD Analog
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Generic,DEV=0
    HD-Audio Generic, ALC887-VD Analog
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Generic,DEV=0
    HD-Audio Generic, ALC887-VD Analog
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Generic,DEV=0
    HD-Audio Generic, ALC887-VD Analog
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Generic,DEV=0
    HD-Audio Generic, ALC887-VD Digital
    IEC958 (S/PDIF) Digital Audio Output
usbstream:CARD=Generic
    HD-Audio Generic
    USB Stream Output
usbstream:CARD=BRIO
    Logitech BRIO
    USB Stream Output

FYI: It seems to be a bug in go-mp3, the developer just added the bug tag to my question there.

The solution was found in this link : Linux: ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open) unable to open slave - DEV

I had to create a file, called default.conf, in /etc/modprobe.d/ containing the following:

options snd_hda_intel index=1

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.