Skip to content

Commit

Permalink
Implement audio_element events
Browse files Browse the repository at this point in the history
Also implement testing using qemu on esp32 and esp32c3

Signed-off-by: Paul Guyot <pguyot@kallisys.net>
  • Loading branch information
pguyot committed May 10, 2024
1 parent 0b050e3 commit edcfc3b
Show file tree
Hide file tree
Showing 19 changed files with 1,278 additions and 64 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ jobs:
echo CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY=y >> sdkconfig
idf.py build -DCONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY=y
- name: Run tests using qemu
shell: bash
if: matrix.soc == 'esp32' || matrix.soc == 'esp32c3'
working-directory: AtomVM/src/platforms/esp32/components/atomvm_esp_adf/tests/
run: |
# Some images have erlang-22, so latest compatible rebar is 3.18.0
wget https://github.com/erlang/rebar3/releases/download/3.18.0/rebar3
chmod +x rebar3
DEBIAN_FRONTEND=noninteractive apt install -y -q erlang erlang-eunit
./rebar3 atomvm packbeam
. $IDF_PATH/export.sh
pip install -U pytest-embedded pytest-embedded-qemu
pytest --target=${{ matrix.soc }}
- name: "Create a ${{ matrix.soc }} image"
working-directory: AtomVM/src/platforms/esp32/build
run: |
Expand Down
8 changes: 7 additions & 1 deletion examples/play_aac_i2s/src/play_aac_i2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ start() ->
ok = esp_adf_audio_element:set_read_binary(AACDecoder, AACFile),
ok = esp_adf_audio_pipeline:register(AudioPipeline, AACDecoder, <<"aac">>),

% We know the sample sound is 44.1 kHz, stereo
I2SOutput = esp_adf_i2s_output:init([
{rate, 44100},
{bits, 16},
Expand All @@ -39,7 +40,12 @@ start() ->

ok = esp_adf_audio_pipeline:run(AudioPipeline),

timer:sleep(7000),
% We know the sample sound is about 7 seconds long.
ok =
receive
{audio_element, I2SOutput, {status, state_finished}} -> ok
after 7000 -> timeout
end,

ok = esp_adf_audio_pipeline:stop(AudioPipeline),
ok = esp_adf_audio_pipeline:wait_for_stop(AudioPipeline),
Expand Down
8 changes: 7 additions & 1 deletion examples/play_mp3_i2s/src/play_mp3_i2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ start() ->
ok = esp_adf_audio_element:set_read_binary(MP3Decoder, MP3File),
ok = esp_adf_audio_pipeline:register(AudioPipeline, MP3Decoder, <<"mp3">>),

% We know the sample sound is 44.1 kHz, stereo
I2SOutput = esp_adf_i2s_output:init([
{rate, 44100},
{bits, 16},
Expand All @@ -39,7 +40,12 @@ start() ->

ok = esp_adf_audio_pipeline:run(AudioPipeline),

timer:sleep(7000),
% We know the sample sound is about 7 seconds long.
ok =
receive
{audio_element, I2SOutput, {status, state_finished}} -> ok
after 7000 -> timeout
end,

ok = esp_adf_audio_pipeline:stop(AudioPipeline),
ok = esp_adf_audio_pipeline:wait_for_stop(AudioPipeline),
Expand Down
13 changes: 7 additions & 6 deletions nifs/atomvm_esp_adf_aac_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ static term nif_init(Context *ctx, int argc, term argv[])
TRACE("%s\n", __func__);
UNUSED(argc);

VALIDATE_VALUE(argv[1], term_is_list);
VALIDATE_VALUE(argv[0], term_is_list);
term active_term = interop_kv_get_value_default(argv[0], ATOM_STR("\x6", "active"), TRUE_ATOM, ctx->global);

if (UNLIKELY(memory_ensure_free(ctx, TERM_BOXED_RESOURCE_SIZE) != MEMORY_GC_OK)) {
if (UNLIKELY(memory_ensure_free(ctx, AUDIO_ELEMENT_OPAQUE_TERM_SIZE) != MEMORY_GC_OK)) {
ESP_LOGW(TAG, "Failed to allocate memory: %s:%i.", __FILE__, __LINE__);
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
Expand All @@ -51,13 +52,13 @@ static term nif_init(Context *ctx, int argc, term argv[])
ESP_LOGW(TAG, "Failed to allocate memory: %s:%i.\n", __FILE__, __LINE__);
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
term obj = enif_make_resource(erl_nif_env_from_context(ctx), rsrc_obj);
enif_release_resource(rsrc_obj);

aac_decoder_cfg_t aac_cfg = DEFAULT_AAC_DECODER_CONFIG();
rsrc_obj->audio_element = aac_decoder_init(&aac_cfg);
audio_element_handle_t audio_element = aac_decoder_init(&aac_cfg);

return obj;
atomvm_esp_adf_audio_element_init_resource(rsrc_obj, audio_element, active_term == TRUE_ATOM, ctx);

return atomvm_esp_adf_audio_element_resource_to_opaque(rsrc_obj, ctx);
}

static const struct Nif init_nif = {
Expand Down
Loading

0 comments on commit edcfc3b

Please sign in to comment.