From b9761d521087e23ee7a83b854d356b1fa07fe06e Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 29 Aug 2022 00:00:51 -0500 Subject: add channel_set_volume and channel_set_pan --- src/channel.c | 18 ++++++++++++++++++ src/channel.h | 6 +++++- src/channel.test.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/channel.c b/src/channel.c index 0141569..603a980 100644 --- a/src/channel.c +++ b/src/channel.c @@ -40,6 +40,24 @@ void channel_resume(struct channel_t *chan) } +void channel_set_volume(struct channel_t *chan, float volume) +{ + if (volume > 1.0f) volume = 1.0f; + if (volume < 0.0f) volume = 0.0f; + + p_atomic_int_set(&(chan->volume), 255*volume); +} + + +void channel_set_pan(struct channel_t *chan, float pan) +{ + if (pan > 1.0f) pan = 1.0f; + if (pan < -1.0f) pan = -1.0f; + + p_atomic_int_set(&(chan->pan), 128*pan); +} + + void channel_get_next_sample(float *left, float *right, struct channel_t *chan) { bool active = p_atomic_int_get(&(chan->active)); diff --git a/src/channel.h b/src/channel.h index 2bc45d2..6f06803 100644 --- a/src/channel.h +++ b/src/channel.h @@ -11,7 +11,7 @@ struct channel_t { volatile pint active; /* boolean */ volatile pint paused; /* boolean */ volatile pint volume; /* 0-255 */ - volatile pint pan; /* -128-127 */ + volatile pint pan; /* -128-128 */ PMutex *sound_mutex; struct mossrose_sound_t sound; @@ -21,6 +21,10 @@ struct channel_t { void channel_init(struct channel_t *chan); void channel_reset(struct channel_t *chan); +void channel_pause(struct channel_t *chan); +void channel_resume(struct channel_t *chan); +void channel_set_volume(struct channel_t *chan, float volume); +void channel_set_pan(struct channel_t *chan, float pan); void channel_get_next_sample(float *left, float *right, struct channel_t *chan); diff --git a/src/channel.test.c b/src/channel.test.c index 4fddf45..2c5cdad 100644 --- a/src/channel.test.c +++ b/src/channel.test.c @@ -103,6 +103,50 @@ void test_channel_resume() } +void test_channel_set_volume() +{ + struct channel_t chan; + chan.volume = 255; + + channel_set_volume(&chan, 0.0f); + lily_assert_int_equal(chan.volume, 0); + channel_set_volume(&chan, 0.5f); + lily_assert_int_equal(chan.volume, 127); + channel_set_volume(&chan, 1.0f); + lily_assert_int_equal(chan.volume, 255); + + /* oob */ + channel_set_volume(&chan, -1.0f); + lily_assert_int_equal(chan.volume, 0); + channel_set_volume(&chan, 2.0f); + lily_assert_int_equal(chan.volume, 255); +} + + +void test_channel_set_pan() +{ + struct channel_t chan; + chan.pan = 255; + + channel_set_pan(&chan, -1.0f); + lily_assert_int_equal(chan.pan, -128); + channel_set_pan(&chan, -0.5f); + lily_assert_int_equal(chan.pan, -64); + channel_set_pan(&chan, 0.0f); + lily_assert_int_equal(chan.pan, 0); + channel_set_pan(&chan, 0.5f); + lily_assert_int_equal(chan.pan, 64); + channel_set_pan(&chan, 1.0f); + lily_assert_int_equal(chan.pan, 128); + + /* oob */ + channel_set_pan(&chan, -10.0f); + lily_assert_int_equal(chan.pan, -128); + channel_set_pan(&chan, 2.0f); + lily_assert_int_equal(chan.pan, 128); +} + + /* get_next_sample tests */ void test_channel_get_next_sample_inactive() { @@ -217,6 +261,8 @@ void suite_channel() lily_run_test(test_channel_reset); lily_run_test(test_channel_pause); lily_run_test(test_channel_resume); + lily_run_test(test_channel_set_volume); + lily_run_test(test_channel_set_pan); lily_run_test(test_channel_get_next_sample_inactive); lily_run_test(test_channel_get_next_sample_paused); -- cgit v1.2.1