linuxrec.h
Go to the documentation of this file.
1 /*
2  * a simple record code. using alsa-lib APIs.
3  * keep the function same as winrec.h
4  *
5  * Common steps:
6  * create_recorder,
7  * open_recorder,
8  * start_record,
9  * stop_record,
10  * close_recorder,
11  * destroy_recorder
12  *
13  */
14 
15 #ifndef __IFLY_WINREC_H__
16 #define __IFLY_WINREC_H__
17 #include <alsa/asoundlib.h>
18 #include <pthread.h>
19 #include <iostream>
20 #include "file_operation.h"
21 #include "formats.h"
22 enum
23 {
26  RECORD_STATE_READY, /* Opened */
27  RECORD_STATE_STOPPING, /* During Stop */
28  RECORD_STATE_RECORDING, /* Started */
29 };
30 
31 typedef struct
32 {
33  union
34  {
35  char* name;
36  int index;
37  void* resv;
38  } u;
40 
41 /* recorder object. */
42 struct recorder
43 {
44  void (*on_data_ind)(char* data, unsigned long len, void* user_para);
45  void* user_cb_para;
46  volatile int state; /* internal record state */
47  void* wavein_hdl;
48  /* thread id may be a struct. by implementation
49  * void * will not be ported!! */
50  pthread_t rec_thread;
51  /*void * rec_thread_hdl;*/
52  void* bufheader;
53  unsigned int bufcount;
54  char* audiobuf;
56  unsigned int buffer_time;
57  unsigned int period_time;
58  size_t period_frames;
59  size_t buffer_frames;
60  std::string pcm_file_path;
61 };
62 
63 #if __cplusplus
64 extern "C" {
65 #endif /* C++ */
66 
68 {
69 public:
70  void initRecord(struct recorder* rec, record_dev_id dev, WAVEFORMATEX* fmt);
71  int setRecordDuration(const float duration_time);
72  struct DataBuff startRecord();
73  void closeRecord();
74 
75 private:
76  struct recorder* record_;
77  snd_pcm_hw_params_t* params_; //指向 snd_pcm_hw_params_t的指针
78  unsigned int record_rate_; //录音时候的采样率
79  snd_pcm_format_t format_;
80  snd_pcm_uframes_t frames_;
81  snd_pcm_t* handle_;
84  struct DataBuff audio_pcm_;
86 };
87 
89 {
90 public:
91  float levelEnergy(struct DataBuff pcm_data, const int call_count);
92  float energyPerSample(struct DataBuff pcm_data);
93 
94 private:
95  int call_count_ = 0;
96  float background_energy_ = 0.0;
97  float temp_energy_ = 0.0;
98  float mean_energy_ = 0.0;
99  float level_ = 0;
100  const float forgetfactor_ = 1;
101  const float adjustment_ = 0.05;
103 };
110 
116 int getInputDeviceNum();
117 
131 int createRecorder(struct recorder** out_rec,
132 
133  void (*on_data_ind)(char* data, unsigned long len, void* user_para),
134 
135  void* user_cb_para);
136 
142 void destroyRecorder(struct recorder* rec);
143 
155 int openRecorder(struct recorder* rec, record_dev_id dev, WAVEFORMATEX* fmt, std::string base_path);
156 
162 void closeRecorder(struct recorder* rec);
163 
172 int startRecord(struct recorder* rec);
173 
182 int stopRecord(struct recorder* rec);
183 
190 int isRecordStopped(struct recorder* rec);
191 
192 #if __cplusplus
193 } /* extern "C" */
194 
195 #endif /* C++ */
196 
197 #endif
void * bufheader
Definition: linuxrec.h:52
volatile int state
Definition: linuxrec.h:46
int stopRecord(struct recorder *rec)
Definition: linuxrec.cpp:766
unsigned int record_rate_
Definition: linuxrec.h:78
struct recorder * record_
Definition: linuxrec.h:76
char * audiobuf
Definition: linuxrec.h:54
void * wavein_hdl
Definition: linuxrec.h:47
snd_pcm_t * handle_
Definition: linuxrec.h:81
size_t buffer_frames
Definition: linuxrec.h:59
int getInputDeviceNum()
Definition: linuxrec.cpp:693
int openRecorder(struct recorder *rec, record_dev_id dev, WAVEFORMATEX *fmt, std::string base_path)
Definition: linuxrec.cpp:724
int record_loop_
Definition: linuxrec.h:85
snd_pcm_uframes_t frames_
Definition: linuxrec.h:80
unsigned int period_time
Definition: linuxrec.h:57
void * user_cb_para
Definition: linuxrec.h:45
int createRecorder(struct recorder **out_rec, void(*on_data_ind)(char *data, unsigned long len, void *user_para), void *user_cb_para)
int audiobuf_size_
Definition: linuxrec.h:82
record_dev_id getDefaultInputDevice()
Definition: linuxrec.cpp:680
int startRecord(struct recorder *rec)
Definition: linuxrec.cpp:749
int duration_time_
Definition: linuxrec.h:83
const std::string base_path
int is_speech_count_
Definition: linuxrec.h:102
unsigned int bufcount
Definition: linuxrec.h:53
void destroyRecorder(struct recorder *rec)
Definition: linuxrec.cpp:717
pthread_t rec_thread
Definition: linuxrec.h:50
void closeRecorder(struct recorder *rec)
Definition: linuxrec.cpp:738
int bits_per_frame
Definition: linuxrec.h:55
snd_pcm_format_t format_
Definition: linuxrec.h:79
char * name
Definition: linuxrec.h:35
snd_pcm_hw_params_t * params_
Definition: linuxrec.h:77
void(* on_data_ind)(char *data, unsigned long len, void *user_para)
Definition: linuxrec.h:44
void * resv
Definition: linuxrec.h:37
std::string pcm_file_path
Definition: linuxrec.h:60
int isRecordStopped(struct recorder *rec)
Definition: linuxrec.cpp:786
unsigned int buffer_time
Definition: linuxrec.h:56
size_t period_frames
Definition: linuxrec.h:58


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53