Skip to content

Commit

Permalink
[decomp] macros for sound playback (#1453)
Browse files Browse the repository at this point in the history
* `sound-play` macro

* update source

* fix `add-debug-light` lol

* fix `add-debug-light` forreal

* Update debug.gc

* update some mood/tod decomp
  • Loading branch information
ManDude authored Jun 13, 2022
1 parent d73336b commit 8ccb1df
Show file tree
Hide file tree
Showing 247 changed files with 1,698 additions and 2,139 deletions.
18 changes: 13 additions & 5 deletions common/util/print_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ std::string meters_to_string(float value, bool append_trailing_decimal) {
}

/*!
* Convert a fixed point value to a string. Fixed point values usually end up with strange numbers
* Convert a fixed point value to a float. Fixed point values usually end up with strange numbers
* that were definitely not what was written when we do a naive conversion. This function
* is a bit more clever.
*/
std::string fixed_point_to_string(s64 value, s64 scale, bool append_trailing_decimal) {
float fixed_point_to_float(s64 value, s64 scale) {
float sign = value < 0 ? -1 : 1;
value = std::abs(value);
double naive = (double)value / scale;
Expand All @@ -54,7 +54,7 @@ std::string fixed_point_to_string(s64 value, s64 scale, bool append_trailing_dec
fixed_add += 5;
}
if ((s64)((fixed_start + fixed_add) / flt_scale * scale) == value) {
return float_to_string((fixed_start + fixed_add) / flt_scale * sign, append_trailing_decimal);
return (fixed_start + fixed_add) / flt_scale * sign;
}

// add e.g. 0.001 in a 1/1000 scale
Expand All @@ -63,10 +63,18 @@ std::string fixed_point_to_string(s64 value, s64 scale, bool append_trailing_dec
fixed_add += 1;
}
if ((s64)((fixed_start + fixed_add) / flt_scale * scale) == value) {
return float_to_string((fixed_start + fixed_add) / flt_scale * sign, append_trailing_decimal);
return (fixed_start + fixed_add) / flt_scale * sign;
}
// added too much!
ASSERT_MSG(false, fmt::format("fixed_point_to_string failed hard. v: {} s: {}", value, scale));
ASSERT_MSG(false, fmt::format("fixed_point_to_float failed hard. v: {} s: {}", value, scale));
return 0;
}

/*!
* Convert a fixed point value to a string. Wrapper around fixed_point_to_string
*/
std::string fixed_point_to_string(s64 value, s64 scale, bool append_trailing_decimal) {
return float_to_string(fixed_point_to_float(value, scale), append_trailing_decimal);
}

/*!
Expand Down
1 change: 1 addition & 0 deletions common/util/print_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include "common/common_types.h"

float fixed_point_to_float(s64 value, s64 scale);
std::string fixed_point_to_string(s64 value, s64 scale, bool append_trailing_decimal = false);
std::string float_to_string(float value, bool append_trailing_decimal = true);
std::string meters_to_string(float value, bool append_trailing_decimal = false);
Expand Down
127 changes: 127 additions & 0 deletions decompiler/IR2/FormExpressionAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3058,6 +3058,133 @@ void FunctionCallElement::update_from_stack(const Env& env,
}
}

// check for sound-play stuff
{
if (arg_forms.size() == 7 && unstacked.at(0)->to_form(env).is_symbol("sound-play-by-name")) {
auto ssn = arg_forms.at(0)->to_string(env);
static const std::string ssn_check = "(static-sound-name \"";
// idk what a good way to do this is :(
if (ssn.substr(0, ssn_check.size()) == ssn_check) {
// get sound name
auto sound_name = ssn.substr(ssn_check.size(), ssn.size() - ssn_check.size() - 2);

// get sound id
auto so_id_f = arg_forms.at(1);
if (so_id_f->to_string(env) == "(new-sound-id)") {
so_id_f = nullptr;
}

// get sound volume
bool panic = false;
auto so_vol_o = arg_forms.at(2)->to_form(env);
Form* so_vol_f = nullptr;
if (so_vol_o.is_int()) {
auto vol_as_flt_temp = fixed_point_to_float(so_vol_o.as_int(), 1024) * 100;
// fixed point convert is good but floating point accuracy sucks so we can do even better
// with a hardcoded case
for (int i = 0; i < 100; ++i) {
if (int(i * 1024.0f / 100.0f) == so_vol_o.as_int()) {
vol_as_flt_temp = i;
break;
}
}
// make the number now...
if (int(vol_as_flt_temp * 1024.0f / 100.0f) == so_vol_o.as_int()) {
if (so_vol_o.as_int() != 1024) {
so_vol_f = pool.form<ConstantTokenElement>(float_to_string(vol_as_flt_temp, false));
}
}
} else {
auto mr_vol = match(
Matcher::cast("int", Matcher::op_fixed(FixedOperatorKind::MULTIPLICATION,
{Matcher::single(10.24f), Matcher::any(0)})),
arg_forms.at(2));
if (mr_vol.matched) {
so_vol_f = mr_vol.maps.forms.at(0);
} else {
// AAAHHHH i dont know how to handle this volume thing!
panic = true;
}
}

// get sound pitch mod
auto so_pitch_o = arg_forms.at(3)->to_form(env);
Form* so_pitch_f = nullptr;
if (so_pitch_o.is_int()) {
if (so_pitch_o.as_int() != 0) {
so_pitch_f =
pool.form<ConstantTokenElement>(fixed_point_to_string(so_pitch_o.as_int(), 1524));
}
} else {
auto mr_pitch = match(
Matcher::cast("int", Matcher::op_fixed(FixedOperatorKind::MULTIPLICATION,
{Matcher::single(1524), Matcher::any(0)})),
arg_forms.at(3));
if (mr_pitch.matched) {
so_pitch_f = mr_pitch.maps.forms.at(0);
} else {
panic = true;
}
}

// rest
if (!panic) {
auto so_bend = arg_forms.at(4);
if (so_bend->to_form(env).is_int(0)) {
so_bend = nullptr;
}
auto elt_group = arg_forms.at(5)->try_as_element<GenericElement>();
if (elt_group && elt_group->op().is_func() &&
elt_group->op().func()->to_form(env).is_symbol("sound-group") &&
elt_group->elts().size() == 1) {
Form* so_group_f = nullptr;
if (!elt_group->elts().at(0)->to_form(env).is_symbol("sfx")) {
so_group_f = pool.form<ConstantTokenElement>(
elt_group->elts().at(0)->to_form(env).as_symbol()->name);
}
auto so_positional_f = arg_forms.at(6);
if (so_positional_f->to_form(env).is_symbol("#t")) {
so_positional_f = nullptr;
}
// now make the macro call!
std::vector<Form*> macro_args;
macro_args.push_back(pool.form<StringConstantElement>(sound_name));
if (so_id_f) {
macro_args.push_back(pool.form<ConstantTokenElement>(":id"));
macro_args.push_back(so_id_f);
}
if (so_vol_f) {
macro_args.push_back(pool.form<ConstantTokenElement>(":vol"));
macro_args.push_back(so_vol_f);
}
if (so_pitch_f) {
macro_args.push_back(pool.form<ConstantTokenElement>(":pitch"));
macro_args.push_back(so_pitch_f);
}
if (so_bend) {
macro_args.push_back(pool.form<ConstantTokenElement>(":bend"));
macro_args.push_back(so_bend);
}
if (so_group_f) {
macro_args.push_back(pool.form<ConstantTokenElement>(":group"));
macro_args.push_back(so_group_f);
}
if (so_positional_f) {
macro_args.push_back(pool.form<ConstantTokenElement>(":position"));
macro_args.push_back(so_positional_f);
}

new_form = pool.alloc_element<GenericElement>(
GenericOperator::make_function(pool.form<ConstantTokenElement>("sound-play")),
macro_args);
result->push_back(new_form);
return;
}
}
}
}
}

new_form = pool.alloc_element<GenericElement>(GenericOperator::make_function(unstacked.at(0)),
arg_forms);

Expand Down
33 changes: 23 additions & 10 deletions decompiler/config/all-types.gc
Original file line number Diff line number Diff line change
Expand Up @@ -2970,6 +2970,19 @@

;; - Types

(defenum sound-group
:bitfield #t
:type uint8
(sfx)
(music)
(dialog)
(sog3)
(ambient)
(sog5)
(sog6)
(sog7)
)

(deftype sound-id (uint32)
()
(:methods
Expand Down Expand Up @@ -3023,7 +3036,7 @@
(priority int8 :offset-assert 11)
(volume int32 :offset-assert 12)
(trans vector3w :inline :offset-assert 16)
(group uint8 :offset-assert 28)
(group sound-group :offset-assert 28)
)
:pack-me
:method-count-assert 9
Expand All @@ -3048,7 +3061,7 @@
)

(deftype sound-rpc-group-cmd (sound-rpc-cmd)
((group uint8 :offset-assert 4)
((group sound-group :offset-assert 4)
)
:method-count-assert 9
:size-assert #x5
Expand Down Expand Up @@ -3283,9 +3296,9 @@
(deftype sound-spec (basic)
((mask sound-mask :offset-assert 4)
(num float :offset-assert 8)
(group uint8 :offset-assert 12)
(group sound-group :offset-assert 12)
(sound-name-char uint8 16 :offset 16)
(sound-name sound-name :score 20 :offset 16)
(sound-name-char uint8 16 :offset 16) ;; moved to after sound-name
(trans float 4 :offset-assert 32) ;; guess
(volume int32 :offset-assert 48)
(pitch-mod int32 :offset-assert 52)
Expand Down Expand Up @@ -16301,7 +16314,7 @@
(define-extern effect-param->sound-spec (function sound-spec (pointer float) int sound-spec))
(define-extern ear-trans (function vector))
(define-extern sound-play-by-spec (function sound-spec sound-id vector sound-id))
(define-extern sound-play-by-name (function sound-name sound-id int int int int symbol sound-id))
(define-extern sound-play-by-name (function sound-name sound-id int int int sound-group symbol sound-id))
(define-extern sound-angle-convert (function float int))
(define-extern sound-set-ear-trans (function vector vector float int))
(define-extern activate-progress (function process progress-screen none))
Expand All @@ -16324,13 +16337,13 @@
(define-extern swap-sound-buffers (function vector vector float int))
(define-extern free-last-sound-buffer-entry (function int))
(define-extern sound-basic-cb (function int (pointer int32) none))
(define-extern sound-set-volume (function uint float int))
(define-extern sound-set-volume (function sound-group float int))
(define-extern sound-set-reverb (function int float float uint int))
(define-extern sound-pause (function sound-id int))
(define-extern sound-continue (function sound-id int))
(define-extern sound-group-pause (function uint int))
(define-extern sound-group-stop (function uint int))
(define-extern sound-group-continue (function uint int))
(define-extern sound-group-pause (function sound-group int))
(define-extern sound-group-stop (function sound-group int))
(define-extern sound-group-continue (function sound-group int))
(define-extern sound-set-falloff-curve (function int float float int))
(define-extern sound-set-sound-falloff (function sound-name int int int int))
(define-extern sound-set-flava (function uint int))
Expand Down Expand Up @@ -18601,7 +18614,7 @@
(define-extern update-mood-jungleb-blue (function mood-context float int none))
(define-extern update-mood-prt-color (function mood-context vector))
(define-extern update-mood-default (function mood-context float int none))
(define-extern update-mood-misty (function mood-context float float none))
(define-extern update-mood-misty (function mood-context float int none))
(define-extern update-mood-village2 (function mood-context float int none))
(define-extern update-mood-swamp (function mood-context float int none))
(define-extern update-mood-village1 (function mood-context float int none))
Expand Down
13 changes: 12 additions & 1 deletion decompiler/config/jak1_ntsc_black_label/var_names.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -3739,7 +3739,18 @@
"s4-0": ["s4-0", "vector"],
"v1-42": ["v1-42", "vector"],
"s3-1": "lod-to-use",
"f30-1": "cam-dist"
"f30-1": "cam-dist",
"s3-0": "tod",
"v1-17": "shadow-msk",
"a0-10": "lev-idx",
"s0-1": "lgt",
"s1-1": "cur-lgt",
"s2-0": "vu-lgt",
"a1-22": "lgt-msk-0",
"a2-14": "lgt-msk-1",
"f26-0": "lgt-interp",
"f28-0": "interp",
"f30-0": "cur-interp"
}
},

Expand Down
6 changes: 3 additions & 3 deletions goal_src/engine/ambient/ambient.gc
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@
(s4-1 string->sound-name)
)
(format (clear *temp-string*) "spool-~S" arg0)
(let ((s5-2 (s5-1 (s4-1 *temp-string*) (new-sound-id) 1024 0 0 1 #t)))
(let ((s5-2 (s5-1 (s4-1 *temp-string*) (new-sound-id) 1024 0 0 (sound-group sfx) #t)))
(set! (-> self sound-id) s5-2)
(let ((s4-3 (-> *display* base-frame-counter)))
(while (and (!= (current-str-id) s5-2) (< (- (-> *display* base-frame-counter) s4-3) (seconds 5)))
Expand Down Expand Up @@ -554,7 +554,7 @@
)
(format (clear *temp-string*) "spool-~S" arg0)
(set! (-> self sound-id)
(s5-0 (s4-0 *temp-string*) (new-sound-id) 1024 1 0 1 (the-as symbol (-> self trans)))
(s5-0 (s4-0 *temp-string*) (new-sound-id) 1024 1 0 (sound-group sfx) (the-as symbol (-> self trans)))
)
)
)
Expand All @@ -563,7 +563,7 @@
(s4-2 string->sound-name)
)
(format (clear *temp-string*) "spool-~S" arg0)
(set! (-> self sound-id) (s5-1 (s4-2 *temp-string*) (new-sound-id) 1024 0 0 1 #t))
(set! (-> self sound-id) (s5-1 (s4-2 *temp-string*) (new-sound-id) 1024 0 0 (sound-group sfx) #t))
)
)
)
Expand Down
30 changes: 12 additions & 18 deletions goal_src/engine/ambient/mood.gc
Original file line number Diff line number Diff line change
Expand Up @@ -677,23 +677,17 @@
)
(when (>= (-> *math-camera* trans y) -81920.0)
(if arg6
(sound-play-by-name
(static-sound-name "thunder")
*thunder-id*
(the int (* 10.24 (rand-vu-float-range 25.0 75.0)))
(the int (* 1524.0 (rand-vu-float-range -1.0 1.0)))
0
1
#t
(sound-play
"thunder"
:id *thunder-id*
:vol (rand-vu-float-range 25.0 75.0)
:pitch (rand-vu-float-range -1.0 1.0)
)
(sound-play-by-name
(static-sound-name "thunder")
*thunder-id*
(the int (* 10.24 (rand-vu-float-range 200.0 400.0)))
(the int (* 1524.0 (rand-vu-float-range -1.0 1.0)))
0
1
#t
(sound-play
"thunder"
:id *thunder-id*
:vol (rand-vu-float-range 200.0 400.0)
:pitch (rand-vu-float-range -1.0 1.0)
)
)
)
Expand Down Expand Up @@ -914,11 +908,11 @@
)


(defun update-mood-misty ((arg0 mood-context) (arg1 float) (arg2 float))
(defun update-mood-misty ((arg0 mood-context) (arg1 float) (arg2 int))
(update-mood-fog arg0 arg1)
(update-mood-sky-texture arg0 arg1)
(clear-mood-times arg0)
(update-mood-palette arg0 arg1 (the-as int arg2))
(update-mood-palette arg0 arg1 arg2)
(when *time-of-day-effects*
(update-mood-flames arg0 2 4 0 0.333 0.001953125 1.0)
(let* ((s4-1 (get-task-control (game-task misty-warehouse)))
Expand Down
2 changes: 1 addition & 1 deletion goal_src/engine/camera/cam-master.gc
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
(collide-kind background)
(the-as process #f)
s4-2
(new 'static 'pat-surface :camera #x1 :nocamera #x1 :nolineofsight #x1)
(new 'static 'pat-surface :nocamera #x1 :nolineofsight #x1)
)
)
)
Expand Down
Loading

0 comments on commit 8ccb1df

Please sign in to comment.