2018-11-15 14:11:45 -05:00
|
|
|
/*
|
2019-08-03 12:20:06 -04:00
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
2018-11-15 14:11:45 -05:00
|
|
|
*
|
2019-08-03 12:20:06 -04:00
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 DeanM for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2018-11-15 14:11:45 -05:00
|
|
|
*/
|
2019-08-25 00:36:18 -04:00
|
|
|
#include "shared-bindings/audiomixer/Mixer.h"
|
2021-11-10 09:42:21 -05:00
|
|
|
#include "shared-bindings/audiomixer/MixerVoice.h"
|
2019-08-25 00:36:18 -04:00
|
|
|
#include "shared-module/audiomixer/MixerVoice.h"
|
2018-11-15 14:11:45 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2019-08-25 00:36:18 -04:00
|
|
|
#include "shared-module/audiomixer/__init__.h"
|
2019-08-03 12:20:06 -04:00
|
|
|
#include "shared-module/audiocore/RawSample.h"
|
2018-11-20 14:14:22 -05:00
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
void common_hal_audiomixer_mixervoice_construct(audiomixer_mixervoice_obj_t *self) {
|
2019-08-24 08:56:12 -04:00
|
|
|
self->sample = NULL;
|
Mixer: Rework for performance, particularly of the S16 case
This removes downscaling (halving-add) when multiple voices are
being mixed. To avoid clipping, and get similar behavior to before,
set the "level" of each voice to (1/voice_count).
Slow paths that were applicable to only M0 chips were removed.
As a side effect, the internal volume representation is now 0 ..
0x8000 (inclusive), which additionally makes a level of exactly 0.5
representable.
Testing performed, on PyGamer: For all 4 data cases, for stereo and
mono, for 1 and 2 voices, play pure sign waves represented as
RawSamples and view the result on a scope and through headphones.
Also, scope the amount of time spent in background tasks.
Code size: growth of +272 bytes
Performance (time in background task when mixing 2 stereo 16-bit voices):
76us per down from 135us (once per ~2.9ms long term average)
(Decrease from 4.7% to 2.4% of all CPU time)
2020-01-12 12:02:47 -05:00
|
|
|
self->level = 1 << 15;
|
2019-08-24 08:56:12 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiomixer_mixervoice_set_parent(audiomixer_mixervoice_obj_t *self, audiomixer_mixer_obj_t *parent) {
|
|
|
|
self->parent = parent;
|
2018-11-20 14:14:22 -05:00
|
|
|
}
|
2018-11-15 14:11:45 -05:00
|
|
|
|
2023-04-05 09:51:55 -04:00
|
|
|
mp_float_t common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t *self) {
|
|
|
|
return (mp_float_t)self->level / (1 << 15);
|
2019-03-12 17:10:54 -04:00
|
|
|
}
|
|
|
|
|
2023-04-05 09:51:55 -04:00
|
|
|
void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t *self, mp_float_t level) {
|
|
|
|
self->level = (uint16_t)(level * (1 << 15));
|
2018-11-15 14:11:45 -05:00
|
|
|
}
|
2018-11-20 14:14:22 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiomixer_mixervoice_play(audiomixer_mixervoice_obj_t *self, mp_obj_t sample, bool loop) {
|
2018-11-20 14:14:22 -05:00
|
|
|
if (audiosample_sample_rate(sample) != self->parent->sample_rate) {
|
|
|
|
mp_raise_ValueError(translate("The sample's sample rate does not match the mixer's"));
|
|
|
|
}
|
|
|
|
if (audiosample_channel_count(sample) != self->parent->channel_count) {
|
|
|
|
mp_raise_ValueError(translate("The sample's channel count does not match the mixer's"));
|
|
|
|
}
|
|
|
|
if (audiosample_bits_per_sample(sample) != self->parent->bits_per_sample) {
|
|
|
|
mp_raise_ValueError(translate("The sample's bits_per_sample does not match the mixer's"));
|
|
|
|
}
|
|
|
|
bool single_buffer;
|
|
|
|
bool samples_signed;
|
|
|
|
uint32_t max_buffer_length;
|
|
|
|
uint8_t spacing;
|
|
|
|
audiosample_get_buffer_structure(sample, false, &single_buffer, &samples_signed,
|
2021-03-15 09:57:36 -04:00
|
|
|
&max_buffer_length, &spacing);
|
2018-11-20 14:14:22 -05:00
|
|
|
if (samples_signed != self->parent->samples_signed) {
|
|
|
|
mp_raise_ValueError(translate("The sample's signedness does not match the mixer's"));
|
|
|
|
}
|
|
|
|
self->sample = sample;
|
|
|
|
self->loop = loop;
|
|
|
|
|
|
|
|
audiosample_reset_buffer(sample, false, 0);
|
2021-03-15 09:57:36 -04:00
|
|
|
audioio_get_buffer_result_t result = audiosample_get_buffer(sample, false, 0, (uint8_t **)&self->remaining_buffer, &self->buffer_length);
|
2018-11-20 14:14:22 -05:00
|
|
|
// Track length in terms of words.
|
|
|
|
self->buffer_length /= sizeof(uint32_t);
|
|
|
|
self->more_data = result == GET_BUFFER_MORE_DATA;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_audiomixer_mixervoice_get_playing(audiomixer_mixervoice_obj_t *self) {
|
|
|
|
return self->sample != NULL;
|
2018-11-20 14:14:22 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiomixer_mixervoice_stop(audiomixer_mixervoice_obj_t *self) {
|
2018-11-20 14:14:22 -05:00
|
|
|
self->sample = NULL;
|
|
|
|
}
|