MXL audio sample player app
This commit is contained in:
+69
-8
@@ -20,19 +20,35 @@ const (
|
||||
|
||||
KeyEscape uint32 = 0x1B
|
||||
KeyF uint32 = 0x66
|
||||
|
||||
InitAudio uint32 = 0x00000010
|
||||
AudioDeviceDefaultPlayback uint32 = 0xFFFFFFFF
|
||||
AudioF32 uint16 = 0x8120
|
||||
)
|
||||
|
||||
var (
|
||||
sdlInit func(flags uint32) bool
|
||||
sdlQuit func()
|
||||
sdlGetError func() uintptr
|
||||
sdlCreateWindow func(title *byte, w, h int32, flags uint64) uintptr
|
||||
sdlDestroyWindow func(window uintptr)
|
||||
sdlInit func(flags uint32) bool
|
||||
sdlQuit func()
|
||||
sdlGetError func() uintptr
|
||||
|
||||
sdlCreateWindow func(title *byte, w, h int32, flags uint64) uintptr
|
||||
sdlDestroyWindow func(window uintptr)
|
||||
|
||||
sdlVulkanGetInstanceExtensions func(count *uint32) uintptr
|
||||
sdlVulkanCreateSurface func(window, instance, allocator uintptr, surface *uint64) bool
|
||||
sdlPollEvent func(event unsafe.Pointer) bool
|
||||
sdlGetWindowSizeInPixels func(window uintptr, w, h *int32) bool
|
||||
sdlSetWindowFullscreen func(window uintptr, fullscreen bool) bool
|
||||
|
||||
sdlPollEvent func(event unsafe.Pointer) bool
|
||||
|
||||
sdlGetWindowSizeInPixels func(window uintptr, w, h *int32) bool
|
||||
sdlSetWindowFullscreen func(window uintptr, fullscreen bool) bool
|
||||
|
||||
sdlOpenAudioDeviceStream func(devid uint32, spec unsafe.Pointer, callback uintptr, userdata uintptr) uintptr
|
||||
sdlResumeAudioStreamDevice func(stream uintptr) bool
|
||||
sdlPutAudioStreamData func(stream uintptr, buf unsafe.Pointer, length int32) bool
|
||||
sdlGetAudioStreamQueued func(stream uintptr) int32
|
||||
sdlDestroyAudioStream func(stream uintptr)
|
||||
sdlGetAudioPlaybackDevices func(count *int32) uintptr
|
||||
sdlGetAudioDeviceName func(devid uint32) uintptr
|
||||
)
|
||||
|
||||
var loaded = false
|
||||
@@ -55,6 +71,14 @@ func Load() error {
|
||||
purego.RegisterLibFunc(&sdlPollEvent, h, "SDL_PollEvent")
|
||||
purego.RegisterLibFunc(&sdlGetWindowSizeInPixels, h, "SDL_GetWindowSizeInPixels")
|
||||
purego.RegisterLibFunc(&sdlSetWindowFullscreen, h, "SDL_SetWindowFullscreen")
|
||||
// audio
|
||||
purego.RegisterLibFunc(&sdlOpenAudioDeviceStream, h, "SDL_OpenAudioDeviceStream")
|
||||
purego.RegisterLibFunc(&sdlResumeAudioStreamDevice, h, "SDL_ResumeAudioStreamDevice")
|
||||
purego.RegisterLibFunc(&sdlPutAudioStreamData, h, "SDL_PutAudioStreamData")
|
||||
purego.RegisterLibFunc(&sdlGetAudioStreamQueued, h, "SDL_GetAudioStreamQueued")
|
||||
purego.RegisterLibFunc(&sdlDestroyAudioStream, h, "SDL_DestroyAudioStream")
|
||||
purego.RegisterLibFunc(&sdlGetAudioPlaybackDevices, h, "SDL_GetAudioPlaybackDevices")
|
||||
purego.RegisterLibFunc(&sdlGetAudioDeviceName, h, "SDL_GetAudioDeviceName")
|
||||
loaded = true
|
||||
return nil
|
||||
}
|
||||
@@ -113,3 +137,40 @@ func cbytes(s string) *byte {
|
||||
runtime.KeepAlive(b)
|
||||
return &b[0]
|
||||
}
|
||||
|
||||
type AudioSpec struct {
|
||||
Format uint16
|
||||
Channels int32
|
||||
Freq int32
|
||||
}
|
||||
|
||||
func OpenAudioDeviceStream(devid uint32, spec AudioSpec) uintptr {
|
||||
return sdlOpenAudioDeviceStream(devid, unsafe.Pointer(&spec), 0, 0)
|
||||
}
|
||||
func ResumeAudioStreamDevice(stream uintptr) bool { return sdlResumeAudioStreamDevice(stream) }
|
||||
func DestroyAudioStream(stream uintptr) { sdlDestroyAudioStream(stream) }
|
||||
func PutAudioStreamData(stream uintptr, buf []byte) bool {
|
||||
return sdlPutAudioStreamData(stream, unsafe.Pointer(&buf[0]), int32(len(buf)))
|
||||
}
|
||||
func GetAudioStreamQueued(stream uintptr) int32 { return sdlGetAudioStreamQueued(stream) }
|
||||
|
||||
// playback device
|
||||
type AudioDevice struct {
|
||||
ID uint32
|
||||
Name string
|
||||
}
|
||||
|
||||
func GetAudioPlaybackDevices() []AudioDevice {
|
||||
var count int32
|
||||
ptr := sdlGetAudioPlaybackDevices(&count)
|
||||
if ptr == 0 || count == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := unsafe.Slice((*uint32)(unsafe.Pointer(ptr)), count)
|
||||
devs := make([]AudioDevice, 0, count)
|
||||
for _, id := range ids {
|
||||
name := cstr(sdlGetAudioDeviceName(id))
|
||||
devs = append(devs, AudioDevice{ID: id, Name: name})
|
||||
}
|
||||
return devs
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user