1 |
12 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: buildsamples.cpp
|
4 |
|
|
//
|
5 |
|
|
// Project: CMod S6 System on a Chip, ZipCPU demonstration project
|
6 |
|
|
//
|
7 |
38 |
dgisselq |
// Purpose: This program converts an 8kHz, 16t, mono- sound data file into
|
8 |
|
|
// samples that can be read and understood by the C compiler and
|
9 |
|
|
// hence included in a S6SoC project as the ``doorbell'' audio sound.
|
10 |
12 |
dgisselq |
//
|
11 |
38 |
dgisselq |
// To use this, first convert your sound file to 8kHz, 16t, raw mono
|
12 |
|
|
// format. The sox utility works well for this purpose.
|
13 |
|
|
//
|
14 |
|
|
// Then, run this program wih the single argument being the name of that
|
15 |
|
|
// sound file. The standard output will be the resulting C-code
|
16 |
|
|
// representing/recreating that sound file. Pipe this into a file for
|
17 |
|
|
// inclusion in your program. You can then reference both the number of
|
18 |
|
|
// samples from that file, as well as the number of words those samples
|
19 |
|
|
// are stored in, as well as what those samples are.
|
20 |
|
|
//
|
21 |
|
|
// The samples themselves can then be placed in a flash partition, and
|
22 |
|
|
// read like any memory from within your program.
|
23 |
|
|
//
|
24 |
12 |
dgisselq |
// Creator: Dan Gisselquist, Ph.D.
|
25 |
|
|
// Gisselquist Technology, LLC
|
26 |
|
|
//
|
27 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
28 |
|
|
//
|
29 |
|
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
30 |
|
|
//
|
31 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
32 |
|
|
// modify it under the terms of the GNU General Public License as published
|
33 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
34 |
|
|
// your option) any later version.
|
35 |
|
|
//
|
36 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
37 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
38 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
39 |
|
|
// for more details.
|
40 |
|
|
//
|
41 |
|
|
// You should have received a copy of the GNU General Public License along
|
42 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
43 |
|
|
// target there if the PDF file isn't present.) If not, see
|
44 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
45 |
|
|
//
|
46 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
47 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
48 |
|
|
//
|
49 |
|
|
//
|
50 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
51 |
|
|
//
|
52 |
|
|
//
|
53 |
|
|
#include <stdio.h>
|
54 |
|
|
#include <stdlib.h>
|
55 |
|
|
#include <string.h>
|
56 |
|
|
|
57 |
|
|
#define SLEN 512
|
58 |
|
|
#define ILEN (SLEN/2)
|
59 |
|
|
|
60 |
|
|
int main(int argc, char **argv) {
|
61 |
|
|
FILE *fp, *legal;
|
62 |
|
|
|
63 |
|
|
char cbuf[SLEN];
|
64 |
|
|
short sbuf[SLEN];
|
65 |
|
|
int nread = 0, nr;
|
66 |
|
|
|
67 |
38 |
dgisselq |
if (argc != 1) {
|
68 |
|
|
fprintf(stderr,
|
69 |
|
|
"Usage:\tbuildsamples samplefile\n"
|
70 |
|
|
"\n"
|
71 |
|
|
"\twhere 'samplefile' is a sound data file consisting of 16-bit samples\n"
|
72 |
|
|
"\tthat have been sampled at 8kHz. The sample file should have no\n"
|
73 |
|
|
"\theader, and should only reference a mono (not stereo) output.\n"
|
74 |
|
|
"\n"
|
75 |
|
|
"\tThe result is a C program fragment that can be redirected to a file for\n"
|
76 |
|
|
"\tlater inclusion in your project.\n");
|
77 |
|
|
exit(EXIT_SUCCESS);
|
78 |
|
|
}
|
79 |
|
|
|
80 |
12 |
dgisselq |
fp = fopen(argv[1], "rb");
|
81 |
|
|
if (!fp) {
|
82 |
|
|
fprintf(stderr, "Could not open %s for reading\n", argv[1]);
|
83 |
|
|
exit(EXIT_FAILURE);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
/*
|
87 |
|
|
legal = fopen("../legal.txt", "r");
|
88 |
|
|
if (!legal)
|
89 |
|
|
legal = fopen("../../legal.txt", "r");
|
90 |
|
|
*/
|
91 |
|
|
legal = NULL;
|
92 |
|
|
if (legal) {
|
93 |
|
|
while(fgets(cbuf, SLEN, legal)) {
|
94 |
|
|
if (strncmp(cbuf, "// Filename:", 12)==0)
|
95 |
|
|
printf("// Filename:\tsamples.c\n");
|
96 |
|
|
else printf("%s", cbuf);
|
97 |
|
|
} fclose(legal);
|
98 |
|
|
} else {
|
99 |
|
|
printf("// This file should be copyrighted but I can't find\n");
|
100 |
|
|
printf("// the copyright statement.\n//\n");
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
printf(
|
104 |
|
|
"// This file is computer generated--DO NOT EDIT IT! The generator file can\n"
|
105 |
|
|
"// be found in trunk/sw/host/%s\n"
|
106 |
|
|
"//\n"
|
107 |
|
|
"//\n"
|
108 |
|
|
"#ifndef\tSOUND_DATA_H\n"
|
109 |
|
|
"#define\tSOUND_DATA_H\n\n"
|
110 |
|
|
"const\tint\tsound_data[] = {\n", __FILE__);
|
111 |
|
|
|
112 |
|
|
while((nr = fread(sbuf, sizeof(short), SLEN, fp))>0) {
|
113 |
|
|
int pos = 0;
|
114 |
|
|
nread += nr;
|
115 |
|
|
while(pos < nr) {
|
116 |
|
|
printf("\t");
|
117 |
|
|
for(int i=0; (pos<((nr+1)&-2))&&(i<8); i+=2) {
|
118 |
|
|
int iv = (sbuf[pos]<<16)|(sbuf[pos+1] & 0x0ffff);
|
119 |
|
|
printf("0x%08x, ", iv);
|
120 |
|
|
pos+=2;
|
121 |
|
|
} printf("\n");
|
122 |
|
|
}
|
123 |
|
|
} printf("\t0\n};\n");
|
124 |
|
|
|
125 |
|
|
printf("\n\n"
|
126 |
|
|
"#define\tNSAMPLES\t%d\n"
|
127 |
|
|
"#define\tNSAMPLE_WORDS\t%d\n"
|
128 |
|
|
"\n\n#endif\n", nread, 1+((nread+1)>>1));
|
129 |
|
|
|
130 |
|
|
}
|
131 |
|
|
|