-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileWriter.h
219 lines (164 loc) · 5.42 KB
/
FileWriter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef FILE_WRITER_H
#define FILE_WRITER_H
#define JC_MAX(a,b) (((a)>(b))?(a):(b))
#define JC_MIN(a,b) (((a)<(b))?(a):(b))
#define ALIGN_UP(value,alignment) (((uintptr_t)value + alignment - 1) & -alignment)
#define ALIGN_UP_DOUBLE(p) ALIGN_UP(p,sizeof(double)) // Using double because double should always be very large.
#include <string>
#include <ctime>
#include <thread>
#include "logging_macros.h"
#ifdef __linux__
#include "sndfile.h"
#endif
#include "opus.h"
#include "opus_multistream.h"
#include "opus_projection.h"
#include "opusenc.h"
#include "lame.h"
#include "LockFreeQueue.h"
// TIL you can do this here also
#ifdef __cplusplus
extern "C" {
#include "vringbuffer.h"
}
#endif
typedef struct buffer_t{
int overruns;
int pos;
// float data[];
float *data;
int size ;
} buffer_t;
typedef struct staticBuffer_t{
int overruns;
float pos;
float data[512];
// float *data;
} staticBuffer_t;
typedef enum {
WAV = 0,
OPUS = 1,
MP3 = 2
} FileType;
#define MAX_PACKET_SIZE (3*1276)
class FileWriter {
// for removing pops and clicks
static int total_overruns;
static long disk_writes ;
static long processed ;
int total_xruns = 0;
static int buffer_write_index ;
static int unreported_overruns;
#ifdef __linux__
SF_INFO sf_info ;
#endif
public: int bitRate = 64000 ;
static OggOpusComments *comments;
static lame_t lame ;
#ifndef LOCK_FREE_SIZE
#define LOCK_FREE_SIZE 4096
#endif
static LockFreeQueue<buffer_t *, LOCK_FREE_SIZE> lockFreeQueue ;
static int num_channels;
static OpusEncoder *encoder;
static OggOpusEnc * oggOpusEnc ;
static opus_int16 opusIn[960 * 2];
static unsigned char opusOut[MAX_PACKET_SIZE];
static int opusRead ;
static FILE * outputFile ;//for formats other than sndfile
static bool ready ;
static FileType fileType;
bool buffer_interleaved = true ;
static vringbuffer_t * vringbuffer ;
static int jack_samplerate ;
static int buffer_size_in_bytes ;
static float min_buffer_time ,
max_buffer_time ;
float *empty_buffer;
static std::thread fileWriteThread ;
static int block_size;
int default_block_size = 384 ;
static int
autoincrease_callback(vringbuffer_t *vrb, bool first_call, int reading_size, int writing_size);
int64_t seconds_to_frames(float seconds);
float frames_to_seconds(int frames);
int seconds_to_blocks(float seconds);
int seconds_to_buffers(float seconds);
static void *my_calloc(size_t size1, size_t size2);
public:
FileWriter ();
~FileWriter ();
static int disk_write(AudioBuffer * buffer);
int disk_write_callback(float *data, size_t frames);
static void * mp3_buffer ;
int buffer_index = 0 ;
std::string filename ;
void setBufferSize(int bufferSize);
void setSampleRate(int sampleRate);
void setFileName(std::string name);
void openFile();
void closeFile();
void startRecording();
static enum vringbuffer_receiver_callback_return_t disk_callback(vringbuffer_t *vrb,bool first_time,void *element){
// IN
// staticBuffer_t * sbuffer = (staticBuffer_t * ) element ;
buffer_t *buffer=(buffer_t*)element;
buffer_t ** doubleBuffer = static_cast<buffer_t **>(element);
if (first_time==true) {
// LOGD( "first time out");
// OUT
return static_cast<vringbuffer_receiver_callback_return_t>(true);
}
// disk_write(buffer->data, buffer->pos);
return VRB_CALLBACK_USED_BUFFER;
if (!useStaticBuffer) {
if (bufferUsed < MAX_STATIC_BUFFER) {
for (int i = 0 ; i < buffer->pos ; i ++)
buffers[bufferUsed].data [i] = buffer->data [i];
buffers[bufferUsed].pos = buffer->pos;
bufferUsed ++ ;
} else {
// LOGD("buffer used: %d", bufferUsed);
// for (int i = 0 ; i < bufferUsed ; i ++)
// disk_write(buffers[i].data, buffers [i].pos);
//
// disk_write(buffer->data, buffer->pos);
bufferUsed = 0;
}
}
else {
// LOGD("buffer used: %d", bufferUsed);
// for (int i = 0 ; i < bufferUsed; i ++) {
// disk_write(doubleBuffer [i] -> data,doubleBuffer [i] -> pos);
// }
bufferUsed = 0 ;
}
// OUT
return VRB_CALLBACK_USED_BUFFER;
}
// static int disk_write(SNDFILE *soundfile, void *data, size_t frames);
// static SNDFILE * soundfile ;
void stopRecording();
#ifdef __linux__
static SNDFILE *soundfile;
#endif
static int process(int nframes, const float *arg);
static void process_fill_buffers(void *data, int samples);
static bool process_new_current_buffer(int frames_left);
static bool useStaticBuffer ;
static buffer_t *current_buffer;
static buffer_t *bg_buffer;
static int MAX_STATIC_BUFFER ;
static staticBuffer_t buffers [1025] ;
static int bufferUsed ;
static void send_buffer_to_disk_thread(buffer_t *buffer);
static void process_fill_buffer(float **in, buffer_t *buffer, int i, int end);
static float buffers_to_seconds(int buffers);
static float blocks_to_seconds(int blocks);
void setFileType(int fType);
void setChannels(int channels);
void writeLoop();
void setLamePreset(int preset);
};
#endif //FILE_WRITER_H