Giter Site home page Giter Site logo

Comments (9)

fbrozovic avatar fbrozovic commented on July 17, 2024 1

Sorry, I realized I could have been clearer with explaining how I'm trying to do this.

This is my attempt now, but I'm still getting the same error... I'm not really familiar with Cgo, so any help would be much appreciated.

...

infos, err := ctx.Devices(malgo.Duplex)
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

fmt.Println("Duplex devices")
for i, info := range infos {
	fmt.Printf("%d: %v, %s\n", i, info.ID, info.Name())
}
fmt.Println()

devConf1 := malgo.DeviceConfig {
	DeviceType: malgo.Duplex,
	SampleRate: 44100,
	Capture: malgo.SubConfig{
		DeviceID: &infos[0].ID,
		Format: malgo.FormatS16,
		Channels: 1,
	},
	Playback: malgo.SubConfig{
		DeviceID: &infos[0].ID,
		Format: malgo.FormatS16,
		Channels: 1,
	},
}

...

dev1, err := malgo.InitDevice(ctx.Context, devConf1, devCallbacks1)
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

...

from malgo.

gen2brain avatar gen2brain commented on July 17, 2024

Passing ID to what? You should not use DefaultDeviceConfig but create custom DeviceConfig instead, which should contain ID from context.Devices you want to use. I had some examples where I needed that, but cannot find it, though, it should not be difficult.

from malgo.

gen2brain avatar gen2brain commented on July 17, 2024

Ok, need to check and test, API was different.

from malgo.

fbrozovic avatar fbrozovic commented on July 17, 2024

Have you had a chance to look at this yet?

from malgo.

gen2brain avatar gen2brain commented on July 17, 2024

No, didn't find time to check.

from malgo.

i0x71 avatar i0x71 commented on July 17, 2024

I am having the same issue, will post here if i can get it to work.

from malgo.

fbrozovic avatar fbrozovic commented on July 17, 2024

So I managed to get it to work by doing the following:

diff --git a/device_config.go b/device_config.go
index 3392191..f28010d 100644
--- a/device_config.go
+++ b/device_config.go
@@ -37,7 +37,7 @@ func (d *DeviceConfig) cptr() *C.ma_device_config {
 
 // SubConfig type.
 type SubConfig struct {
-       DeviceID   *DeviceID
+       DeviceID   unsafe.Pointer
        Format     FormatType
        Channels   uint32
        ChannelMap [C.MA_MAX_CHANNELS]uint8
diff --git a/device_info.go b/device_info.go
index dc7eb59..1e07d10 100644
--- a/device_info.go
+++ b/device_info.go
@@ -21,8 +21,8 @@ func (d DeviceID) String() string {
        return hex.EncodeToString(d[:displayLen])
 }
 
-func (d *DeviceID) cptr() *C.ma_device_id {
-       return (*C.ma_device_id)(unsafe.Pointer(d))
+func (d *DeviceID) Cptr() unsafe.Pointer {
+       return C.CBytes(d[:])
 }
 
 // DeviceInfo type.

and setting the DeviceConfig struct as follows:

devConf1 := malgo.DeviceConfig {
	DeviceType: malgo.Duplex,
	SampleRate: 44100,
	Capture: malgo.SubConfig{
		DeviceID: infos[0].ID.Cptr(),
		Format: malgo.FormatS16,
		Channels: 1,
	},
	Playback: malgo.SubConfig{
		DeviceID: infos[0].ID.Cptr(),
		Format: malgo.FormatS16,
		Channels: 1,
	},
}

Let me know if this is an OK solution for you - I will create a PR with the changes if it is.

from malgo.

fbrozovic avatar fbrozovic commented on July 17, 2024

Just realized that the pointer returned by C.CBytes() needs to be freed, but unfortunately the device config is not accessible from Uninit() as far as I can see...

Though I guess that since with the above solution, the user is calling Cptr() on the device ID, they should also be responsible for calling C.free on it. It's a bit ugly though, because of the

// #include <stdlib.h>
import "C"

that's needed in the user application.

from malgo.

gen2brain avatar gen2brain commented on July 17, 2024

It should be fixed in 1b98196 , just pass DeviceID: infos[0].ID.Pointer() or whatever index of device.

@fbrozovic No need to free anything in this case, it is not allocated from Go, whatever you allocate in C from Go should be freed.

from malgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.