-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic audio support and play-wav example (#28)
* First version of play-wav examples. * Move RWMode to rw.ts and remove types.ts * Throw an error in denoFromPlatformCallback. * Mark SDL_AudioSpec.callback as defective and allow SDL_AudioSpec to be constructed wihtout any parameters.
- Loading branch information
Showing
29 changed files
with
1,440 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Adapted from https://gigi.nullneuron.net/gigilabs/playing-a-wav-file-using-sdl2/ | ||
|
||
import { Box, Pointer, SDL, U32, U8, u32, u8 } from "SDL_ts"; | ||
import { SDL_FUNCTIONS } from "./sdlConfig.ts"; | ||
import { ASSETS_PATH } from "../../shared/constants.ts"; | ||
import { path } from "../../deps.ts"; | ||
|
||
const main = (): number => { | ||
if (SDL.Init(SDL.InitFlags.AUDIO, { functions: SDL_FUNCTIONS }) < 0) { | ||
return 1; | ||
} | ||
|
||
console.info("SDL Initialized."); | ||
|
||
const wavSpec = new SDL.AudioSpec(); | ||
const wavBufferBox = new Box<Pointer<u8>>(Pointer); | ||
const wavLengthBox = new Box<u32>(U32); | ||
|
||
if ( | ||
SDL.LoadWav( | ||
path.join(ASSETS_PATH, "powerup.wav"), | ||
wavSpec, | ||
wavBufferBox, | ||
wavLengthBox | ||
) == null | ||
) { | ||
console.error("ERROR: Failed to load wav file."); | ||
return 1; | ||
} | ||
|
||
const audioDeviceID = SDL.OpenAudioDevice(null, 0, wavSpec, null, 0); | ||
if (audioDeviceID <= 0) { | ||
console.error("ERROR: Faield to open an audio device."); | ||
return 1; | ||
} | ||
|
||
if ( | ||
SDL.QueueAudio(audioDeviceID, wavBufferBox.value, wavLengthBox.value) != 0 | ||
) { | ||
console.error(`ERROR: Faield to queue audio: ${SDL.GetError()}`); | ||
return 1; | ||
} | ||
|
||
SDL.PauseAudioDevice(audioDeviceID, 0); | ||
|
||
SDL.Delay(1000); | ||
|
||
SDL.CloseAudioDevice(audioDeviceID); | ||
SDL.FreeWAV(wavBufferBox.value); | ||
SDL.Quit(); | ||
console.info("SDL Shutdown."); | ||
|
||
return 0; | ||
}; | ||
|
||
try { | ||
Deno.exit(main()); | ||
} catch (error) { | ||
console.error(error); | ||
Deno.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { SDL } from "SDL_ts"; | ||
|
||
// This file contains the list of functions that are used in the project. | ||
|
||
export const SDL_FUNCTIONS = [ | ||
SDL.CloseAudioDevice, | ||
SDL.Delay, | ||
SDL.FreeWAV, | ||
SDL.GetError, | ||
SDL.Init, | ||
SDL.LoadWAV_RW, | ||
SDL.OpenAudioDevice, | ||
SDL.PauseAudioDevice, | ||
SDL.QueueAudio, | ||
SDL.Quit, | ||
SDL.RWFromFile, | ||
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
export * from "./src/SDL/audio.ts"; | ||
export * from "./src/SDL/callbacks.ts"; | ||
export * from "./src/SDL/constants.ts"; | ||
export * from "./src/SDL/enums.ts"; | ||
export * from "./src/SDL/events.ts"; | ||
export * from "./src/SDL/functionMacros.ts"; | ||
export * from "./src/SDL/functions.ts"; | ||
export * from "./src/SDL/pixels.ts"; | ||
export * from "./src/SDL/rw.ts"; | ||
export * from "./src/SDL/structs.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { u16, u32 } from "../types.ts"; | ||
|
||
export type AudioFormat = u16; | ||
export type AudioDeviceID = u32; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.