-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindSilence_test.h
178 lines (145 loc) · 3.87 KB
/
findSilence_test.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
#ifndef FIND_SILENCE_TEST_H
#define FIND_SILENCE_TEST_H
#include <cairo.h>
#include <gtk/gtk.h>
typedef unsigned int uint;
struct rgb
{
unsigned char b, g, r, a;
};
struct audio_play
{
Uint8 *audio_pos;
Uint32 audio_len;
};
#define WIDTH 1024
#define HEIGHT 768
void draw_signal( double *sample_data, uint len, uint *silence )
{
cairo_surface_t *csur;
struct rgb *image_data;
struct rgb blue = { 255, 0, 0, 255 }, red = { 0, 0, 255, 255 };
double max, min;
int i, j;
csur = cairo_image_surface_create( CAIRO_FORMAT_RGB24, WIDTH, HEIGHT );
image_data = ((struct rgb *) cairo_image_surface_get_data( csur ));
max = min = sample_data[0];
for ( i = 0; i < len; ++i )
{
max = (sample_data[i] > max) ? sample_data[i] : max;
min = (sample_data[i] < min) ? sample_data[i] : min;
}
for ( i = 0; i < HEIGHT; ++i )
for ( j = 0; j < WIDTH; ++j )
{
image_data[i * WIDTH + j].a = 255;
image_data[i * WIDTH + j].r = 0;
image_data[i * WIDTH + j].g = 0;
image_data[i * WIDTH + j].b = 0;
}
for ( i = 0; i < len; ++i )
{
int val = HEIGHT - 1 - sample_data[i] * HEIGHT / (max - min);
int y = val;
int x = (double)((WIDTH-1) * i) / (double)(len);
y = (y > HEIGHT) ? (HEIGHT - 1) : ((y < 0) ? 0 : y);
for ( j = y; j < HEIGHT; ++j )
image_data[j*WIDTH+x] = silence[i] ? blue : red;
}
cairo_surface_write_to_png( csur, "Signal" );
}
void draw_histogram( double *sample_data, uint len )
{
cairo_surface_t *csur;
struct rgb *image_data;
uint *hist;
uint i, k;
double max, min;
uint hist_size;
csur = cairo_image_surface_create( CAIRO_FORMAT_RGB24, WIDTH, HEIGHT );
image_data = ((struct rgb *) cairo_image_surface_get_data( csur ));
max = min = sample_data[0];
for ( i = 0; i < len; ++i )
{
max = (sample_data[i] > max) ? sample_data[i] : max;
min = (sample_data[i] < min) ? sample_data[i] : min;
}
hist_size = max + 1;
hist = calloc( hist_size, sizeof(uint) );
for ( i = 0; i < len; ++i )
++hist[(uint) abs(sample_data[i])];
for ( i = 0; i < HEIGHT; ++i )
for ( k = 0; k < WIDTH; ++k )
{
image_data[i*WIDTH+k].a = 255;
image_data[i*WIDTH+k].r = 0;
image_data[i*WIDTH+k].g = 0;
image_data[i*WIDTH+k].b = 0;
}
for ( i = 0; i <= max; ++i )
{
uint y = hist[i];
uint x = i * WIDTH / hist_size;
y = (y > HEIGHT) ? (HEIGHT - 1) : ((y < 0) ? 0 : y);
y = HEIGHT - y;
for ( k = HEIGHT - 1; k > y; --k )
{
image_data[k*WIDTH+x].a = 255;
image_data[k*WIDTH+x].r = 0;
image_data[k*WIDTH+x].g = 255;
image_data[k*WIDTH+x].b = 0;
}
}
cairo_surface_write_to_png( csur, "Histogram" );
}
void silence_to_zero( struct audio_file *a_file, uint *silence, uint len )
{
int i;
for ( i = 0; i < a_file->samples_count; ++i )
if ( silence[i/(a_file->samples_count/len)] )
switch ( a_file->depth )
{
case 1:
((int8_t *) (a_file->raw_data))[i*a_file->channels] = 0;
break;
case 2:
((int16_t *) (a_file->raw_data))[i*a_file->channels] = 0;
break;
}
}
void my_audio_callback( void *userdata, Uint8 *stream, int len )
{
struct audio_play *play = (struct audio_play *) userdata;
len = ( len > play->audio_len ? play->audio_len : len );
memcpy( stream, play->audio_pos, len );
play->audio_pos += len;
play->audio_len -= len;
}
void play_audio( struct audio_file *a_file, uint32_t len )
{
struct audio_play *play = malloc( sizeof(struct audio_play) );
SDL_AudioSpec audio_spec;
audio_spec.freq = a_file->frequency;
switch ( a_file->depth )
{
case 1:
audio_spec.format = AUDIO_S8;
break;
case 2:
audio_spec.format = AUDIO_S16;
break;
}
audio_spec.channels = a_file->channels;
audio_spec.samples = 4096;
audio_spec.callback = my_audio_callback;
audio_spec.userdata = play;
play->audio_pos = a_file->raw_data;
play->audio_len = len;
if ( SDL_OpenAudio(&audio_spec, NULL) < 0 )
{
fprintf( stderr, "Couldn't open audio: %s\n", SDL_GetError() );
exit(-1);
}
SDL_PauseAudio(0);
}
#endif