-
Notifications
You must be signed in to change notification settings - Fork 1
/
sig2img.c
287 lines (233 loc) · 7.31 KB
/
sig2img.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// vim: softtabstop=4 :shiftwidth=4 :expandtab
/*******************************************************************************
*
* sig2img
* write an audiobuffer as a sequence of image files
* can be usefull for an audio-based texture in a 3D
* application.
* 062011 u@sansculotte.net
*
* todo:
* - pixelbuffer > audiobuffer: shift
* - audiobuffer > pixelbuffer: squeeze? truncate?
* - option to seperate channels
* - distort an input image with soundbuffer
* - float fps
* - test compile and run on different systems
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sndfile.h>
#include <png.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#define MINFPS 1
#define MAXFPS 1024
#define MAXWIDTH 8196
#define MAXHEIGHT 8196
#define MINWIDTH 1
#define MINHEIGHT 1
#define MINBUFSIZE 64
#define MAXBUFSIZE 2^32
#define BYTES_PER_PIXEL 1
typedef struct {
uint16_t width;
uint16_t height;
uint32_t audio_frames;
uint16_t channels;
uint16_t bit_depth;
} s_dimension;
char const *audio_path;
char output_dir[256] = "";
static void frame_loop(size_t frame, SNDFILE* audio_file, s_dimension d);
static void set_filename(char* file_name, const char* dir, size_t index);
static int min(int val1, int val2);
/**
* entry point
*/
int main (int argc, char *argv[]) {
uint16_t width, height, fps = 25, bit_depth = 8;
uint32_t audio_buffer_size, pixel_buffer_size;
size_t audio_frames, video_frames;
SNDFILE *audio_file;
SF_INFO info;
if (argc<5) {
printf("Usage: %s <audio file> <width> <height> <fps> [<ouput dir>]\n", argv[0]);
return(EXIT_FAILURE);
}
audio_path = argv[1];
if (argc>=6) {
if (strlen(argv[5]) > 255) {
puts("output_dir too long\n");
return(EXIT_FAILURE);
}
strcpy(output_dir, argv[5]);
if(access(output_dir, W_OK) != 0) {
printf("output_dir '%s' is not accessible\n", output_dir);
return(EXIT_FAILURE);
}
printf("strlen(output_dir) = %lu", strlen(output_dir));
}
if (argc>=4) {
fps = atoi(argv[4]);
}
width = atoi(argv[2]);
height = atoi(argv[3]);
if (width<MINWIDTH || width>MAXWIDTH || height<MINHEIGHT || height>MAXHEIGHT) {
printf("width must be beetween %d and %d, height between %d %d\n", MINWIDTH, MAXWIDTH, MINHEIGHT, MAXHEIGHT);
return(EXIT_FAILURE);
}
if (fps<MINFPS || fps>MAXFPS) {
printf("frames per second must be between %d and %d\n", MINFPS, MAXFPS );
return(EXIT_FAILURE);
}
memset(&info, 0, sizeof(info));
// open soundfile
audio_file = sf_open(audio_path, SFM_READ, &info);
if (audio_file == NULL) {
printf("failed to open file '%s' : \n%s\n", audio_path, sf_strerror (NULL));
return(EXIT_FAILURE);
}
if (info.channels < 1) {
puts("no audio channels.");
return(EXIT_FAILURE);
}
// allocate buffer
audio_frames = floor(info.samplerate / fps);
audio_buffer_size = info.channels * audio_frames;
pixel_buffer_size = width * height * BYTES_PER_PIXEL;
if (audio_buffer_size > pixel_buffer_size) {
printf(
"audiobuffer (%d) greater than pixel buffer (%d). can't handle this atm.\n",
audio_buffer_size,
pixel_buffer_size
);
printf("samplerate: %d, channels: %d\n", info.samplerate, info.channels);
return(EXIT_FAILURE);
}
video_frames = ceil(fps * info.frames / (int) info.samplerate);
//printf("frames: %lu\n", info.frames);
//printf("video_frames: %u, audioframes: %u, samplerate: %u\n", v_frames, info.frames, (int) info.samplerate);
if (video_frames < 1) {
puts("not enough samples for a video frame");
return(EXIT_FAILURE);
}
s_dimension dimension = {
width,
height,
audio_frames,
(short)info.channels,
bit_depth
};
for (size_t frame=0; frame<video_frames; frame++) {
frame_loop(frame, audio_file, dimension);
}
sf_close(audio_file);
return(EXIT_SUCCESS);
}
/*
* write audio block to png file
*/
void frame_loop(size_t frame, SNDFILE* audio_file, s_dimension d) {
size_t x, y;
size_t area = d.width * d.height;
size_t wsec = ceil(d.width / d.channels);
sf_count_t req, cnt;
sf_count_t copy_frames = ceil(area / d.channels);
png_uint_32 i;
png_infop info_ptr;
png_structp png_ptr;
png_color_8 sig_bit;
png_byte pixelbuffer[d.height][d.width * BYTES_PER_PIXEL];
png_bytep row_pointers[d.height];
short int audiobuffer[d.channels * area];
char file_name[268] = "";
set_filename(file_name, output_dir, frame);
FILE *fp = fopen(file_name, "wb");
if (!fp) {
exit(EXIT_FAILURE);
}
printf("%s\n", file_name);
// clear pixelbuffer
memset(&pixelbuffer, 0, area * BYTES_PER_PIXEL);
// assign row pointers
for (i = 0; i < d.height; i++) {
row_pointers[i] = &(pixelbuffer[i][0]);
}
req = (size_t)sf_seek(audio_file, d.audio_frames * frame, SEEK_SET);
if (req == -1) {
puts("[!] audiofile seek error");
// skip frame
return;
}
cnt = sf_readf_short(audio_file, audiobuffer, copy_frames);
// more pixel than audioframes? => scroll
if (area * BYTES_PER_PIXEL > d.audio_frames) {
puts("pixelbuffer scroll not implemented");
}
// copy block from audiobuffer to pixelbuffer
for (x=0; x<cnt; x++) {
//pixelbuffer[ (int) floor(x / width) ][x % width] = (char) audiobuffer[x * channels];
for(y=0; y<d.channels; y++) {
size_t ix = min(floor(x / wsec), d.height-1);
size_t iy = x % wsec + y * wsec;
pixelbuffer[ix][iy] = abs(audiobuffer[x * d.channels + y] / 128);
//printf("%lu ", (audiobuffer[x * channels + y] + sizeof(short int)) / 2);
//printf("%i ", abs((audiobuffer[x * channels + y]) / 128));
}
//printf("\n");
}
// generate png from pixelbuffer
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose(fp);
exit(EXIT_FAILURE);
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
exit(EXIT_FAILURE);
}
if (setjmp(png_jmpbuf(png_ptr))) {
puts("png error");
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
exit(EXIT_FAILURE);
}
// write png
png_init_io(png_ptr, fp);
png_set_IHDR(
png_ptr,
info_ptr,
d.width,
d.height,
d.bit_depth,
PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
sig_bit.gray = d.bit_depth;
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
png_write_info(png_ptr, info_ptr);
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
}
int min(int val1, int val2) {
return val2 > val1 ? val1 : val2;
}
void set_filename(char* file_name, const char* dir, size_t index) {
size_t num = index + 1;
if(strlen(dir) == 0) {
sprintf(file_name, "f%06lu.png", num);
} else if (strcmp(dir, "/") == 0) {
sprintf(file_name, "/f%06lu.png", num);
} else {
sprintf(file_name, "%s/f%06lu.png", dir, num);
}
}