circuitpython/shared-module/audiomixer/MixerVoice.c

89 lines
3.7 KiB
C
Raw Normal View History

2018-11-15 14:11:45 -05:00
/*
* This file is part of the Micro Python project, http://micropython.org/
2018-11-15 14:11:45 -05: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
*/
#include "shared-bindings/audiomixer/Mixer.h"
#include "shared-bindings/audiomixer/MixerVoice.h"
#include "shared-module/audiomixer/MixerVoice.h"
2018-11-15 14:11:45 -05:00
#include <stdint.h>
#include "py/runtime.h"
#include "shared-module/audiomixer/__init__.h"
#include "shared-module/audiocore/RawSample.h"
2018-11-20 14:14:22 -05:00
void common_hal_audiomixer_mixervoice_construct(audiomixer_mixervoice_obj_t *self) {
2019-08-24 08:56:12 -04:00
self->sample = NULL;
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
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
}
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;
}