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

Add custom sound effect Lua API #185

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

griffi-gh
Copy link
Contributor

@griffi-gh griffi-gh commented Oct 20, 2024

Proof-of-concept sound effect API

Adds following functions:

  • this:create_sfx(sample_array: table, name: string?) -> sfx
    Creates a sound effect. Sample array is an array of samples (floating-point, 2 channels at 44khz)
    Each sample must be a floating point number in range: -1.0..1.0 (out-of-range values are clamped)
    Number of samples must be a multiple of number of channels (2)
    Ordering is as follows: [right ch. sample 1][left ch. sample 1][right ch. sample 2][left ch. sample 2]... and so on
  • sfx:play(volume: number?)
    sfx:play(volume: number?, x: number, y: number)
    If position is omitted: Play sound effect globally with specified volume (full volume if nil/omitted)
    If position is specified: Plays the sound effect locally at the specified coordinates with the specified volume (full volume if nil)

TODO:

  • allow creating sound effects and playing them
  • allow setting sound source position and volume (add optional args to sfx:play(...))
  • support different source formats (e.g. integer samples or mono channel)
  • extend function to append extra chunks to the sfx
  • figure out how and when to properly clean up sound effect objects?
    • __gc gets called randomly?
    • can sound effects be freed while they're playing?

MAYBE:

  • create a tool like the image to lua one for converting wav/ogg/mp3/etc files to a list of samples for create_sfx.
  • function to get built-in sound effects as SfxMT userdatas? which would allow this api to also play built-in ones
  • some sort of "real-time" audio playback feature for longer audio? (with callback for generating audio data)

@griffi-gh
Copy link
Contributor Author

(enable sound for this one)

2024-10-20_17-58-39.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant