1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sndfile.h>
6 
write_sample(SNDFILE * snd,FILE * fp,char * wav,char * name,float * buf,int bufsize,uint32_t * pos,int sr)7 void write_sample(SNDFILE *snd, FILE *fp, char *wav, char *name,
8         float *buf, int bufsize, uint32_t *pos, int sr)
9 {
10     SF_INFO info;
11     memset(&info, 0, sizeof(SF_INFO));
12     info.format = 0;
13     int count;
14     SNDFILE *in = sf_open(wav, SFM_READ, &info);
15     fprintf(fp, "[%s]\npos = %g\n", name, 1.0 * (*pos) / sr);
16     uint32_t start = *pos;
17     while((count = sf_read_float(in, buf, bufsize))) {
18         sf_write_float(snd, buf, count);
19         *pos += count;
20     }
21     fprintf(fp, "size = %g\n\n", (1.0 * (*pos) - start) / sr);
22     sf_close(in);
23 }
24 
main(int argc,char * argv[])25 int main(int argc, char *argv[])
26 {
27     if(argc <= 1) {
28         printf("Usage: [options] wav2smp in1.wav in1_name in2.wav in2_name...\n\n");
29         printf("Flags:\n");
30         printf("\t-w\tWAV file to write to (default: out.wav)\n");
31         printf("\t-o\tINI file to write to (default: out.ini)\n");
32         printf("\t-r\tSet samplerate. (default: 96000)\n");
33         return 0;
34     }
35 
36 
37     int argpos = 1;
38     int i;
39     int sr = 96000;
40     char wavfile[30] = "out.wav";
41     char inifile[30] = "out.ini";
42     float buf[4096];
43 
44     argv++;
45 
46     while(argv[0][0] == '-') {
47         switch(argv[0][1]) {
48             case 'w':
49                 strncpy(wavfile, argv[1], 30);
50                 argv++;
51                 argpos++;
52                 break;
53 
54             case 'o':
55                 strncpy(inifile, argv[1], 30);
56                 argv++;
57                 argpos++;
58                 break;
59 
60             case 'r':
61                 sr = atoi(argv[1]);
62                 argv++;
63                 argpos++;
64                 break;
65 
66             default:
67                 fprintf(stderr, "Uknown option '%c'\n", argv[0][1]);
68                 break;
69 
70         }
71         argv++;
72         argpos++;
73     }
74 
75     SF_INFO info;
76     info.samplerate = sr;
77     info.channels = 1;
78     info.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
79     SNDFILE *snd = sf_open(wavfile, SFM_WRITE, &info);
80     FILE *fp = fopen(inifile, "w");
81     uint32_t pos = 0;
82 
83 
84     for(i = argpos; i < argc; ) {
85         write_sample(snd, fp, argv[0], argv[1], buf, 4096, &pos, sr);
86         argv += 2;
87         i += 2;
88     }
89 
90     fclose(fp);
91     sf_close(snd);
92     return 0;
93 }
94