Skip to content

Commit

Permalink
ADD userData pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
ninovanhooff committed Mar 29, 2024
1 parent 7e1eb45 commit cc6eff8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/playdate/bindings/sound.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type AudioSamplePtr = pointer
type SamplePlayerPtr = pointer
type SoundSourcePtr = pointer

type PDSndCallbackProcRaw {.importc: "sndCallbackProc", header: "pd_api.h".} = proc(soundSource: SoundSourcePtr): void {.cdecl.}
type PDSndCallbackProcRaw {.importc: "sndCallbackProc", header: "pd_api.h".} = proc(soundSource: SoundSourcePtr, userData: pointer): void {.cdecl.}


type PlaydateSoundFileplayer {.importc: "const struct playdate_sound_fileplayer",
Expand Down Expand Up @@ -88,7 +88,7 @@ type PlaydateSoundSampleplayer {.importc: "const struct playdate_sound_samplepla
# setPlayRange* {.importc: "setPlayRange".}: proc (player: ptr SamplePlayer;
# start: cint; `end`: cint) {.cdecl.}
setFinishCallback* {.importc: "setFinishCallback".}: proc (
player: SamplePlayerPtr; callback: PDSndCallbackProcRaw) {.cdecl, raises: [].}
player: SamplePlayerPtr; callback: PDSndCallbackProcRaw, userData: pointer = nil) {.cdecl, raises: [].}
# setLoopCallback* {.importc: "setLoopCallback".}: proc (player: ptr SamplePlayer;
# callback: SndCallbackProc) {.cdecl.}
# getOffset* {.importc: "getOffset".}: proc (player: ptr SamplePlayer): cfloat {.cdecl.}
Expand Down
10 changes: 5 additions & 5 deletions src/playdate/sound.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type
resource: AudioSamplePtr
AudioSample* = ref AudioSampleObj

PDSoundCallbackFunction* = proc() {.raises: [].}
PDSoundCallbackFunction* = proc(userData: pointer) {.raises: [].}

var
## Finish callbacks for sound sources (FilePlayer, SamplePlayer)
Expand Down Expand Up @@ -217,9 +217,9 @@ proc rate*(this: SamplePlayer): float32 =
privateAccess(PlaydateSoundSampleplayer)
return playdate.sound.sampleplayer.getRate(this.resource).float32

proc privateFinishCallback(soundSource: SoundSourcePtr) {.cdecl, raises: [].} =
proc privateFinishCallback(soundSource: SoundSourcePtr, userData: pointer) {.cdecl, raises: [].} =
try:
soundCallbackMap[soundSource]()
soundCallbackMap[soundSource](userData)
except:
echo "No finish callback for sound source pointer " & repr(soundSource)

Expand All @@ -229,10 +229,10 @@ proc setFinishCallback*(this: SamplePlayer, callback: PDSoundCallbackFunction) =
try:
if callback == nil:
soundCallbackMap.del(this.resource)
playdate.sound.sampleplayer.setFinishCallback(this.resource, nil)
playdate.sound.sampleplayer.setFinishCallback(this.resource, nil, nil)
else:
soundCallbackMap[this.resource] = callback
playdate.sound.sampleplayer.setFinishCallback(this.resource, privateFinishCallback)
playdate.sound.sampleplayer.setFinishCallback(this.resource, privateFinishCallback, nil)
except:
echo "Error setting finish callback"

Expand Down

0 comments on commit cc6eff8

Please sign in to comment.