summaryrefslogtreecommitdiff
path: root/shared-module/audiomixer
diff options
context:
space:
mode:
authorsommersoft <sommersoft@gmail.com>2019-08-24 23:36:18 -0500
committersommersoft <sommersoft@gmail.com>2019-08-24 23:36:18 -0500
commitdf5568d993ad10bb6ef68fc0f01f8561f3a6c78d (patch)
treeb159b1273d698ea36fe28aefab7b0392baf159f7 /shared-module/audiomixer
parentb54fd961cb20333c1db0c84b7f61467681911554 (diff)
move Mixer & MixerVoice from 'audiocore' to 'audiomixer'
Diffstat (limited to 'shared-module/audiomixer')
-rw-r--r--shared-module/audiomixer/Mixer.c430
-rw-r--r--shared-module/audiomixer/Mixer.h69
-rw-r--r--shared-module/audiomixer/MixerVoice.c87
-rw-r--r--shared-module/audiomixer/MixerVoice.h46
-rw-r--r--shared-module/audiomixer/__init__.c0
-rw-r--r--shared-module/audiomixer/__init__.h32
6 files changed, 664 insertions, 0 deletions
diff --git a/shared-module/audiomixer/Mixer.c b/shared-module/audiomixer/Mixer.c
new file mode 100644
index 000000000..0ae7c7b09
--- /dev/null
+++ b/shared-module/audiomixer/Mixer.c
@@ -0,0 +1,430 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
+ * 2018 DeanM for Adafruit Industries
+ * 2019 Michael Schroeder
+ *
+ * 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.
+ */
+
+#include "shared-bindings/audiomixer/Mixer.h"
+#include "shared-bindings/audiomixer/MixerVoice.h"
+
+#include <stdint.h>
+
+#include "py/runtime.h"
+#include "shared-module/audiocore/__init__.h"
+#include "shared-module/audiocore/RawSample.h"
+
+void common_hal_audiomixer_mixer_construct(audiomixer_mixer_obj_t* self,
+ uint8_t voice_count,
+ uint32_t buffer_size,
+ uint8_t bits_per_sample,
+ bool samples_signed,
+ uint8_t channel_count,
+ uint32_t sample_rate) {
+ self->len = buffer_size / 2 / sizeof(uint32_t) * sizeof(uint32_t);
+
+ self->first_buffer = m_malloc(self->len, false);
+ if (self->first_buffer == NULL) {
+ common_hal_audiomixer_mixer_deinit(self);
+ mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate first buffer"));
+ }
+
+ self->second_buffer = m_malloc(self->len, false);
+ if (self->second_buffer == NULL) {
+ common_hal_audiomixer_mixer_deinit(self);
+ mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate second buffer"));
+ }
+
+ self->bits_per_sample = bits_per_sample;
+ self->samples_signed = samples_signed;
+ self->channel_count = channel_count;
+ self->sample_rate = sample_rate;
+ self->voice_count = voice_count;
+}
+
+void common_hal_audiomixer_mixer_deinit(audiomixer_mixer_obj_t* self) {
+ self->first_buffer = NULL;
+ self->second_buffer = NULL;
+}
+
+bool common_hal_audiomixer_mixer_deinited(audiomixer_mixer_obj_t* self) {
+ return self->first_buffer == NULL;
+}
+
+uint32_t common_hal_audiomixer_mixer_get_sample_rate(audiomixer_mixer_obj_t* self) {
+ return self->sample_rate;
+}
+
+bool common_hal_audiomixer_mixer_get_playing(audiomixer_mixer_obj_t* self) {
+ for (int32_t v = 0; v < self->voice_count; v++) {
+ if (common_hal_audiomixer_mixervoice_get_playing(MP_OBJ_TO_PTR(self->voice[v]))) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void audiomixer_mixer_reset_buffer(audiomixer_mixer_obj_t* self,
+ bool single_channel,
+ uint8_t channel) {
+#if 0
+ for (int32_t i = 0; i < self->voice_count; i++) {
+ self->voice[i].sample = NULL;
+ }
+#endif
+}
+
+uint32_t add8signed(uint32_t a, uint32_t b) {
+ #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
+ return __QADD8(a, b);
+ #else
+ uint32_t result = 0;
+ for (int8_t i = 0; i < 4; i++) {
+ int8_t ai = a >> (sizeof(int8_t) * 8 * i);
+ int8_t bi = b >> (sizeof(int8_t) * 8 * i);
+ int32_t intermediate = (int32_t) ai + bi;
+ if (intermediate > CHAR_MAX) {
+ intermediate = CHAR_MAX;
+ } else if (intermediate < CHAR_MIN) {
+ intermediate = CHAR_MIN;
+ }
+ result |= (((uint32_t) intermediate) & 0xff) << (sizeof(int8_t) * 8 * i);
+ }
+ return result;
+ #endif
+}
+
+uint32_t add8unsigned(uint32_t a, uint32_t b) {
+ /*#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
+ // Subtract out the DC offset, add and then shift back.
+ a = __USUB8(a, 0x80808080);
+ b = __USUB8(b, 0x80808080);
+ uint32_t sum = __QADD8(a, b);
+ return __UADD8(sum, 0x80808080);
+ #else*/
+ uint32_t result = 0;
+ for (int8_t i = 0; i < 4; i++) {
+ uint8_t ai = (a >> (sizeof(uint8_t) * 8 * i));
+ uint8_t bi = (b >> (sizeof(uint8_t) * 8 * i));
+ int32_t intermediate = (int32_t) (ai + bi) / 2;
+ if (intermediate > UCHAR_MAX) {
+ intermediate = UCHAR_MAX;
+ }
+ result |= ((uint32_t) intermediate & 0xff) << (sizeof(uint8_t) * 8 * i);
+ }
+ return result;
+ //#endif
+}
+
+uint32_t add16signed(uint32_t a, uint32_t b) {
+ /*#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
+ return __QADD16(a, b);
+ #else*/
+ uint32_t result = 0;
+ for (int8_t i = 0; i < 2; i++) {
+ int16_t ai = a >> (sizeof(int16_t) * 8 * i);
+ int16_t bi = b >> (sizeof(int16_t) * 8 * i);
+ int32_t intermediate = (int32_t) ai + bi;
+ if (intermediate > SHRT_MAX) {
+ intermediate = SHRT_MAX;
+ } else if (intermediate < SHRT_MIN) {
+ intermediate = SHRT_MIN;
+ }
+ result |= (((uint32_t) intermediate) & 0xffff) << (sizeof(int16_t) * 8 * i);
+ }
+ return result;
+ //#endif
+}
+
+uint32_t add16unsigned(uint32_t a, uint32_t b) {
+ #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
+ // Subtract out the DC offset, add and then shift back.
+ a = __USUB16(a, 0x80008000);
+ b = __USUB16(b, 0x80008000);
+ uint32_t sum = __QADD16(a, b);
+ return __UADD16(sum, 0x80008000);
+ #else
+ uint32_t result = 0;
+ for (int8_t i = 0; i < 2; i++) {
+ int16_t ai = (a >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
+ int16_t bi = (b >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
+ int32_t intermediate = (int32_t) ai + bi;
+ if (intermediate > USHRT_MAX) {
+ intermediate = USHRT_MAX;
+ }
+ result |= ((uint16_t) intermediate + 0x8000) << (sizeof(int16_t) * 8 * i);
+ }
+ return result;
+ #endif
+}
+
+//TODO:
+static inline uint32_t mult8unsigned(uint32_t val, int32_t mul) {
+ // if mul == 0, no need in wasting cycles
+ if (mul == 0) {
+ return 0;
+ }
+ /*#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
+ return val;
+ #else*/
+ uint32_t result = 0;
+ float mod_mul = (float) mul / (float) ((1<<15)-1);
+ for (int8_t i = 0; i < 4; i++) {
+ uint8_t ai = val >> (sizeof(uint8_t) * 8 * i);
+ int32_t intermediate = ai * mod_mul;
+ if (intermediate > SHRT_MAX) {
+ intermediate = SHRT_MAX;
+ }
+ result |= ((uint32_t) intermediate & 0xff) << (sizeof(uint8_t) * 8 * i);
+ }
+
+ return result;
+ //#endif
+}
+
+//TODO:
+static inline uint32_t mult8signed(uint32_t val, int32_t mul) {
+ // if mul == 0, no need in wasting cycles
+ if (mul == 0) {
+ return 0;
+ }
+ /*
+ #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
+ return val;
+ #else
+ */
+ uint32_t result = 0;
+ float mod_mul = (float)mul / (float)((1<<15)-1);
+ for (int8_t i = 0; i < 4; i++) {
+ int16_t ai = val >> (sizeof(int8_t) * 8 * i);
+ int32_t intermediate = ai * mod_mul;
+ if (intermediate > CHAR_MAX) {
+ intermediate = CHAR_MAX;
+ } else if (intermediate < CHAR_MIN) {
+ intermediate = CHAR_MIN;
+ }
+ result |= (((uint32_t) intermediate) & 0xff) << (sizeof(int16_t) * 8 * i);
+ }
+ return result;
+ //#endif
+}
+
+//TODO:
+static inline uint32_t mult16unsigned(uint32_t val, int32_t mul) {
+ // if mul == 0, no need in wasting cycles
+ if (mul == 0) {
+ return 0;
+ }
+ /*
+ #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
+ return val;
+ #else
+ */
+ uint32_t result = 0;
+ float mod_mul = (float)mul / (float)((1<<15)-1);
+ for (int8_t i = 0; i < 2; i++) {
+ int16_t ai = (val >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
+ int32_t intermediate = ai * mod_mul;
+ if (intermediate > SHRT_MAX) {
+ intermediate = SHRT_MAX;
+ } else if (intermediate < SHRT_MIN) {
+ intermediate = SHRT_MIN;
+ }
+ result |= (((uint32_t) intermediate) + 0x8000) << (sizeof(int16_t) * 8 * i);
+ }
+ return val;
+ //#endif
+}
+
+static inline uint32_t mult16signed(uint32_t val, int32_t mul) {
+ // if mul == 0, no need in wasting cycles
+ if (mul == 0) {
+ return 0;
+ }
+ #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
+ int32_t hi, lo;
+ int32_t bits = 16; // saturate to 16 bits
+ int32_t shift = 0; // shift is done automatically
+ asm volatile("smulwb %0, %1, %2" : "=r" (lo) : "r" (mul), "r" (val));
+ asm volatile("smulwt %0, %1, %2" : "=r" (hi) : "r" (mul), "r" (val));
+ asm volatile("ssat %0, %1, %2, asr %3" : "=r" (lo) : "I" (bits), "r" (lo), "I" (shift));
+ asm volatile("ssat %0, %1, %2, asr %3" : "=r" (hi) : "I" (bits), "r" (hi), "I" (shift));
+ asm volatile("pkhbt %0, %1, %2, lsl #16" : "=r" (val) : "r" (lo), "r" (hi)); // pack
+ return val;
+ #else
+ uint32_t result = 0;
+ float mod_mul = (float)mul / (float)((1<<15)-1);
+ for (int8_t i = 0; i < 2; i++) {
+ int16_t ai = val >> (sizeof(int16_t) * 8 * i);
+ int32_t intermediate = ai * mod_mul;
+ if (intermediate > SHRT_MAX) {
+ intermediate = SHRT_MAX;
+ } else if (intermediate < SHRT_MIN) {
+ intermediate = SHRT_MIN;
+ }
+ result |= (((uint32_t) intermediate) & 0xffff) << (sizeof(int16_t) * 8 * i);
+ }
+ return result;
+ #endif
+}
+
+audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t* self,
+ bool single_channel,
+ uint8_t channel,
+ uint8_t** buffer,
+ uint32_t* buffer_length) {
+ if (!single_channel) {
+ channel = 0;
+ }
+
+ uint32_t channel_read_count = self->left_read_count;
+ if (channel == 1) {
+ channel_read_count = self->right_read_count;
+ }
+ *buffer_length = self->len;
+
+ bool need_more_data = self->read_count == channel_read_count;
+ if (need_more_data) {
+ uint32_t* word_buffer;
+ if (self->use_first_buffer) {
+ *buffer = (uint8_t*) self->first_buffer;
+ word_buffer = self->first_buffer;
+ } else {
+ *buffer = (uint8_t*) self->second_buffer;
+ word_buffer = self->second_buffer;
+ }
+ self->use_first_buffer = !self->use_first_buffer;
+ bool voices_active = false;
+ for (int32_t v = 0; v < self->voice_count; v++) {
+ audiomixer_mixervoice_obj_t* voice = MP_OBJ_TO_PTR(self->voice[v]);
+
+ uint32_t j = 0;
+ bool voice_done = voice->sample == NULL;
+ for (uint32_t i = 0; i < self->len / sizeof(uint32_t); i++) {
+ if (!voice_done && j >= voice->buffer_length) {
+ if (!voice->more_data) {
+ if (voice->loop) {
+ audiosample_reset_buffer(voice->sample, false, 0);
+ } else {
+ voice->sample = NULL;
+ voice_done = true;
+ }
+ }
+ if (!voice_done) {
+ // Load another buffer
+ audioio_get_buffer_result_t result = audiosample_get_buffer(voice->sample, false, 0, (uint8_t**) &voice->remaining_buffer, &voice->buffer_length);
+ // Track length in terms of words.
+ voice->buffer_length /= sizeof(uint32_t);
+ voice->more_data = result == GET_BUFFER_MORE_DATA;
+ j = 0;
+ }
+ }
+ // First active voice gets copied over verbatim.
+ uint32_t sample_value;
+ if (voice_done) {
+ // Exit early if another voice already set all samples once.
+ if (voices_active) {
+ continue;
+ }
+ sample_value = 0;
+ if (!self->samples_signed) {
+ if (self->bits_per_sample == 8) {
+ sample_value = 0x7f7f7f7f;
+ } else {
+ sample_value = 0x7fff7fff;
+ }
+ }
+ } else {
+ sample_value = voice->remaining_buffer[j];
+ }
+
+ // apply the mixer level
+ if (!self->samples_signed) {
+ if (self->bits_per_sample == 8) {
+ sample_value = mult8unsigned(sample_value, voice->level);
+ } else {
+ sample_value = mult16unsigned(sample_value, voice->level);
+ }
+ } else {
+ if (self->bits_per_sample == 8) {
+ sample_value = mult8signed(sample_value, voice->level);
+ } else {
+ sample_value = mult16signed(sample_value, voice->level);
+ }
+ }
+
+ if (!voices_active) {
+ word_buffer[i] = sample_value;
+ } else {
+ if (self->bits_per_sample == 8) {
+ if (self->samples_signed) {
+ word_buffer[i] = add8signed(word_buffer[i], sample_value);
+ } else {
+ word_buffer[i] = add8unsigned(word_buffer[i], sample_value);
+ }
+ } else {
+ if (self->samples_signed) {
+ word_buffer[i] = add16signed(word_buffer[i], sample_value);
+ } else {
+ word_buffer[i] = add16unsigned(word_buffer[i], sample_value);
+ }
+ }
+ }
+ j++;
+ }
+ voice->buffer_length -= j;
+ voice->remaining_buffer += j;
+
+ voices_active = true;
+ }
+
+ self->read_count += 1;
+ } else if (!self->use_first_buffer) {
+ *buffer = (uint8_t*) self->first_buffer;
+ } else {
+ *buffer = (uint8_t*) self->second_buffer;
+ }
+
+
+ if (channel == 0) {
+ self->left_read_count += 1;
+ } else if (channel == 1) {
+ self->right_read_count += 1;
+ *buffer = *buffer + self->bits_per_sample / 8;
+ }
+ return GET_BUFFER_MORE_DATA;
+}
+
+void audiomixer_mixer_get_buffer_structure(audiomixer_mixer_obj_t* self, bool single_channel,
+ bool* single_buffer, bool* samples_signed,
+ uint32_t* max_buffer_length, uint8_t* spacing) {
+ *single_buffer = false;
+ *samples_signed = self->samples_signed;
+ *max_buffer_length = self->len;
+ if (single_channel) {
+ *spacing = self->channel_count;
+ } else {
+ *spacing = 1;
+ }
+}
diff --git a/shared-module/audiomixer/Mixer.h b/shared-module/audiomixer/Mixer.h
new file mode 100644
index 000000000..ab4780efc
--- /dev/null
+++ b/shared-module/audiomixer/Mixer.h
@@ -0,0 +1,69 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Scott Shawcroft
+ *
+ * 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.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER_MIXER_H
+#define MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER_MIXER_H
+
+#include "py/obj.h"
+#include "py/objtuple.h"
+
+#include "shared-module/audiocore/__init__.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint32_t* first_buffer;
+ uint32_t* second_buffer;
+ uint32_t len; // in words
+ uint8_t bits_per_sample;
+ bool use_first_buffer;
+ bool samples_signed;
+ uint8_t channel_count;
+ uint32_t sample_rate;
+
+ uint32_t read_count;
+ uint32_t left_read_count;
+ uint32_t right_read_count;
+
+ uint8_t voice_count;
+ mp_obj_tuple_t *voice_tuple;
+ mp_obj_t voice[];
+} audiomixer_mixer_obj_t;
+
+
+// These are not available from Python because it may be called in an interrupt.
+void audiomixer_mixer_reset_buffer(audiomixer_mixer_obj_t* self,
+ bool single_channel,
+ uint8_t channel);
+audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t* self,
+ bool single_channel,
+ uint8_t channel,
+ uint8_t** buffer,
+ uint32_t* buffer_length); // length in bytes
+void audiomixer_mixer_get_buffer_structure(audiomixer_mixer_obj_t* self, bool single_channel,
+ bool* single_buffer, bool* samples_signed,
+ uint32_t* max_buffer_length, uint8_t* spacing);
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER_MIXER_H
diff --git a/shared-module/audiomixer/MixerVoice.c b/shared-module/audiomixer/MixerVoice.c
new file mode 100644
index 000000000..ff05dc93e
--- /dev/null
+++ b/shared-module/audiomixer/MixerVoice.c
@@ -0,0 +1,87 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * 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.
+ */
+#include "shared-bindings/audiomixer/Mixer.h"
+#include "shared-module/audiomixer/MixerVoice.h"
+
+#include <stdint.h>
+
+#include "py/runtime.h"
+#include "shared-module/audiomixer/__init__.h"
+#include "shared-module/audiocore/RawSample.h"
+
+void common_hal_audiomixer_mixervoice_construct(audiomixer_mixervoice_obj_t *self) {
+ self->sample = NULL;
+ self->level = ((1 << 15) - 1);
+}
+
+void common_hal_audiomixer_mixervoice_set_parent(audiomixer_mixervoice_obj_t* self, audiomixer_mixer_obj_t *parent) {
+ self->parent = parent;
+}
+
+float common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t* self) {
+ return ((float) self->level / ((1 << 15) - 1));
+}
+
+void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t* self, float level) {
+ self->level = level * ((1 << 15)-1);
+}
+
+void common_hal_audiomixer_mixervoice_play(audiomixer_mixervoice_obj_t* self, mp_obj_t sample, bool loop) {
+ 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,
+ &max_buffer_length, &spacing);
+ 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);
+ audioio_get_buffer_result_t result = audiosample_get_buffer(sample, false, 0, (uint8_t**) &self->remaining_buffer, &self->buffer_length);
+ // Track length in terms of words.
+ self->buffer_length /= sizeof(uint32_t);
+ self->more_data = result == GET_BUFFER_MORE_DATA;
+}
+
+bool common_hal_audiomixer_mixervoice_get_playing(audiomixer_mixervoice_obj_t* self) {
+ return self->sample != NULL;
+}
+
+void common_hal_audiomixer_mixervoice_stop(audiomixer_mixervoice_obj_t* self) {
+ self->sample = NULL;
+}
diff --git a/shared-module/audiomixer/MixerVoice.h b/shared-module/audiomixer/MixerVoice.h
new file mode 100644
index 000000000..efac19156
--- /dev/null
+++ b/shared-module/audiomixer/MixerVoice.h
@@ -0,0 +1,46 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * 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.
+ */
+#ifndef SHARED_MODULE_AUDIOMIXER_MIXERVOICE_H_
+#define SHARED_MODULE_AUDIOMIXER_MIXERVOICE_H_
+
+#include "py/obj.h"
+
+#include "shared-module/audiomixer/__init__.h"
+#include "shared-module/audiomixer/Mixer.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ audiomixer_mixer_obj_t *parent;
+ mp_obj_t sample;
+ bool loop;
+ bool more_data;
+ uint32_t* remaining_buffer;
+ uint32_t buffer_length;
+ int16_t level;
+} audiomixer_mixervoice_obj_t;
+
+
+#endif /* SHARED_MODULE_AUDIOMIXER_MIXERVOICE_H_ */
diff --git a/shared-module/audiomixer/__init__.c b/shared-module/audiomixer/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/audiomixer/__init__.c
diff --git a/shared-module/audiomixer/__init__.h b/shared-module/audiomixer/__init__.h
new file mode 100644
index 000000000..35b731558
--- /dev/null
+++ b/shared-module/audiomixer/__init__.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Dan Halbert 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.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER__INIT__H
+#define MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER__INIT__H
+
+#include "shared-module/audiocore/__init__.h"
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER__INIT__H