Developer World
Spresense Arduino Library v2.2.0-1e6eb73
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 
44 class 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 
72 extern "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 /*--------------------------------------------------------------------------*/
104 
108 typedef unsigned int err_t;
109 
110 /*--------------------------------------------------------------------------*/
111 
117 {
118 public:
119 
124  {
125  static AudioClass instance;
126  return &instance;
127  }
128 
136  typedef enum
137  {
140  } PlayerId;
141 
154  err_t begin(void);
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  {
326  } ThroughInput;
327 
334  typedef enum
335  {
339  } ThroughI2sOut;
340 
357  ThroughInput input,
358  ThroughI2sOut i2s_out,
359  bool sp_out,
360  int32_t input_gain,
364  uint8_t sp_drv
366  );
367 
380  err_t setReadyMode(void);
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 
607  err_t startRecorder(void);
608 
621  PlayerId id
622  );
623 
635  PlayerId id,
636  uint8_t mode
637  );
638 
649  err_t stopRecorder(void);
650 
659  err_t setBeep(
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 
856 private:
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 
930 extern AudioClass Audio;
931 
934 // #endif //__cplusplus
935 #endif //Audio_h
936 
err_t setVolume(int volume)
Set Player Volume.
err_t setReadyMode(void)
Set Audio Library Mode to Ready.
ThroughI2sOut
I2S output select parameter at baseband through mode.
Definition: Audio.h:334
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 writeWavHeader(File &myFile)
Write WAV Header.
Definition: Audio.h:324
err_t closeOutputFile(File &myFile)
Close Outputfile.
err_t setBeep(char enable, short volume, short frequency)
Set Beep Sound.
PlayerId
Audio library allows you to use two players simultaneously. Please set Player ID that player instance...
Definition: Audio.h:136
Definition: Audio.h:325
err_t begin(void)
Initialize the audio library and HW modules.
ThroughInput
Input select parameter at baseband through mode.
Definition: Audio.h:321
Definition: Audio.h:336
err_t setRenderingClockMode(AsClkMode mode)
Set Rendering clock mode.
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.
err_t stopPlayer(PlayerId id)
Stop Player.
Definition: Audio.h:337
Definition: Audio.h:139
static AudioClass * getInstance()
Get instance of AudioClass for singleton.
Definition: Audio.h:123
err_t stopRecorder(void)
Stop Recorder.
unsigned int err_t
Definition: Audio.h:108
Definition: Audio.h:323
int getRecordingSize()
Get recording ES size.
Definition: Audio.h:852
err_t initRecorder(uint8_t codec, uint32_t fs, uint8_t channel)
Initialize recorder.
err_t readFrames(File &myFile)
Read Stream Data from FIFO to a file by some frames.(now 5 frames)
Audio Library Class Definitions.
Definition: Audio.h:116
err_t setRecorderMode(uint8_t input_device)
Set Audio Library Mode to Sound Recorder.
err_t startRecorder(void)
Start Recorder.
err_t setLRgain(PlayerId id, unsigned char l_gain, unsigned char r_gain)
Set Player L/R Gain.
Definition: Audio.h:138
err_t startPlayer(PlayerId id)
Start Player.
err_t initPlayer(PlayerId id, uint8_t codec, uint32_t fs, uint8_t channel)
Initialize player.
The File class allows for reading from and writing to individual files on the File System...
Definition: File.h:55
Definition: Audio.h:338
err_t end(void)
Finalization the audio library and HW modules.