Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fade volume #87

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion playdate_example/src/playdate_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import playdate/api
const FONT_PATH = "/System/Fonts/Asheville-Sans-14-Bold.pft"
const NIM_IMAGE_PATH = "/images/nim_logo"
const PLAYDATE_NIM_IMAGE_PATH = "/images/playdate_nim"
const BACKGROUND_MUSIC_PATH = "/audio/finally_see_the_light"
const BACKGROUND_MUSIC_SAMPLE_RATE = 48_000
const BACKGROUND_MUSIC_FADE_IN_SECONDS = 4.0
const BACKGROUND_MUSIC_FADE_IN_SAMPLES = (BACKGROUND_MUSIC_SAMPLE_RATE * BACKGROUND_MUSIC_FADE_IN_SECONDS).int32

var font: LCDFont

Expand Down Expand Up @@ -80,9 +84,11 @@ proc handler(event: PDSystemEvent, keycode: uint) {.raises: [].} =
except:
playdate.system.logToConsole(getCurrentExceptionMsg())
# Inline try/except
filePlayer = try: playdate.sound.newFilePlayer("/audio/finally_see_the_light") except: nil
filePlayer = try: playdate.sound.newFilePlayer(BACKGROUND_MUSIC_PATH) except: nil

filePlayer.play(0)
fileplayer.volume = 0.0 # first set folume to 0%
filePlayer.fadeVolume(1.0, 1.0, BACKGROUND_MUSIC_FADE_IN_SAMPLES, nil) # then fade to 100%

# Add a checkmark menu item that plays a sound when switched and unpaused
discard playdate.system.addCheckmarkMenuItem("Checkmark", false,
Expand Down
8 changes: 4 additions & 4 deletions src/playdate/bindings/sound.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ type PlaydateSoundFileplayer {.importc: "const struct playdate_sound_fileplayer"
# start: cfloat; `end`: cfloat) {.cdecl.}
# didUnderrun* {.importc: "didUnderrun".}: proc (player: ptr FilePlayer): cint {.cdecl.}
setFinishCallback* {.importc: "setFinishCallback".}: proc (
player: FilePlayerPtr; callback: PDSndCallbackProcRaw, userData: pointer = nil) {.cdecl, raises: [].}
player: FilePlayerPtr; callback: PDSndCallbackProcRaw, userdata: pointer = nil) {.cdecl, raises: [].}
# setLoopCallback* {.importc: "setLoopCallback".}: proc (player: ptr FilePlayer;
# callback: SndCallbackProc) {.cdecl.}
getOffset {.importc: "getOffset".}: proc (player: FilePlayerPtr): cfloat {.cdecl, raises: [].}
# getRate* {.importc: "getRate".}: proc (player: ptr FilePlayer): cfloat {.cdecl.}
# setStopOnUnderrun* {.importc: "setStopOnUnderrun".}: proc (
# player: ptr FilePlayer; flag: cint) {.cdecl.}
# fadeVolume* {.importc: "fadeVolume".}: proc (player: ptr FilePlayer; left: cfloat;
# right: cfloat; len: int32T; finishCallback: SndCallbackProc) {.cdecl.}
fadeVolume* {.importc: "fadeVolume".}: proc (player: FilePlayerPtr; left: cfloat;
ninovanhooff marked this conversation as resolved.
Show resolved Hide resolved
right: cfloat; len: cint; finishCallback: PDSndCallbackProcRaw, userdata: pointer = nil) {.cdecl, raises:[].}
# setMP3StreamSource* {.importc: "setMP3StreamSource".}: proc (
# player: ptr FilePlayer; dataSource: proc (data: ptr uint8T; bytes: cint;
# userdata: pointer): cint {.cdecl.}; userdata: pointer; bufferLen: cfloat) {.
Expand Down Expand Up @@ -90,7 +90,7 @@ type PlaydateSoundSampleplayer {.importc: "const struct playdate_sound_samplepla
setPlayRange* {.importc: "setPlayRange".}: proc (player: SamplePlayerPtr;
start: cint; `end`: cint) {.cdecl, raises: [].}
setFinishCallback* {.importc: "setFinishCallback".}: proc (
player: SamplePlayerPtr; callback: PDSndCallbackProcRaw, userData: pointer = nil) {.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: SamplePlayerPtr): cfloat {.cdecl , raises: [].}
Expand Down
17 changes: 16 additions & 1 deletion src/playdate/sound.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type
resource: AudioSamplePtr
AudioSample* = ref AudioSampleObj

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

proc `=destroy`(this: var AudioSampleObj) =
privateAccess(PlaydateSound)
Expand Down Expand Up @@ -55,6 +55,7 @@ type SoundSource* = ref SoundSourceObj
type
FilePlayerObj = object of SoundSourceObj
finishCallback: PDFilePlayerCallbackFunction
fadeVolumeCallback: PDFilePlayerCallbackFunction
FilePlayer* = ref FilePlayerObj

PDFilePlayerCallbackFunction* = proc(player: FilePlayer) {.raises: [].}
Expand Down Expand Up @@ -139,6 +140,11 @@ proc privateFilePlayerFinishCallback(soundSource: SoundSourcePtr, userdata: poin
if filePlayer.finishCallback != nil:
filePlayer.finishCallback(filePlayer)

proc privateFilePlayerFadeVolumeCallback(soundSource: SoundSourcePtr, userdata: pointer) {.cdecl, raises: [].} =
ninovanhooff marked this conversation as resolved.
Show resolved Hide resolved
let filePlayer = cast[FilePlayer](userdata)
if filePlayer.fadeVolumeCallback != nil:
filePlayer.fadeVolumeCallback(filePlayer)

proc setFinishCallback*(this: FilePlayer, callback: PDFilePlayerCallbackFunction) =
privateAccess(PlaydateSound)
privateAccess(PlaydateSoundFileplayer)
Expand All @@ -148,6 +154,15 @@ proc setFinishCallback*(this: FilePlayer, callback: PDFilePlayerCallbackFunction
else:
playdate.sound.fileplayer.setFinishCallback(this.resource, privateFilePlayerFinishCallback, cast[pointer](this))

proc fadeVolume*(this: FilePlayer, left, right: float32, len: int32, callback: PDFilePlayerCallbackFunction) =
privateAccess(PlaydateSound)
privateAccess(PlaydateSoundFileplayer)
this.fadeVolumeCallback = callback
if callback == nil:
playdate.sound.fileplayer.fadeVolume(this.resource, left.cfloat, right.cfloat, len.cint , nil, nil)
else:
playdate.sound.fileplayer.fadeVolume(this.resource, left.cfloat, right.cfloat, len.cint , privateFilePlayerFadeVolumeCallback, cast[pointer](this))
ninovanhooff marked this conversation as resolved.
Show resolved Hide resolved

proc finishCallback*(this: FilePlayer): PDFilePlayerCallbackFunction =
return this.finishCallback

Expand Down
Loading