Developer World
Spresense Arduino Library v3.3.0-b2e6327
Audio.h
Go to the documentation of this file.
1/*
2 * Audio.h - Audio include file for the Spresense SDK
3 * Copyright 2018 Sony Semiconductor Solutions Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
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 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
29#ifndef Audio_h
30#define Audio_h
31
32#ifdef SUBCORE
33#error "Audio library is NOT supported by SubCore."
34#endif
35
42#include <pins_arduino.h>
43
44class File;
45
46// #ifdef __cplusplus
47
48#include <audio/audio_high_level_api.h>
49#include <audio/utilities/wav_containerformat.h>
50#include <audio/utilities/wav_containerformat_parser.h>
51#include <audio/utilities/frame_samples.h>
52#include <memutils/simple_fifo/CMN_SimpleFifo.h>
53
54#define WRITE_FIFO_FRAME_NUM (8)
55#define WRITE_FIFO_FRAME_SIZE (1024*2*3)
56#define WRITE_BUF_SIZE (WRITE_FIFO_FRAME_NUM * WRITE_FIFO_FRAME_SIZE)
57
58#define READ_FIFO_FRAME_NUM (10)
59#define READ_FIFO_FRAME_SIZE (1024*2*8)
60#define READ_BUF_SIZE (READ_FIFO_FRAME_NUM * READ_FIFO_FRAME_SIZE)
61
62#define FIFO_FRAME_SIZE (\
63 (WRITE_BUF_SIZE > READ_BUF_SIZE) ?\
64 (WRITE_FIFO_FRAME_SIZE) : (READ_FIFO_FRAME_SIZE)\
65 )
66
67#define SIMPLE_FIFO_BUF_SIZE (\
68 ((WRITE_BUF_SIZE) > (READ_BUF_SIZE)) ? \
69 (WRITE_BUF_SIZE) : (READ_BUF_SIZE)\
70 )
71
72extern "C" void outputDeviceCallback(uint32_t);
73
74/*--------------------------------------------------------------------------*/
75#define print_err printf
76
77#ifdef BRD_DEBUG
78#define print_dbg(...) printf(__VA_ARGS__)
79#else
80#define print_dbg(x...)
81#endif
82
83/*--------------------------------------------------------------------------*/
84
85#define AS_CODECTYPE_PCM 5
86
87/*--------------------------------------------------------------------------*/
91#define AUDIOLIB_ECODE_OK 0
92#define AUDIOLIB_ECODE_SHARED_MEMORY_ERROR 1
93#define AUDIOLIB_ECODE_SIMPLEFIFO_ERROR 2
94#define AUDIOLIB_ECODE_AUDIOCOMMAND_ERROR 3
95#define AUDIOLIB_ECODE_FILEACCESS_ERROR 4
96#define AUDIOLIB_ECODE_FILEEND 5
97#define AUDIOLIB_ECODE_BUFFER_AREA_ERROR 6
98#define AUDIOLIB_ECODE_BUFFER_SIZE_ERROR 7
99#define AUDIOLIB_ECODE_INSUFFICIENT_BUFFER_AREA 8
100#define AUDIOLIB_ECODE_WAV_PARSE_ERROR 9
101#define AUDIOLIB_ECODE_PARAMETER_ERROR 10
103/*--------------------------------------------------------------------------*/
108typedef unsigned int err_t;
109
110/*--------------------------------------------------------------------------*/
111
117{
118public:
119
124 {
125 static AudioClass instance;
126 return &instance;
127 }
128
136 typedef enum
137 {
139 Player1
141
155
163 err_t begin(AudioAttentionCb attcb);
164
178 err_t end(void);
179
193 uint8_t device
195 );
196
205 uint8_t device,
207 uint32_t player0bufsize,
208 uint32_t player1bufsize
209 );
210
220 uint8_t device,
222 uint8_t sp_drv
224 );
225
234 uint8_t device,
236 uint8_t sp_drv,
238 uint32_t player0bufsize,
239 uint32_t player1bufsize
240 );
241
263 uint8_t input_device
264 );
265
275 uint8_t input_device,
276 int32_t input_gain
280 );
281
290 uint8_t input_device,
291 int32_t input_gain,
295 uint32_t bufsize
296 );
297
306 uint8_t input_device,
307 int32_t input_gain,
311 uint32_t bufsize,
312 bool is_digital
313 );
314
321 typedef enum
322 {
325 BothIn
327
334 typedef enum
335 {
338 Mic
340
357 ThroughInput input,
358 ThroughI2sOut i2s_out,
359 bool sp_out,
360 int32_t input_gain,
364 uint8_t sp_drv
366 );
367
381
400 PlayerId id,
401 uint8_t codec,
402 uint32_t fs,
403 uint8_t channel
404 );
405
424 PlayerId id,
425 uint8_t codec,
426 uint32_t fs,
427 uint8_t bitlen,
428 uint8_t channel
429 );
430
449 PlayerId id,
450 uint8_t codec,
451 const char *codec_path,
452 uint32_t fs,
453 uint8_t channel
454 );
455
474 PlayerId id,
475 uint8_t codec,
476 const char *codec_path,
477 uint32_t fs,
478 uint8_t bitlen,
479 uint8_t channel
480 );
481
500 uint8_t codec,
501 uint32_t fs,
502 uint8_t channel
503 );
504
523 uint8_t codec,
524 uint32_t fs,
525 uint8_t bitlen,
526 uint8_t channel
527 );
528
547 uint8_t codec,
548 const char *codec_path,
549 uint32_t fs,
550 uint8_t channel
551 );
552
571 uint8_t codec,
572 const char *codec_path,
573 uint32_t fs,
574 uint8_t bitlen,
575 uint8_t channel
576 );
577
594 PlayerId id
595 );
596
608
621 PlayerId id
622 );
623
635 PlayerId id,
636 uint8_t mode
637 );
638
650
660 char enable,
661 short volume,
662 short frequency
663 );
664
673 int volume
674 );
675
684 int master,
685 int player0,
686 int player1
687 );
688
698 PlayerId id,
699 unsigned char l_gain,
700 unsigned char r_gain
701 );
702
720 PlayerId id,
721 File& myfile
722 );
723
739 PlayerId id,
740 int fd
741 );
742
759 PlayerId id,
760 uint8_t *data,
761 uint32_t write_size
762 );
763
775 File& myFile
776 );
777
790 File& myFile
791 );
792
802 File& myFile
803 );
804
805
821 char* p_buffer,
822 uint32_t buffer_size,
823 uint32_t* read_size
824 );
825
843 AsClkMode mode
844 );
845
853 return m_es_size;
854 }
855
856private:
857
862 AudioClass()
863 : m_player0_simple_fifo_buf(NULL)
864 , m_player1_simple_fifo_buf(NULL)
865 , m_attention_callback(NULL)
866 {}
867 AudioClass(const AudioClass&);
868 AudioClass& operator=(const AudioClass&);
869 ~AudioClass() {}
870
871 char m_es_player0_buf[FIFO_FRAME_SIZE];
872 char m_es_player1_buf[WRITE_FIFO_FRAME_SIZE];
873
874 CMN_SimpleFifoHandle m_player0_simple_fifo_handle;
875 CMN_SimpleFifoHandle m_player1_simple_fifo_handle;
876 uint32_t *m_player0_simple_fifo_buf;
877 uint32_t *m_player1_simple_fifo_buf;
878
879 AsPlayerInputDeviceHdlrForRAM m_player0_input_device_handler;
880 AsPlayerInputDeviceHdlrForRAM m_player1_input_device_handler;
881
882 AsRecorderOutputDeviceHdlr m_output_device_handler;
883 int m_es_size;
884 WAVHEADER m_wav_format;
885 int m_codec_type;
886
887 AudioAttentionCb m_attention_callback;
888
889 /* Private Functions */
890
891 /* Functions for initialization on begin/end */
892 err_t begin_manager(void);
893 err_t begin_player(void);
894 err_t begin_recorder(void);
895
896 err_t end_manager(void);
897 err_t end_player(void);
898 err_t end_recorder(void);
899
900 err_t activateAudio(void);
901
902 err_t powerOn(void);
903 err_t powerOff(void);
904
905 /* Functions for initialization Encoder */
906
907 err_t initMicFrontend(uint8_t ch_num, uint8_t bit_length, uint16_t sample);
908 err_t init_recorder_wav(AudioCommand* command, uint32_t sampling_rate, uint8_t bit_length, uint8_t channel_number);
909 err_t init_recorder_mp3(AudioCommand* command, uint32_t sampling_rate, uint8_t bit_length, uint8_t channel_number);
910 err_t init_recorder_opus(AudioCommand* command, uint32_t sampling_rate, uint8_t bit_length, uint8_t channel_number);
911 err_t init_recorder_pcm(AudioCommand* command, uint32_t sampling_rate, uint8_t bit_length, uint8_t channel_number);
912
913 /* Functions for initialization on player mode. */
914 err_t set_output(uint8_t device, uint8_t sp_drv);
915
916 err_t write_fifo(int, char*, uint32_t, CMN_SimpleFifoHandle*);
917 err_t write_fifo(File&, char*, uint32_t, CMN_SimpleFifoHandle*);
918
919 /* Functions for initialization on recorder mode. */
920 err_t set_mic_map(uint8_t map[AS_MIC_CHANNEL_MAX]);
921 err_t init_mic_gain(int, int);
922
923 /* Functions for initialization on through mode. */
924 err_t send_set_through(void);
925
926 bool check_decode_dsp(uint8_t codec_type, const char *path);
927 bool check_encode_dsp(uint8_t codec_type, const char *path, uint32_t fs);
928};
929
930extern AudioClass Audio;
931
934// #endif //__cplusplus
935#endif //Audio_h
936
Audio Library Class Definitions.
Definition: Audio.h:117
err_t setPlayerMode(uint8_t device, uint32_t player0bufsize, uint32_t player1bufsize)
Set Audio Library Mode to Music Player.
err_t setPlayerMode(uint8_t device, uint8_t sp_drv)
Set Audio Library Mode to Music Player.
err_t closeOutputFile(File &myFile)
Close Outputfile.
err_t initRecorder(uint8_t codec, const char *codec_path, uint32_t fs, uint8_t bitlen, uint8_t channel)
Initialize recorder.
err_t stopPlayer(PlayerId id)
Stop Player.
err_t setRecorderMode(uint8_t input_device, int32_t input_gain, uint32_t bufsize)
Set Audio Library Mode to Sound Recorder.
err_t setVolume(int volume)
Set Player Volume.
err_t writeFrames(PlayerId id, File &myfile)
Write Stream Data from a file to FIFO by some frames.(now 5 frames)
err_t setPlayerMode(uint8_t device)
Set Audio Library Mode to Music Player.
err_t setBeep(char enable, short volume, short frequency)
Set Beep Sound.
int getRecordingSize()
Get recording ES size.
Definition: Audio.h:852
err_t stopRecorder(void)
Stop Recorder.
err_t startPlayer(PlayerId id)
Start Player.
err_t end(void)
Finalization the audio library and HW modules.
err_t initRecorder(uint8_t codec, const char *codec_path, uint32_t fs, uint8_t channel)
Initialize recorder.
err_t setPlayerMode(uint8_t device, uint8_t sp_drv, uint32_t player0bufsize, uint32_t player1bufsize)
Set Audio Library Mode to Music Player.
err_t setLRgain(PlayerId id, unsigned char l_gain, unsigned char r_gain)
Set Player L/R Gain.
err_t begin(AudioAttentionCb attcb)
Initialize the audio library and HW modules.
PlayerId
Audio library allows you to use two players simultaneously. Please set Player ID that player instance...
Definition: Audio.h:137
@ Player1
Definition: Audio.h:139
@ Player0
Definition: Audio.h:138
err_t startRecorder(void)
Start Recorder.
err_t writeWavHeader(File &myFile)
Write WAV Header.
err_t readFrames(char *p_buffer, uint32_t buffer_size, uint32_t *read_size)
Read Stream Data from FIFO to a file by some frames.(now 5 frames)
err_t initPlayer(PlayerId id, uint8_t codec, const char *codec_path, uint32_t fs, uint8_t channel)
Initialize player.
err_t stopPlayer(PlayerId id, uint8_t mode)
Stop Player (mode specified)
ThroughInput
Input select parameter at baseband through mode.
Definition: Audio.h:322
@ BothIn
Definition: Audio.h:325
@ I2sIn
Definition: Audio.h:324
@ MicIn
Definition: Audio.h:323
err_t begin(void)
Initialize the audio library and HW modules.
err_t setRecorderMode(uint8_t input_device, int32_t input_gain, uint32_t bufsize, bool is_digital)
Set Audio Library Mode to Sound Recorder.
err_t setRecorderMode(uint8_t input_device, int32_t input_gain)
Set Audio Library Mode to Sound Recorder.
err_t initRecorder(uint8_t codec, uint32_t fs, uint8_t channel)
Initialize recorder.
err_t setReadyMode(void)
Set Audio Library Mode to Ready.
err_t setRenderingClockMode(AsClkMode mode)
Set Rendering clock mode.
err_t setVolume(int master, int player0, int player1)
Set Player Volume.
err_t readFrames(File &myFile)
Read Stream Data from FIFO to a file by some frames.(now 5 frames)
err_t initRecorder(uint8_t codec, uint32_t fs, uint8_t bitlen, uint8_t channel)
Initialize recorder.
err_t setThroughMode(ThroughInput input, ThroughI2sOut i2s_out, bool sp_out, int32_t input_gain, uint8_t sp_drv)
Set Audio Library Mode to BaseBand Through.
static AudioClass * getInstance()
Get instance of AudioClass for singleton.
Definition: Audio.h:123
err_t writeFrames(PlayerId id, uint8_t *data, uint32_t write_size)
Write Stream Data from buffer.
err_t initPlayer(PlayerId id, uint8_t codec, uint32_t fs, uint8_t bitlen, uint8_t channel)
Initialize player.
err_t initPlayer(PlayerId id, uint8_t codec, uint32_t fs, uint8_t channel)
Initialize player.
err_t setRecorderMode(uint8_t input_device)
Set Audio Library Mode to Sound Recorder.
err_t initPlayer(PlayerId id, uint8_t codec, const char *codec_path, uint32_t fs, uint8_t bitlen, uint8_t channel)
Initialize player.
err_t writeFrames(PlayerId id, int fd)
Write Stream Data from a file to FIFO by some frames.(now 5 frames)
ThroughI2sOut
I2S output select parameter at baseband through mode.
Definition: Audio.h:335
@ Mixer
Definition: Audio.h:337
@ Mic
Definition: Audio.h:338
@ None
Definition: Audio.h:336
The File class allows for reading from and writing to individual files on the File System.
Definition: File.h:55
unsigned int err_t
Definition: Audio.h:108