ESP8266
i2s.h
1 /*
2  i2s.h - Software I2S library for esp8266
3 
4  Copyright (c) 2015 Hristo Gochkov. All rights reserved.
5  This file is part of the esp8266 core for Arduino environment.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #ifndef I2S_h
22 #define I2S_h
23 
24 /*
25 How does this work? Basically, to get sound, you need to:
26 - Connect an I2S codec to the I2S pins on the ESP.
27 - Start up a thread that's going to do the sound output
28 - Call i2s_begin()
29 - Call i2s_set_rate() with the sample rate you want.
30 - Generate sound and call i2s_write_sample() with 32-bit samples.
31 The 32bit samples basically are 2 16-bit signed values (the analog values for
32 the left and right channel) concatenated as (Rout<<16)+Lout
33 
34 i2s_write_sample will block when you're sending data too quickly, so you can just
35 generate and push data as fast as you can and i2s_write_sample will regulate the
36 speed.
37 */
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 void i2s_begin();
44 void i2s_end();
45 void i2s_set_rate(uint32_t rate);//Sample Rate in Hz (ex 44100, 48000)
46 bool i2s_write_sample(uint32_t sample);//32bit sample with channels being upper and lower 16 bits (blocking when DMA is full)
47 bool i2s_write_sample_nb(uint32_t sample);//same as above but does not block when DMA is full and returns false instead
48 bool i2s_write_lr(int16_t left, int16_t right);//combines both channels and calls i2s_write_sample with the result
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 
52 #ifdef __cplusplus
53 }
54 #endif
55 
56 #endif