i_discretize_file.cpp
4 KB
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
//Read only array of dim 1
#include "blc_core.h"
#include "blc_channel.h"
#include "blc_program.h"
#include <unistd.h>
#include <termios.h>
#include <libgen.h> //basename
#define READ_VAR(file, var) read_file(file, (void*)&var, sizeof(var))
#define WRITE_VAR(file, var) write_file(file, (void*)&var, sizeof(var))
bool read_file(FILE *file, void *data, size_t size){
ssize_t ret;
ret=fread(data, 1, size, file);
if (ret != (ssize_t)size) {
if ((ret==0) && feof(file)) return false;
else EXIT_ON_SYSTEM_ERROR("Reading file ret: '%d' instead od '%d'", ret, size);
}
return true;
}
void write_file(FILE *file, void *data, size_t size){
ssize_t ret;
ret=fwrite(data, 1, size, file);
if (ret != (ssize_t)size) EXIT_ON_SYSTEM_ERROR("Reading file ret: '%d' instead od '%d'", ret, size);
}
int main(int argc, char** argv){
char const *channel_name, *input_filename, *output_filename, *length_str, *samplerate_str, *period_str, *offset_str, *gain_str;
char *default_output;
float offset, gain;
int number=-1;
uint32_t start_load, time_load, start_delay, time_delay;
blc_channel channel;
FILE *file;
int period, length;
int samplerate;
asprintf(&default_output, "/%s%d", basename(argv[0]), getpid()); //This will not be free but it is only allocate once
blc_program_add_option(&output_filename, 'f', "file", "string", "filename of the file to export", NULL);
blc_program_add_option(&gain_str, 'g', "gain", "real", "gain to amplify the signal", "1.0");
blc_program_add_option(&channel_name, 'o', "blc_channel-out", "string", "blc_channel to export", default_output);
blc_program_add_option(&period_str, 'p', "period", "integer", "items nb in buffer", "0");
blc_program_add_option(&length_str, 's', "size", "integer", "items nb in buffer", "1024");
blc_program_add_option(&offset_str, 'O', "offset", "real", "offset to remove from the signal", "0");
blc_program_add_option(&samplerate_str, 'S', "samplerate", "integer", "samplerate", "8000");
blc_program_add_parameter(&input_filename, "filename", 1, "File to load", NULL);
blc_program_init(&argc, &argv, blc_quit);
blc_command_forward_blc_channels();
SSCANF(1, period_str, "%d", &period);
SSCANF(1, length_str, "%d", &length);
SSCANF(1, samplerate_str, "%d", &samplerate);
SSCANF(1, offset_str, "%f", &offset);
SSCANF(1, gain_str, "%f", &gain);
channel.create_or_open(channel_name, BLC_CHANNEL_WRITE, 'FL32', 'LPCM', 1, length);
blc_loop_try_add_posting_semaphore(channel.sem_new_data);
blc_loop_try_add_waiting_semaphore(channel.sem_ack_data);
SYSTEM_ERROR_CHECK(file=fopen(input_filename, "r"), NULL, "Opening '%s'", input_filename);
channel.publish();
uint32_t iteration=0;
float a, b, u, v,result, moy;
a=0;
u=0;
int index;
b=0;
start_delay=0;
iteration=0;
moy=0;
v=0;
BLC_COMMAND_LOOP(period*1000){
if (feof(file)){
if (number==-1) blc_command_ask_quit();
else EXIT_ON_ERROR("End of file and you request '%d' iterations. Only '%d' has been done.", number, blc_loop_iteration);
}
else{
for(index=0; index < channel.total_length; index++){
if (b<=iteration){
READ_VAR(file, start_load);
READ_VAR(file, time_load);
READ_VAR(file, start_delay);
READ_VAR(file, time_delay);
a=b;
u=v;
b=(float)start_delay*samplerate/1000000.f;
v=gain*((float)(time_delay)-offset);
}
//Formule of interpolation
result=u+(iteration-a)*(v-u)/(b-a);
channel.floats[index]=result;
iteration++;
}
}
}
SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", input_filename);
fprintf(stderr, "Stop at %u µs iteration %d", start_delay, iteration);
return EXIT_SUCCESS;
}