Created
May 7, 2025 16:57
-
-
Save ABelliqueux/21e951c6b1b7a610178970c590c4a463 to your computer and use it in GitHub Desktop.
Audio2ASCII
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This a quick and dirty variation around the code provided here by @rsta2 : https://github.com/rsta2/circle/discussions/567#discussioncomment-13051923 | |
// Build it with : gcc -o audio2ascii audio2ascii.c | |
// Run it without arguments to see usage. | |
// See here for more details : https://github.com/rsta2/circle/tree/master/sample/12-pwmsound | |
#include <stdio.h> | |
#include <string.h> | |
int main (int argc, char* argv[]) | |
{ | |
FILE *fin = NULL; | |
if (argc >= 3) { | |
fin = fopen (argv[1], "rb"); | |
} | |
if (fin == NULL) | |
{ | |
printf("Usage:\n" | |
" audio2ascii $input_file.raw $nchannel $nbits.\n\n" | |
"$nchannel can be 1 for mono, 2 for stereo.\n" | |
"$nbits can be 8 or 16.\n" | |
); | |
return 1; | |
} | |
printf("//\n" | |
"// sound.h \n" | |
"//\n" | |
"#ifndef _sound_h \n" | |
"#define _sound_h \n" | |
"\n" | |
"#define SOUND_CHANNELS %s \n" | |
"#define SOUND_BITS %s \n" | |
"\n" | |
"signed short Sound[] = \n" | |
"{\n" | |
,argv[2], argv[3]); | |
char bit8 = strcmp(argv[2], "8")==0?1:0; | |
unsigned offset = 0; | |
short value; | |
while (fread (&value, sizeof value, 1, fin) == 1) | |
{ | |
if (bit8){ | |
// Split short in two | |
printf ("%d,%d", (unsigned char) ((int) value), (unsigned char) ((int) value>>8)); | |
} else { | |
printf ("%d", (int) value); | |
} | |
if (offset++ % (24>>bit8) == (23>>bit8)) | |
{ | |
printf (",\n"); | |
} | |
else | |
{ | |
printf (","); | |
} | |
} | |
printf( "}; \n" | |
"\n" | |
"#endif" | |
); | |
fclose (fin); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment