Skip to content

Commit

Permalink
FEAT: possibility to set/get device's gain in db and get device's cha…
Browse files Browse the repository at this point in the history
…nnels and sample rate
  • Loading branch information
Oldes committed Sep 20, 2023
1 parent e84372d commit 1374aeb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 46 deletions.
83 changes: 45 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,53 +194,60 @@ Return true if sound ended

#### __MA-SOUND__ - MiniAudio sound object

| Refinement | Gets | Sets | Description
|-----------------|--------------------|------------------------------|------------
|volume |decimal! |integer! decimal! percent! |Sound volume
|pan |decimal! |decimal! |Stereo panning (from -1.0 to 1.0)
|pitch |decimal! |decimal! |Sound pitch
|position |pair! |pair! |Sound position (x and y for now) relative to the listener
|cursor |integer! |integer! time! |Sound playback position in PCM frames
|time |time! |time! |Sound playback position as time
|duration |time! |none |Sound duration in time
|frames |integer! |none |Sound length in PCM frames
|sample-rate |integer! |none |Number of samples per second
|spatialize |logic! |logic! |3D spatialization state
|is-looping |logic! |logic! |Whether sound is looping
|is-playing |logic! |logic! |Whether sound is playing
|at-end |logic! |none |Whether sound is at end
|start |integer! |integer! time! |Absolute timer when the sound should be started (frames or time)
|stop |integer! |integer! time! |Absolute timer when the sound should be stopped (frames or time)
|x |decimal! |integer! decimal! |Sound X position
|y |decimal! |integer! decimal! |Sound Y position
|z |decimal! |integer! decimal! |Sound Z position
|source |file! handle! |none |Sound source as a loaded file or data source node
```rebol
;Refinement Gets Sets Description
/volume decimal! [integer! decimal! percent!] "Sound volume"
/pan decimal! decimal! "Stereo panning (from -1.0 to 1.0)"
/pitch decimal! decimal! "Sound pitch"
/position pair! pair! "Sound position (x and y for now) relative to the listener"
/cursor integer! [integer! time!] "Sound playback position in PCM frames"
/time time! time! "Sound playback position as time"
/duration time! none "Sound duration in time"
/frames integer! none "Sound length in PCM frames"
/sample-rate integer! none "Number of samples per second"
/spatialize logic! logic! "3D spatialization state"
/is-looping logic! logic! "Whether sound is looping"
/is-playing logic! logic! "Whether sound is playing"
/at-end logic! none "Whether sound is at end"
/start integer! [integer! time!] "Absolute timer when the sound should be started (frames or time)"
/stop integer! [integer! time!] "Absolute timer when the sound should be stopped (frames or time)"
/x decimal! [integer! decimal!] "Sound X position"
/y decimal! [integer! decimal!] "Sound Y position"
/z decimal! [integer! decimal!] "Sound Z position"
/source [file! handle!] none "Sound source as a loaded file or data source node"
```

#### __MA-ENGINE__ - MiniAudio device engine

| Refinement | Gets | Sets | Description
|-----------------|--------------------|------------------------------|------------
|volume |decimal! |integer! decimal! percent! |Global volume
|cursor |integer! |integer! time! |Engine playback position in PCM frames
|time |time! |time! |Engine playback position as time
|resources |block! |none |Used engine resources (sounds, nodes..)
```rebol
;Refinement Gets Sets Description
/volume decimal! [integer! decimal! percent!] "Global volume"
/cursor integer! [integer! time!] "Engine playback position in PCM frames"
/time time! time! "Engine playback position as time"
/resources block! none "Used engine resources (sounds, nodes..)"
/channels integer! none "Number of output channels"
/sample-rate integer! none "Ouput device sample rate per second"
/gain-db decimal! [integer! decimal!] "The amplification factor in decibels"
```

#### __MA-NOISE__ - MiniAudio noise generator

| Refinement | Gets | Sets | Description
|-----------------|--------------------|------------------------------|------------
|amplitude |decimal! |decimal! |Maximum value of the noise signal
|format |word! |none |f32, s16, s24, s32, u8
|type |word! |none |white, pink or brownian
```rebol
;Refinement Gets Sets Description
/amplitude decimal! decimal! "Maximum value of the noise signal"
/format word! none "f32, s16, s24, s32, u8"
/type word! none "white, pink or brownian"
```

#### __MA-WAWEFORM__ - MiniAudio sine, square, triangle and sawtooth waveforms generator

| Refinement | Gets | Sets | Description
|-----------------|--------------------|------------------------------|------------
|amplitude |decimal! |decimal! |Signal amplitude
|frequency |decimal! |decimal! |Signal frequency in hertzs
|format |word! |none |f32, s16, s24, s32, u8
|type |word! |none |sine, square, triangle or sawtooth
```rebol
;Refinement Gets Sets Description
/amplitude decimal! decimal! "Signal amplitude"
/frequency decimal! decimal! "Signal frequency in hertzs"
/format word! none "f32, s16, s24, s32, u8"
/type word! none "sine, square, triangle or sawtooth"
```


## Other extension values:
Expand Down
17 changes: 17 additions & 0 deletions src/miniaudio-commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ int MAEngine_get_path(REBHOB *hob, REBCNT word, REBCNT *type, RXIARG *arg) {
*type = RXT_TIME;
arg->int64 = ma_engine_get_time_in_milliseconds(engine) * 1000000;
break;
case W_ARG_CHANNELS:
*type = RXT_INTEGER;
arg->uint64 = ma_engine_get_channels(engine);
break;
case W_ARG_SAMPLE_RATE:
*type = RXT_INTEGER;
arg->uint64 = ma_engine_get_sample_rate(engine);
break;
case W_ARG_GAIN_DB:
*type = RXT_DECIMAL;
arg->dec64 = ma_engine_get_gain_db(engine);
break;
default:
return PE_BAD_SELECT;
}
Expand Down Expand Up @@ -170,6 +182,11 @@ int MAEngine_set_path(REBHOB *hob, REBCNT word, REBCNT *type, RXIARG *arg) {
if (arg->uint64 < 0) return PE_BAD_SET;
ma_engine_set_time_in_milliseconds(engine, arg->uint64 / 1000000);
break;
case W_ARG_GAIN_DB:
if (*type == RXT_DECIMAL) ma_engine_set_gain_db(engine, (float)arg->dec64);
else if (*type == RXT_INTEGER) ma_engine_set_gain_db(engine, (float)arg->int64);
else return PE_BAD_SET_TYPE;
break;
default:
return PE_BAD_SET;
}
Expand Down
4 changes: 3 additions & 1 deletion src/miniaudio-rebol-extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ enum ma_arg_words {W_ARG_0,
W_ARG_Z,
W_ARG_SOURCE,
W_ARG_RESOURCES,
W_ARG_CHANNELS,
W_ARG_GAIN_DB,
W_ARG_AMPLITUDE,
W_ARG_FORMAT,
W_ARG_TYPE,
Expand Down Expand Up @@ -140,7 +142,7 @@ typedef int (*MyCommandPointer)(RXIFRM *frm, void *ctx);
"looping: command [\"Set the looping\" sound [handle!] value [logic!]]\n"\
"looping?: command [\"Get the looping\" sound [handle!]]\n"\
"end?: command [\"Return true if sound ended\" sound [handle!]]\n"\
"init-words [volume pan pitch position cursor time duration frames sample-rate spatialize is-looping is-playing at-end start stop x y z source resources amplitude format type frequency][white pink brownian sine square triangle sawtooth f32 s16 s24 s32 u8]\n"\
"init-words [volume pan pitch position cursor time duration frames sample-rate spatialize is-looping is-playing at-end start stop x y z source resources channels gain-db amplitude format type frequency][white pink brownian sine square triangle sawtooth f32 s16 s24 s32 u8]\n"\
"protect/hide 'init-words\n"\
"\n"\
";; Waveform types\n"\
Expand Down
18 changes: 11 additions & 7 deletions src/miniaudio-rebol-extension.r3
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ handles: make map! [
cursor integer! [integer! time!] "Engine playback position in PCM frames"
time time! time! "Engine playback position as time"
resources block! none "Used engine resources (sounds, nodes..)"
channels integer! none "Number of output channels"
sample-rate integer! none "Ouput device sample rate per second"
gain-db decimal! [integer! decimal!] "The amplification factor in decibels"
]
ma-noise: [
"MiniAudio noise generator"
Expand All @@ -151,7 +154,7 @@ handles: make map! [
amplitude decimal! decimal! "Signal amplitude"
frequency decimal! decimal! "Signal frequency in hertzs"
format word! none "f32, s16, s24, s32, u8"
type word! none "sine, square, triangle or sawtooth"
type word! none "sine, square, triangle or sawtooth"
]
]

Expand All @@ -161,19 +164,20 @@ handles-doc: copy {}
foreach [name spec] handles [
append handles-doc ajoin [
LF LF "#### __" uppercase form name "__ - " spec/1 LF
LF "| Refinement | Gets | Sets | Description"
LF "|-----------------|--------------------|------------------------------|------------"
LF "```rebol"
LF ";Refinement Gets Sets Description"
]
foreach [name gets sets desc] next spec [
append handles-doc rejoin [
LF
#"|" pad name 17
#"|" pad gets 20
#"|" pad sets 30
#"|" desc
#"/" pad name 17
pad mold gets 20
pad mold sets 30
#"^"" desc #"^""
]
append arg-words name
]
append handles-doc "^/```"
]
;print handles-doc
arg-words: unique arg-words
Expand Down

0 comments on commit 1374aeb

Please sign in to comment.