malgo icon indicating copy to clipboard operation
malgo copied to clipboard

Convert all Malgo structs to Go representations

Open Keithcat1 opened this issue 4 years ago • 12 comments

This changes the ContextConfig, DeviceConfig and DeviceInfo structs to use Go types which are easier to use, require less integer conversion, safer, and we use CGO to figure out struct field offsets by manually converting structs so everything will work properly when Miniaudio updates. This is a breaking change because the types and names of many fields of the before mentioned structs have changed. Everything else should still be the same. Also move all the callback stuff from miniaudio.c and malgo.h into callbacks.c and callbacks.h to make things tidier.

Keithcat1 avatar Sep 16 '21 18:09 Keithcat1

I am getting segfault with playback example

[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4ab9ca]

goroutine 1 [running]:
github.com/gen2brain/malgo.goLogCallback(0x300000002, 0xc0000001a0, 0x539188)
	github.com/gen2brain/malgo/context.go:168 +0x6a
github.com/gen2brain/malgo._Cfunc_ma_context_init(0x0, 0x0, 0xc000430000, 0x11a8820)
	_cgo_gotypes.go:1416 +0x4c
github.com/gen2brain/malgo.InitContext.func2(0x0, 0x519c48, 0x0, 0x0)
	github.com/gen2brain/malgo/context.go:205 +0x8f
github.com/gen2brain/malgo.InitContext({0x0, 0x0, 0xc00009a180}, {0x0, 0x0, {0x0}, {{0x0, 0x0}, {0x0, 0x0}, ...}, ...}, ...)
	github.com/gen2brain/malgo/context.go:205 +0x237
main.main()
	_/home/milann/Projects/malgo/_examples/playback/playback.go:61 +0x466

gen2brain avatar Sep 18 '21 23:09 gen2brain

Okay, should be fixed now. Let me know if it comes back.

Keithcat1 avatar Sep 21 '21 17:09 Keithcat1

Now there are no errors but there is also no sound, I am trying just playback example to play some mp3. Thank you for the work so far, looks great, but everything should work as before.

gen2brain avatar Sep 26 '21 12:09 gen2brain

I think that the playback example is broken. Basically the libraries we're using provide unsigned 16-bit samples but the closest thing Miniaudio supports is signed 16-bit samples. Eventually I'll do a pull request and add bindings to use the ma_decoder API which is built-in to Miniaudio and is also easy to use. Here's a file that plays fine on my computer, please try it and let me know what happens: ball.zip Also try the capture example.

Keithcat1 avatar Sep 27 '21 17:09 Keithcat1

Sorry, I didn't have time to check all this, probably for next weekend, really busy with daily jobs.

gen2brain avatar Oct 11 '21 17:10 gen2brain

Are you still alive :)

Keithcat1 avatar Nov 09 '21 19:11 Keithcat1

Interested to keep this pr alive 👍

not-matt avatar Jan 06 '22 11:01 not-matt

@Keithcat1 I find some time to test with your file. Again, there is no sound, capture example also does nothing. As I probably mentioned, I would prefer if you can add fixes first (struct changes, errors etc.) and then later introduce something breaking. This PR has many changes and it is not easy to follow.

gen2brain avatar Jan 17 '22 11:01 gen2brain

This PR does not have many changes. Nowhere near as many as that first big one, anyway. It changes the layout of the ContextConfig, DeviceConfig and DeviceInfo structs and adds code to convert them between C and Go representations. Also moves some code around. I just tested the capture example on my computer and it works fine. This PR is literally focused on removing as many things that could break Miniaudio as possible, so it shouldn't be the problem. Did you change your audio drivers or something? Try running the capture example with the ma_debug build tag and post the text it prints.

Keithcat1 avatar Jan 19 '22 19:01 Keithcat1

Ah, sorry, I was referring to your first big one. Nothing changed in drivers, I use plain alsa, when I apply your changes it just hangs.

$ ./capture 
: Failed to initialize WASAPI backend.
: Failed to initialize DirectSound backend.
: Failed to initialize WinMM backend.
: Failed to initialize Core Audio backend.
: Failed to initialize sndio backend.
: Failed to initialize audio(4) backend.
: Failed to initialize OSS backend.
: Attempting to initialize PulseAudio backend...
ERROR: [PulseAudio] Failed to connect PulseAudio context.


^C

And with pulseaudio:

$ ./capture 
: Failed to initialize WASAPI backend.
: Failed to initialize DirectSound backend.
: Failed to initialize WinMM backend.
: Failed to initialize Core Audio backend.
: Failed to initialize sndio backend.
: Failed to initialize audio(4) backend.
: Failed to initialize OSS backend.
: Attempting to initialize PulseAudio backend...
[miniaudio] Endian:  LE
[miniaudio] SSE2:    YES
[miniaudio] AVX2:    NO
[miniaudio] AVX512F: NO
[miniaudio] NEON:    NO
Recording...
[PulseAudio] Capture attr: maxlength=2880, tlength=960, prebuf=-1, minreq=-1, fragsize=960; periodSizeInFrames=480
[PulseAudio] Capture actual attr: maxlength=2880, tlength=960, prebuf=-1, minreq=-1, fragsize=960; periodSizeInFrames=480
[PulseAudio] Playback attr: maxlength=5292, tlength=1764, prebuf=-1, minreq=-1, fragsize=1764; periodSizeInFrames=441
[PulseAudio] Playback actual attr: maxlength=5292, tlength=1324, prebuf=888, minreq=440, fragsize=1764; internalPeriodSizeInFrames=0
ERROR: [PulseAudio] Failed to initialize ring buffer.
LOG <[PulseAudio] Failed to initialize ring buffer.>
miniaudio: invalid args

And when I reverse the changes (current master):

$ ./capture 
: Failed to initialize WASAPI backend.
: Failed to initialize DirectSound backend.
: Failed to initialize WinMM backend.
: Failed to initialize Core Audio backend.
: Failed to initialize sndio backend.
: Failed to initialize audio(4) backend.
: Failed to initialize OSS backend.
: Attempting to initialize PulseAudio backend...
ERROR: [PulseAudio] Failed to connect PulseAudio context.
LOG <[PulseAudio] Failed to connect PulseAudio context.>
: Failed to initialize PulseAudio backend.
: Attempting to initialize ALSA backend...
[miniaudio] Endian:  LE
[miniaudio] SSE2:    YES
[miniaudio] AVX2:    NO
[miniaudio] AVX512F: NO
[miniaudio] NEON:    NO
Recording...
INFO: [ALSA]
INFO:   Default Capture Device (Capture)
INFO:     Format:      16-bit Signed Integer -> 16-bit Signed Integer
INFO:     Channels:    1 -> 1
INFO:     Sample Rate: 48000 -> 44100
INFO:     Buffer Size: 1024*3 (3072)
INFO:     Conversion:
INFO:       Pre Format Conversion:    NO
INFO:       Post Format Conversion:   NO
INFO:       Channel Routing:          NO
INFO:       Resampling:               YES
INFO:       Passthrough:              NO
INFO:   Default Playback Device (Playback)
INFO:     Format:      16-bit Signed Integer -> 16-bit Signed Integer
INFO:     Channels:    1 -> 1
INFO:     Sample Rate: 44100 -> 48000
INFO:     Buffer Size: 1024*3 (3072)
INFO:     Conversion:
INFO:       Pre Format Conversion:    NO
INFO:       Post Format Conversion:   NO
INFO:       Channel Routing:          NO
GINFO:       Resampling:               YES
INFO:       Passthrough:              NO
Press Enter to stop recording...

[ALSA] Waking up... Done
[ALSA] POLLIN set for wakeupfd
[ALSA] Dropping capture device... Done
[ALSA] Preparing capture device... Done
[ALSA] Dropping playback device... Done
[ALSA] Preparing playback device... Done
Playing...
INFO: [ALSA]
INFO:   Default Capture Device (Capture)
INFO:     Format:      16-bit Signed Integer -> 16-bit Signed Integer
INFO:     Channels:    1 -> 1
INFO:     Sample Rate: 48000 -> 44100
INFO:     Buffer Size: 1024*3 (3072)
INFO:     Conversion:
INFO:       Pre Format Conversion:    NO
INFO:       Post Format Conversion:   NO
INFO:       Channel Routing:          NO
INFO:       Resampling:               YES
INFO:       Passthrough:              NO
INFO:   Default Playback Device (Playback)
INFO:     Format:      16-bit Signed Integer -> 16-bit Signed Integer
INFO:     Channels:    1 -> 1
INFO:     Sample Rate: 44100 -> 48000
INFO:     Buffer Size: 1024*3 (3072)
INFO:     Conversion:
INFO:       Pre Format Conversion:    NO
INFO:       Post Format Conversion:   NO
INFO:       Channel Routing:          NO
INFO:       Resampling:               YES
INFO:       Passthrough:              NO
Press Enter to quit...

[ALSA] Waking up... Done
[ALSA] POLLIN set for wakeupfd
[ALSA] Dropping capture device... Done
[ALSA] Preparing capture device... Done
[ALSA] Dropping playback device... Done
[ALSA] Preparing playback device... Done

Btw. I am interested in your fixes from the first big one, when you find the time please send them. Thanks!

gen2brain avatar Jan 19 '22 20:01 gen2brain

I realize that https://github.com/gen2brain/malgo/pull/39, if it goes in, ends up doing some of the initial conversions to more idiomatic go use, but not nearly as good as this PR.

edaniels avatar Aug 14 '22 03:08 edaniels

Apparently, this PR stops Miniaudio from working somehow, which is weird because it worked fine on my end. I don't know what the problem is.

Keithcat1 avatar Aug 19 '22 18:08 Keithcat1