Advertisement
26F

GBDK 4-bit audio hex converter

26F
Aug 11th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. /*
  2.  
  3. Raw unsigned 8-bit audio to 4bit audio for gameboy color
  4.  
  5. Audacity:
  6.  
  7. Mono track at 8192Hz project rate
  8.  
  9. (mono track)
  10.  
  11. Other uncompressed files -- RAW (header-less)
  12. Unsigned 8 bit PCM
  13.  
  14. */
  15. Code seems to produce weird sounds on game boy advance running the dmg mode rom... not sure why
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21.  
  22. char * remove_ext(char * fn) {
  23. int c;
  24. for (c = 0; fn[c] != '.'; c++) {
  25. ;
  26. }
  27. char * nfn = malloc(c * sizeof(char));
  28. for (c = 0; fn[c] != '.'; c++) {
  29. nfn[c] = fn[c];
  30. }
  31. nfn[c] = '\0';
  32. return nfn;
  33.  
  34. }
  35.  
  36. int main() {
  37. int k, file_size;
  38. long t;
  39. t = time(NULL); // seed rand
  40. srand(t);
  41. int nsamples = 0;
  42. char fn[50];
  43. int c;
  44. unsigned char hch0, hch1;
  45. FILE * f;
  46. unsigned char ch;
  47. while (1) {
  48. printf("\nFilename: \n");
  49. scanf("%s", fn);
  50. f = fopen(fn, "rb");
  51. if (f != NULL) {
  52. break;
  53. }
  54. else
  55. {
  56. printf("\n%s is not a valid file ?\n", fn);
  57. }
  58. }
  59.  
  60. fseek(f, 0, SEEK_END); // seek to end of file
  61. file_size = ftell(f); // get filesize
  62. fseek(f, 0, SEEK_SET); // back to beginning of file
  63.  
  64. char nfn_we[100];
  65. strcpy(nfn_we, remove_ext(fn));
  66. strncat(nfn_we, ".h", 2);
  67. //printf("\n%s\n", remove_ext(fn));
  68. //printf("%s", nfn_we); // for testing
  69.  
  70.  
  71. FILE * out = fopen(nfn_we, "wb");
  72. if (out == NULL) {
  73. printf("\nError creating new file.");
  74. return -1;
  75. }
  76.  
  77. char definition[100];
  78. sprintf(definition, "\n#define SAMPLES %d\n\nconst UBYTE sample[] =\n{\n", file_size-1);
  79. fprintf(out, "%s", definition);
  80. for (c = 0; !feof(f); ch = fgetc(f), c++) {
  81. if (c % 2 == 0 && c > 0) {
  82. fprintf(out, "0x%02x,", ((hch0 & 0b11110000) | ((hch1 & 0b11110000)) >> 4));
  83. nsamples++;
  84.  
  85. }
  86. if (c % 2 == 0) {
  87. hch0 = ch;
  88. }
  89. if (c % 2 == 1) {
  90. hch1 = ch;
  91. }
  92. if (c % 24 == 0 && c > 0) {
  93. fprintf(out, "\n");
  94. }
  95. }
  96.  
  97. fprintf(out, "\n");
  98. fputc('}', out);
  99. fputc(';', out);
  100. fclose(f);
  101. fclose(out);
  102.  
  103. printf("\n%d samples\n", nsamples * 2);
  104. printf("\n%d filesize\n", file_size - 1);
  105. printf("\nPress any key...");
  106. getch();
  107.  
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement