main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * meg4/tests/modplayer/main.c
  3. *
  4. * Copyright (C) 2023 bzt
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * @brief Tests MEG-4 Amiga MOD capabilities, imports a .mod and plays it
  21. *
  22. */
  23. #define _POSIX_C_SOURCE 199309L /* needed for timespec and nanosleep() */
  24. #include <stdio.h>
  25. #include <time.h>
  26. #include <signal.h>
  27. #ifdef TEST_PA
  28. #include <portaudio.h>
  29. #else
  30. #include <SDL2/SDL.h>
  31. #endif
  32. #include "../../src/meg4.h"
  33. meg4_t meg4;
  34. int meg4_isbyte(void *buf, uint8_t byte, int len)
  35. {
  36. uint8_t *ptr = (uint8_t*)buf;
  37. int i;
  38. if(!buf || len < 1) return 0;
  39. for(i = 0; i < len && ptr[i] == byte; i++);
  40. return i >= len;
  41. }
  42. #include "../../src/editors/fmt_mod.h"
  43. /**
  44. * Audio callback wrappers
  45. */
  46. #ifdef TEST_PA
  47. PaStream *pa = NULL;
  48. static int main_audio(const void *inp, void *out, long unsigned int framesPerBuffer, const PaStreamCallbackTimeInfo *info,
  49. PaStreamCallbackFlags flags, void *ctx)
  50. {
  51. (void)inp; (void)info; (void)flags; (void)ctx;
  52. meg4_audiofeed((float*)out, framesPerBuffer);
  53. return 0;
  54. }
  55. #else
  56. int audio;
  57. SDL_AudioSpec want, have;
  58. void main_audio(void *ctx, Uint8 *buf, int len)
  59. {
  60. (void)ctx;
  61. meg4_audiofeed((float*)buf, len >> 2);
  62. }
  63. #endif
  64. /**
  65. * Log messages
  66. */
  67. void main_log(int lvl, const char* fmt, ...)
  68. {
  69. __builtin_va_list args;
  70. __builtin_va_start(args, fmt);
  71. (void)lvl;
  72. printf("meg4: "); vprintf(fmt, args); printf("\r\n");
  73. __builtin_va_end(args);
  74. }
  75. /**
  76. * CTRL+C signal handler
  77. */
  78. static int dorun = 1;
  79. static void main_stop(int signal)
  80. {
  81. (void)signal;
  82. dorun = 0;
  83. }
  84. /**
  85. * Compare two music buffers
  86. */
  87. void compare(uint8_t *buf, int len, uint8_t *out, int olen)
  88. {
  89. int i, n1, n2;
  90. if(olen != len) printf("lengths don't match\n");
  91. memset(buf, 0, 20); memset(out, 0, 20); /* clear song name */
  92. for(i = 0; i < 31; i++) {
  93. memset(buf + 20 + i * 30, 0, 22); /* clear sample names */
  94. memset(out + 20 + i * 30, 0, 22);
  95. }
  96. if(memcmp(out, buf, len < olen ? len : olen)) {
  97. printf("buffers don't match\n");
  98. if(buf[950] != out[950] || memcmp(buf + 952, out + 952, 127))
  99. printf(" - pattern orders don't match\n");
  100. for(n1 = 0, i = 952; i < 952 + buf[950] && 1084 + buf[i] * 1024 < len; i++) if(buf[i] > n1) n1 = buf[i];
  101. n1++;
  102. for(n2 = 0, i = 952; i < 952 + out[950] && 1084 + out[i] * 1024 < olen; i++) if(out[i] > n2) n2 = buf[i];
  103. n2++;
  104. if(n1 != n2) printf(" - number of patterns don't match\n");
  105. else if(memcmp(buf + 1084, out + 1084, n1 * 1024)) printf(" - patterns table don't match\n");
  106. if(len - n1 * 1024 != olen - n2 * 1024) printf(" - sample sizes don't match\n");
  107. else if(memcmp(buf + 1084 + n1 * 1024, out + 1084 + n2 * 1024, olen - 1084 - n2 * 1024))
  108. printf(" - sample buffers don't match\n");
  109. }
  110. }
  111. /**
  112. * Main function
  113. */
  114. int main(int argc, char **argv)
  115. {
  116. struct timespec tv;
  117. FILE *f;
  118. uint8_t *buf = NULL, *out;
  119. int len = 0, olen = 0;
  120. /* load input */
  121. if(argc < 1 || !argv[1]) {
  122. printf("MEG-4 Amiga MOD Player by bzt Copyright (C) 2023 GPLv3+\r\n\r\n");
  123. printf("%s <in.mod> [out.mod]\r\n", argv[0]);
  124. exit(1);
  125. }
  126. f = fopen(argv[1], "rb");
  127. if(f) {
  128. fseek(f, 0, SEEK_END);
  129. len = (int)ftell(f);
  130. fseek(f, 0, SEEK_SET);
  131. buf = malloc(len);
  132. if(buf) fread(buf, 1, len, f); else len = 0;
  133. fclose(f);
  134. }
  135. printf("importing...\n");
  136. /* initialize the DSP */
  137. memset(&meg4, 0, sizeof(meg4));
  138. dsp_init();
  139. /* import as track 0 */
  140. if(!format_mod(0, buf, len)) {
  141. free(buf);
  142. printf("unable to load mod\n");
  143. exit(1);
  144. }
  145. /* save output and do further import/export checks */
  146. if(argc > 2) {
  147. printf("exporting...\n");
  148. out = export_mod(0, &olen);
  149. if(out) {
  150. f = fopen(argv[2], "wb");
  151. if(f) {
  152. fwrite(out, 1, olen, f);
  153. fclose(f);
  154. printf("output saved\n");
  155. }
  156. compare(buf, len, out, olen);
  157. free(out);
  158. } else printf("error generating output\n");
  159. free(buf);
  160. exit(1);
  161. }
  162. free(buf);
  163. /* play the music */
  164. signal(SIGINT, main_stop);
  165. signal(SIGTERM, main_stop);
  166. printf("playing, press CTRL+C to stop...\n");
  167. /* open audio */
  168. #ifdef TEST_PA
  169. Pa_Initialize(); pa = NULL;
  170. if(Pa_OpenDefaultStream(&pa, 0, 1, paFloat32, 44100, 4096, main_audio, NULL) != paNoError || !pa) {
  171. Pa_Terminate(); printf("pa error\n"); return 1;
  172. }
  173. Pa_StartStream(pa);
  174. #else
  175. memset(&want, 0, sizeof(want));
  176. memset(&have, 0, sizeof(have));
  177. want.freq = 44100;
  178. want.format = AUDIO_F32;
  179. want.channels = 1;
  180. want.samples = 4096;
  181. want.callback = main_audio;
  182. if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER) || !(audio = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0)) ||
  183. have.freq != 44100 || have.format != AUDIO_F32 || have.channels != 1) { printf("SDL error"); return 1; }
  184. SDL_PauseAudioDevice(audio, 0);
  185. #endif
  186. /* play track 0 from row 0 with max volume, in the background */
  187. meg4_api_music(0, 0, 255);
  188. /* do something in the foreground */
  189. while(dorun && meg4.mmio.dsp_num > 0) {
  190. printf("\rrow %4u / %4u", meg4.mmio.dsp_row, meg4.mmio.dsp_num); fflush(stdout);
  191. /* wait 1/10 sec */
  192. tv.tv_sec = 0; tv.tv_nsec = 100000000;
  193. nanosleep(&tv, NULL);
  194. }
  195. printf("\nstopped\n");
  196. /* free resources */
  197. dsp_free();
  198. #ifdef TEST_PA
  199. Pa_CloseStream(pa);
  200. Pa_Terminate();
  201. #else
  202. SDL_CloseAudioDevice(audio);
  203. SDL_Quit();
  204. #endif
  205. return 0;
  206. }