arduino-emulator
All Classes Namespaces Files Functions Enumerations Pages
i2s.h
1 /*
2  i2s.h - Software I2S library for esp8266
3  Copyright (c) 2015 Hristo Gochkov. All rights reserved.
4  This file is part of the esp8266 core for Arduino environment.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #ifndef I2S_h
19 #define I2S_h
20 
21 /*
22 How does this work? Basically, to get sound, you need to:
23 - Connect an I2S codec to the I2S pins on the ESP.
24 - Start up a thread that's going to do the sound output
25 - Call i2s_begin()
26 - Call i2s_set_rate() with the sample rate you want.
27 - Generate sound and call i2s_write_sample() with 32-bit samples.
28 The 32bit samples basically are 2 16-bit signed values (the analog values for
29 the left and right channel) concatenated as (Rout<<16)+Lout
30 i2s_write_sample will block when you're sending data too quickly, so you can just
31 generate and push data as fast as you can and i2s_write_sample will regulate the
32 speed.
33 */
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 void i2s_begin(); // Enable TX only, for compatibility
40 bool i2s_rxtx_begin(bool enableRx, bool enableTx); // Allow TX and/or RX, returns false on OOM error
41 void i2s_end();
42 void i2s_set_rate(uint32_t rate);//Sample Rate in Hz (ex 44100, 48000)
43 void i2s_set_dividers(uint8_t div1, uint8_t div2);//Direct control over output rate
44 float i2s_get_real_rate();//The actual Sample Rate on output
45 bool i2s_write_sample(uint32_t sample);//32bit sample with channels being upper and lower 16 bits (blocking when DMA is full)
46 bool i2s_write_sample_nb(uint32_t sample);//same as above but does not block when DMA is full and returns false instead
47 bool i2s_write_lr(int16_t left, int16_t right);//combines both channels and calls i2s_write_sample with the result
48 bool i2s_read_sample(int16_t *left, int16_t *right, bool blocking); // RX data returned in both 16-bit outputs.
49 bool i2s_is_full();//returns true if DMA is full and can not take more bytes (overflow)
50 bool i2s_is_empty();//returns true if DMA is empty (underflow)
51 bool i2s_rx_is_full();
52 bool i2s_rx_is_empty();
53 uint16_t i2s_available();// returns the number of samples than can be written before blocking
54 uint16_t i2s_rx_available();// returns the number of samples than can be written before blocking
55 void i2s_set_callback(void (*callback) (void));
56 void i2s_rx_set_callback(void (*callback) (void));
57 
58 // writes a buffer of frames into the DMA memory, returns the amount of frames written
59 // A frame is just a int16_t for mono, for stereo a frame is two int16_t, one for each channel.
60 uint16_t i2s_write_buffer_mono(int16_t *frames, uint16_t frame_count);
61 uint16_t i2s_write_buffer_mono_nb(int16_t *frames, uint16_t frame_count);
62 uint16_t i2s_write_buffer(int16_t *frames, uint16_t frame_count);
63 uint16_t i2s_write_buffer_nb(int16_t *frames, uint16_t frame_count);
64 
65 #ifdef __cplusplus
66 }
67 #endif
68 
69 #endif