From 7a282716e4c54d880064ac59cbae68516dced4e4 Mon Sep 17 00:00:00 2001 From: Gloria Kennickell Date: Thu, 28 Nov 2024 00:32:16 +0000 Subject: [PATCH] Add support for audio queries: fix permissions on SoundRaw* + add atari_env test --- docs/cpp-interface.md | 12 ++++++++++++ src/ale/common/SoundRaw.cxx | 4 ++-- src/ale/common/SoundRaw.hxx | 2 +- src/ale/python/env.py | 2 +- tests/python/test_atari_env.py | 9 +++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) mode change 100755 => 100644 src/ale/common/SoundRaw.cxx mode change 100755 => 100644 src/ale/common/SoundRaw.hxx diff --git a/docs/cpp-interface.md b/docs/cpp-interface.md index 48c42dff9..fe5f01886 100644 --- a/docs/cpp-interface.md +++ b/docs/cpp-interface.md @@ -37,6 +37,18 @@ ale::Action a = legal_actions[rand() % legal_actions.size()]; float reward = ale.act(a); ``` +An optional sound observation is provided. To enable, set the associated environment parameter: + +```cpp +ale.setBool("sound_obs", True); +``` + +Once enabled, the sound observation may be obtained by calling: + +```cpp +ale.getAudio() +``` + Finally, one can check whether the episode has terminated using the function `ale.game_over()`. With these functions one can already implement a very simple agent that plays randomly for one episode: ```cpp diff --git a/src/ale/common/SoundRaw.cxx b/src/ale/common/SoundRaw.cxx old mode 100755 new mode 100644 index 758f87356..6098c4edb --- a/src/ale/common/SoundRaw.cxx +++ b/src/ale/common/SoundRaw.cxx @@ -120,7 +120,7 @@ void SoundRaw::set(uint16_t addr, uint8_t value, int cycle) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void SoundRaw::process(uint8_t* buffer, uint32_t samples) { - // Process all the audio register updates up to this frame + // Process all the audio register updates up to this frame // Set audio registers uint32_t regSize = myRegWriteQueue.size(); for(uint32_t i = 0; i < regSize; ++i) { @@ -128,7 +128,7 @@ void SoundRaw::process(uint8_t* buffer, uint32_t samples) myTIASound.set(info.addr, info.value); myRegWriteQueue.pop_front(); } - + // Process audio registers myTIASound.process(buffer, samples); } diff --git a/src/ale/common/SoundRaw.hxx b/src/ale/common/SoundRaw.hxx old mode 100755 new mode 100644 index 2d64776c6..87b7fc2d9 --- a/src/ale/common/SoundRaw.hxx +++ b/src/ale/common/SoundRaw.hxx @@ -194,7 +194,7 @@ private: // Indicates the cycle when a sound register was last set int myLastRegisterSetCycle; - // Queue of TIA register writes + // Queue of TIA register writes std::deque myRegWriteQueue; }; diff --git a/src/ale/python/env.py b/src/ale/python/env.py index 29c2027b3..e9d0e3ddb 100644 --- a/src/ale/python/env.py +++ b/src/ale/python/env.py @@ -74,7 +74,7 @@ def __init__( game sounds. This will lock emulation to the ROMs specified FPS If `rgb_array` we'll return the `rgb` key in step metadata with the current environment RGB frame. - sound_obs: bool => Add teh sound from the frame to the observation. + sound_obs: bool => Add the sound from the frame to the observation. Note: - The game must be installed, see ale-import-roms, or ale-py-roms. diff --git a/tests/python/test_atari_env.py b/tests/python/test_atari_env.py index f66e63e29..d113ea5e5 100644 --- a/tests/python/test_atari_env.py +++ b/tests/python/test_atari_env.py @@ -399,3 +399,12 @@ def test_gym_compliance(tetris_env): check_env(tetris_env.unwrapped, skip_render_check=True) assert len(caught_warnings) == 0, [w.message for w in caught_warnings] + + +def test_sound_obs(): + env = gymnasium.make("ALE/MsPacman-v5", sound_obs=True) + + with warnings.catch_warnings(record=True) as caught_warnings: + check_env(env.unwrapped, skip_render_check=True) + + assert caught_warnings == [], [caught.message.args[0] for caught in caught_warnings]