Skip to content

Instantly share code, notes, and snippets.

@brunodeangelis
Last active June 30, 2026 18:38
Show Gist options
  • Select an option

  • Save brunodeangelis/e1e548696d7dd0434497fb03d64bb5d8 to your computer and use it in GitHub Desktop.

Select an option

Save brunodeangelis/e1e548696d7dd0434497fb03d64bb5d8 to your computer and use it in GitHub Desktop.
Odin DirectSound basic bindings for Handmade Hero
package ds
// This package contains the necessary DirectSound bindings
// to be able to carry forward with Handmade Hero from Day 7 onwards, in Odin.
// If you're looking for WAVEFORMATEX, that's defined in core:sys/windows
import win "core:sys/windows"
foreign import ds "system:Dsound.lib"
LPLONG :: ^win.LONG
DSBPLAY_LOOPING :: 0x00000001
DSBPLAY_LOCHARDWARE :: 0x00000002
DSBPLAY_LOCSOFTWARE :: 0x00000004
DSBPLAY_TERMINATEBY_TIME :: 0x00000008
DSBPLAY_TERMINATEBY_DISTANCE :: 0x000000010
DSBPLAY_TERMINATEBY_PRIORITY :: 0x000000020
DSSCL_NORMAL :: 0x00000001
DSSCL_PRIORITY :: 0x00000002
DSSCL_EXCLUSIVE :: 0x00000003
DSSCL_WRITEPRIMARY :: 0x00000004
DSBCAPS_PRIMARYBUFFER :: 0x00000001
DSBCAPS_STATIC :: 0x00000002
DSBCAPS_LOCHARDWARE :: 0x00000004
DSBCAPS_LOCSOFTWARE :: 0x00000008
DSBCAPS_CTRL3D :: 0x00000010
DSBCAPS_CTRLFREQUENCY :: 0x00000020
DSBCAPS_CTRLPAN :: 0x00000040
DSBCAPS_CTRLVOLUME :: 0x00000080
DSBCAPS_CTRLPOSITIONNOTIFY :: 0x00000100
DSBCAPS_CTRLFX :: 0x00000200
DSBCAPS_STICKYFOCUS :: 0x00004000
DSBCAPS_GLOBALFOCUS :: 0x00008000
DSBCAPS_GETCURRENTPOSITION2 :: 0x00010000
DSBCAPS_MUTE3DATMAXDISTANCE :: 0x00020000
DSBCAPS_LOCDEFER :: 0x00040000
DSBCAPS_TRUEPLAYPOSITION :: 0x00080000
DSCAPS :: struct {
dwSize: win.DWORD,
dwFlags: win.DWORD,
dwMinSecondarySampleRate: win.DWORD,
dwMaxSecondarySampleRate: win.DWORD,
dwPrimaryBuffers: win.DWORD,
dwMaxHwMixingAllBuffers: win.DWORD,
dwMaxHwMixingStaticBuffers: win.DWORD,
dwMaxHwMixingStreamingBuffers: win.DWORD,
dwFreeHwMixingAllBuffers: win.DWORD,
dwFreeHwMixingStaticBuffers: win.DWORD,
dwFreeHwMixingStreamingBuffers: win.DWORD,
dwMaxHw3DAllBuffers: win.DWORD,
dwMaxHw3DStaticBuffers: win.DWORD,
dwMaxHw3DStreamingBuffers: win.DWORD,
dwFreeHw3DAllBuffers: win.DWORD,
dwFreeHw3DStaticBuffers: win.DWORD,
dwFreeHw3DStreamingBuffers: win.DWORD,
dwTotalHwMemBytes: win.DWORD,
dwFreeHwMemBytes: win.DWORD,
dwMaxContigFreeHwMemBytes: win.DWORD,
dwUnlockTransferRateHwBuffers: win.DWORD,
dwPlayCpuOverheadSwBuffers: win.DWORD,
dwReserved1: win.DWORD,
dwReserved2: win.DWORD,
}
LPDSCAPS :: ^DSCAPS
DSBCAPS :: struct {
dwSize: win.DWORD,
dwFlags: win.DWORD,
dwBufferBytes: win.DWORD,
dwUnlockTransferRate: win.DWORD,
dwPlayCpuOverhead: win.DWORD,
}
LPDSBCAPS :: ^DSBCAPS;
DSBUFFERDESC :: struct {
dwSize: win.DWORD,
dwFlags: win.DWORD,
dwBufferBytes: win.DWORD,
dwReserved: win.DWORD,
lpwfxFormat: win.LPCWAVEFORMATEX,
}
LPCDSBUFFERDESC :: ^DSBUFFERDESC
IDirectSoundBuffer :: struct #raw_union {
#subtype IUnknown: win.IUnknown,
using Vtbl: ^IDirectSoundBufferVtbl,
}
IDirectSoundBufferVtbl :: struct {
using IUnknownVtbl: win.IUnknownVtbl,
GetCaps: proc "system" (this: ^IDirectSoundBuffer, pDSBufferCaps: LPDSBCAPS) -> win.HRESULT,
GetCurrentPosition: proc "system" (this: ^IDirectSoundBuffer, pdwCurrentPlayCursor, pdwCurrentWriteCursor: win.LPDWORD) -> win.HRESULT,
GetFormat: proc "system" (this: ^IDirectSoundBuffer, pwfxFormat: win.LPCWAVEFORMATEX, dwSizeAllocated: win.DWORD, pdwSizeWritten: win.LPDWORD) -> win.HRESULT,
GetVolume: proc "system" (this: ^IDirectSoundBuffer, plVolume: LPLONG) -> win.HRESULT,
GetPan: proc "system" (this: ^IDirectSoundBuffer, plPan: LPLONG) -> win.HRESULT,
GetFrequency: proc "system" (this: ^IDirectSoundBuffer, pdwFrequency: win.LPDWORD) -> win.HRESULT,
GetStatus: proc "system" (this: ^IDirectSoundBuffer, pdwStatus: win.LPDWORD) -> win.HRESULT,
Initialize: proc "system" (this: ^IDirectSoundBuffer, pDirectSound: LPDIRECTSOUND, pcDSBufferDesc: LPCDSBUFFERDESC) -> win.HRESULT,
Lock: proc "system" (this: ^IDirectSoundBuffer, dwOffset, dwBytes: win.DWORD, ppvAudioPtr1: ^win.LPVOID, pdwAudioBytes1: win.LPDWORD, ppvAudioPtr2: ^win.LPVOID, pdwAudioBytes2: win.LPDWORD, dwFlags: win.DWORD) -> win.HRESULT,
Play: proc "system" (this: ^IDirectSoundBuffer, dwReserved1, dwPriority, dwFlags: win.DWORD) -> win.HRESULT,
SetCurrentPosition: proc "system" (this: ^IDirectSoundBuffer, dwNewPosition: win.DWORD) -> win.HRESULT,
SetFormat: proc "system" (this: ^IDirectSoundBuffer, pcfxFormat: win.LPCWAVEFORMATEX) -> win.HRESULT,
SetVolume: proc "system" (this: ^IDirectSoundBuffer, lVolume: win.LONG) -> win.HRESULT,
SetPan: proc "system" (this: ^IDirectSoundBuffer, lPan: win.LONG) -> win.HRESULT,
SetFrequency: proc "system" (this: ^IDirectSoundBuffer, dwFrequency: win.DWORD) -> win.HRESULT,
Stop: proc "system" (this: ^IDirectSoundBuffer) -> win.HRESULT,
Unlock: proc "system" (this: ^IDirectSoundBuffer, pvAudioPtr1: win.LPVOID, dwAudioBytes1: win.DWORD, pvAudioPtr2: win.LPVOID, dwAudioBytes2: win.DWORD) -> win.HRESULT,
Restore: proc "system" (this: ^IDirectSoundBuffer) -> win.HRESULT,
}
LPDIRECTSOUNDBUFFER :: ^IDirectSoundBuffer
IDirectSound :: struct #raw_union {
#subtype IUnknown: win.IUnknown,
using Vtbl: ^IDirectSoundVtbl,
}
IDirectSoundVtbl :: struct {
using IUnknownVtbl: win.IUnknownVtbl,
CreateSoundBuffer: proc "system" (this: ^IDirectSound, pcDSBufferDesc: LPCDSBUFFERDESC, ppDSBuffer: ^^IDirectSoundBuffer, pUnkOuter: win.LPUNKNOWN) -> win.HRESULT,
GetCaps: proc "system" (this: ^IDirectSound, pDSCaps: LPDSCAPS) -> win.HRESULT,
DuplicateSoundBuffer: proc "system" (this: ^IDirectSound, pDSBufferOriginal: LPDIRECTSOUNDBUFFER, ppDSBufferDuplicate: ^^IDirectSoundBuffer) -> win.HRESULT,
SetCooperativeLevel: proc "system" (this: ^IDirectSound, hwnd: win.HWND, dwLevel: win.DWORD) -> win.HRESULT,
Compact: proc "system" (this: ^IDirectSound) -> win.HRESULT,
GetSpeakerConfig: proc "system" (this: ^IDirectSound, pdwSpeakerConfig: win.LPDWORD) -> win.HRESULT,
SetSpeakerConfig: proc "system" (this: ^IDirectSound, dwSpeakerConfig: win.DWORD) -> win.HRESULT,
Initialize: proc "system" (this: ^IDirectSound, pcGuidDevice: win.LPCGUID) -> win.HRESULT,
}
LPDIRECTSOUND :: ^IDirectSound
@(default_calling_convention = "system")
foreign ds {
DirectSoundCreate :: proc(pcGuidDevice: win.LPCGUID, ppDS: ^LPDIRECTSOUND, pUnkOuter: win.LPUNKNOWN) -> win.HRESULT ---
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment