circuitpython/ports/mimxrt10xx/common-hal/audiobusio/__init__.h
Jeff Epler c6bc9c48c9
mimxrt10xx: implement i2sout
tested on metro m7 (green prototype version) with max98357a i2s amplifier and the following test code:
```py
import board
import time
import digitalio
from audiobusio import I2SOut
from audiocore import RawSample
from microcontroller import pin
from ulab import numpy as np

n = np.array(np.sin(np.linspace(0, np.pi*2, 218, endpoint=False)) * 200, dtype=np.int16)
print(n)
r = RawSample(n, sample_rate=8000, channel_count=2)
def main():
    with digitalio.DigitalInOut(board.LED) as l:
        l.switch_to_output(True)
        value = False
        while True:
            with I2SOut(pin.GPIO_06, pin.GPIO_07, pin.GPIO_04) as i:
                time.sleep(.01)
                l.value = value = not value
                i.play(r, loop=True)
                print(i.playing)
                time.sleep(.5)
                i.stop()
                print("STOPPED")
                print(i.playing)
                time.sleep(.5)
                i.play(r, loop=True)
                print(i.playing)
                print("PLAY AGAIN")
                time.sleep(.5)
            time.sleep(1)
```

Only stereo, 16-bit, raw samples were tested; the sample rate is actually fixed
at 48kHz in the core right now. There is more to do, but the basics work.

# Conflicts:
#	ports/mimxrt10xx/Makefile
#	ports/mimxrt10xx/mpconfigport.mk
2023-03-22 12:15:25 -05:00

60 lines
2.1 KiB
C

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Jeff Epler 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.
*/
#pragma once
#include "sdk/drivers/sai/fsl_sai.h"
#include "py/obj.h"
#include "supervisor/background_callback.h"
typedef struct {
I2S_Type *peripheral;
sai_handle_t handle;
mp_obj_t sample;
uint32_t *buffers[SAI_XFER_QUEUE_SIZE];
uint8_t *sample_data, *sample_end;
background_callback_t callback;
bool playing, paused, loop, stopping;
bool samples_signed;
uint8_t channel_count, bytes_per_sample;
uint8_t buffer_idx;
uint32_t sample_rate;
} i2s_t;
void i2s_reset(void);
void port_i2s_initialize(i2s_t *self, int instance, sai_transceiver_t *config);
void port_i2s_deinit(i2s_t *self);
bool port_i2s_deinited(i2s_t *self);
void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop);
void port_i2s_stop(i2s_t *self);
bool port_i2s_get_playing(i2s_t *self);
bool port_i2s_get_paused(i2s_t *self);
void port_i2s_pause(i2s_t *self);
void port_i2s_resume(i2s_t *self);