Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Raw unsigned 8-bit audio to 4bit audio for gameboy color
- Audacity:
- Mono track at 8192Hz project rate
- (mono track)
- Other uncompressed files -- RAW (header-less)
- Unsigned 8 bit PCM
- */
- Code seems to produce weird sounds on game boy advance running the dmg mode rom... not sure why
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- char * remove_ext(char * fn) {
- int c;
- for (c = 0; fn[c] != '.'; c++) {
- ;
- }
- char * nfn = malloc(c * sizeof(char));
- for (c = 0; fn[c] != '.'; c++) {
- nfn[c] = fn[c];
- }
- nfn[c] = '\0';
- return nfn;
- }
- int main() {
- int k, file_size;
- long t;
- t = time(NULL); // seed rand
- srand(t);
- int nsamples = 0;
- char fn[50];
- int c;
- unsigned char hch0, hch1;
- FILE * f;
- unsigned char ch;
- while (1) {
- printf("\nFilename: \n");
- scanf("%s", fn);
- f = fopen(fn, "rb");
- if (f != NULL) {
- break;
- }
- else
- {
- printf("\n%s is not a valid file ?\n", fn);
- }
- }
- fseek(f, 0, SEEK_END); // seek to end of file
- file_size = ftell(f); // get filesize
- fseek(f, 0, SEEK_SET); // back to beginning of file
- char nfn_we[100];
- strcpy(nfn_we, remove_ext(fn));
- strncat(nfn_we, ".h", 2);
- //printf("\n%s\n", remove_ext(fn));
- //printf("%s", nfn_we); // for testing
- FILE * out = fopen(nfn_we, "wb");
- if (out == NULL) {
- printf("\nError creating new file.");
- return -1;
- }
- char definition[100];
- sprintf(definition, "\n#define SAMPLES %d\n\nconst UBYTE sample[] =\n{\n", file_size-1);
- fprintf(out, "%s", definition);
- for (c = 0; !feof(f); ch = fgetc(f), c++) {
- if (c % 2 == 0 && c > 0) {
- fprintf(out, "0x%02x,", ((hch0 & 0b11110000) | ((hch1 & 0b11110000)) >> 4));
- nsamples++;
- }
- if (c % 2 == 0) {
- hch0 = ch;
- }
- if (c % 2 == 1) {
- hch1 = ch;
- }
- if (c % 24 == 0 && c > 0) {
- fprintf(out, "\n");
- }
- }
- fprintf(out, "\n");
- fputc('}', out);
- fputc(';', out);
- fclose(f);
- fclose(out);
- printf("\n%d samples\n", nsamples * 2);
- printf("\n%d filesize\n", file_size - 1);
- printf("\nPress any key...");
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement