Advertisement
losinggeneration

SN76489 Sample playing

Aug 2nd, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <string.h>
  2.  
  3. #include "SDL.h"
  4. #include "sms.h"
  5. #include "sound.h"
  6. #include "sn76489.h"
  7. #include "freq.h"
  8. #include "gaw.sn76489.h"
  9.  
  10. static const int NTSC_LINES_PER_FRAME = 262;
  11. static const int PSG_DIVISOR = 16;
  12. static const int NTSC_FPS = 60;
  13. static const float NTSC_Z80_CLOCK = 3579545.0f;
  14. static const float NTSC_CLOCKS_PER_SAMPLE = 5.073051303855f;
  15.  
  16. int sms_psg_enabled = 1, sms_ym2413_enabled = 0;
  17.  
  18. // 60 fps
  19. int delay = 30;
  20. sn76489_t psg;
  21. uint32 psg_samples[313];
  22.  
  23. Uint32 emu_delay() {
  24.     static Uint32 nextTime = 0;
  25.     Uint32 now = SDL_GetTicks();
  26.  
  27.     if(nextTime <= now) {
  28.         nextTime = now + delay;
  29.         return 0;
  30.     }
  31.  
  32.     return(nextTime - now);
  33. }
  34.  
  35. int main(int argc, char *argv[]) {
  36.     int i;
  37.     float tmp = NTSC_Z80_CLOCK / PSG_DIVISOR / NTSC_FPS / NTSC_LINES_PER_FRAME /
  38.     NTSC_CLOCKS_PER_SAMPLE;
  39.     SDL_Event event;
  40.  
  41.     if(SDL_Init(SDL_INIT_VIDEO) < 0) {
  42.         return 0;
  43.     }
  44.     atexit(SDL_Quit);
  45.  
  46.     SDL_Window *window = SDL_CreateWindow("sms-synth", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 200, 0);
  47.  
  48.     if(window == 0) {
  49.         return 0;
  50.     }
  51.  
  52.     /* from sms_init */
  53.     for(i = 0; i < NTSC_LINES_PER_FRAME; ++i) {
  54.         psg_samples[i] = (uint32) (tmp * (i + 1)) - (uint32) (tmp * i);
  55.     }
  56.  
  57.     /* We end up generating 734 samples per frame @ 44100 Hz, 60fps, but we
  58.      need 735. */
  59.     psg_samples[261] += 1;
  60.  
  61.     sn76489_init(&psg, NTSC_Z80_CLOCK, 44100.0f, SN76489_NOISE_BITS_SMS, SN76489_NOISE_TAPPED_SMS);
  62.     sound_init(2, SMS_VIDEO_NTSC);
  63.     /* end sms_init */
  64.  
  65.     int done = 0,  gaw_sample = 0, gaw_size = sizeof(gaw);
  66.  
  67.     while(!done) {
  68.         int16 buf[882 << 1];
  69.         int  line, samples = 0;
  70.  
  71.         /* Write one sample per loop */
  72.         if(0x80&gaw[gaw_sample]) {
  73.             sn76489_write(&psg, gaw[gaw_sample++]);
  74.             sn76489_write(&psg, gaw[gaw_sample++]);
  75.         } else {
  76.             sn76489_write(&psg, gaw[gaw_sample++]);
  77.         }
  78.         /* Only play ¼ of the sample for now */
  79.         if(gaw_sample > gaw_size/4) {
  80.             done = 1;
  81.         }
  82.  
  83.         /* From sms_frame */
  84.         for(line = 0; line < NTSC_LINES_PER_FRAME; line++) {
  85.             if(sms_psg_enabled) {
  86.                 sn76489_execute_samples(&psg, buf + samples, psg_samples[line]);
  87.             }
  88.  
  89.             while(SDL_PollEvent(&event)) {
  90.                 switch(event.type) {
  91.                     case SDL_KEYDOWN: {
  92.                         SDL_Keycode k = event.key.keysym.sym;
  93.                         if(k == SDLK_ESCAPE) {
  94.                             done = 1;
  95.                         }
  96.                     }
  97.                     break;
  98.                     case SDL_QUIT:
  99.                         done = 1;
  100.                         break;
  101.                 }
  102.             }
  103.  
  104.             samples += psg_samples[line] << 1;
  105.         }
  106.  
  107.         if(sms_psg_enabled) {
  108.             sound_update_buffer(buf, samples << 1);
  109.             sound_wait();
  110.         }
  111.         SDL_Delay(emu_delay());
  112.     }
  113.  
  114.     sound_pause();
  115.     sound_shutdown();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement