main9.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. program main9;
  2. {$L data/module.bin.o}
  3. {$apptype arm9}
  4. {$define ARM9}
  5. {$mode objfpc}
  6. {$define HW_MIXER} // It works fine on hardware, but on no$gba sound is distorted
  7. { $define SW_MIXER} // It works fine both on hardware and no$gba
  8. { $define NO_MIXER} // No mixer, no sound :)
  9. uses
  10. ctypes, nds9, mikmod9;
  11. { $include nds.inc}
  12. { $include mikmod.inc}
  13. var
  14. module_bin_end: array [0..0] of cuint8; cvar; external;
  15. module_bin: array [0..0] of cuint8; cvar; external;
  16. module_bin_size: cuint32; cvar; external;
  17. // called by the drivers in mikmod library
  18. procedure MikMod9_SendCommand(command: cuint); cdecl; export;
  19. begin
  20. while (REG_IPC_FIFO_CR^ and IPC_FIFO_SEND_FULL) <> 0 do;
  21. REG_IPC_FIFO_TX^ := command;
  22. end;
  23. procedure TimerInterrupt();
  24. begin
  25. // player tick
  26. MikMod_Update();
  27. // the bpm can change in the middle of the song
  28. TIMER0_DATA^ := TIMER_FREQ_256((md_bpm * 50) div 125);
  29. end;
  30. var
  31. song: PModule;
  32. begin
  33. REG_IPC_FIFO_CR^ := IPC_FIFO_ENABLE or IPC_FIFO_SEND_CLEAR;
  34. consoleDemoInit();
  35. irqInit();
  36. irqEnable(IRQ_VBLANK);
  37. {$ifdef HW_MIXER}
  38. MikMod_RegisterDriver(@drv_nds_hw);
  39. {$endif HW_MIXER}
  40. {$ifdef SW_MIXER}
  41. MikMod_RegisterDriver(@drv_nds_sw);
  42. {$endif HW_MIXER}
  43. {$ifdef NO_MIXER}
  44. MikMod_RegisterDriver(@drv_nos);
  45. {$endif NO_MIXER}
  46. // if we don't know what kind of module we're going to load we can register
  47. // all loaders, but that will result in a larger binary
  48. //MikMod_RegisterAllLoaders();
  49. MikMod_RegisterLoader(@load_it);
  50. printf('Initializing library' + #10);
  51. if (MikMod_Init('') <> 0) then
  52. begin
  53. printf('Could not initialize sound, reason: ' + #10 + '%s' + #10, MikMod_strerror(MikMod_errno));
  54. exit;
  55. end;
  56. printf(#10 + 'Loading module' + #10);
  57. // Player_LoadMemory() loads a module directly from memory
  58. // it could be possible to use Player_Load() to load from FAT,
  59. // but I've never tried this
  60. song := Player_LoadMemory(@module_bin, module_bin_size, 64, 0);
  61. if assigned(song) then
  62. begin
  63. printf('Title: %s' + #10, song^.songname);
  64. printf('Channels: %u' + #10, song^.numchn);
  65. printf('bpm: %u' + #10, md_bpm);
  66. printf(#10 + 'Starting module' + #10);
  67. Player_Start(song);
  68. irqSet(IRQ_TIMER0, @TimerInterrupt);
  69. // call update with correct timing
  70. TIMER0_CR^ := TIMER0_CR^ and not TIMER_ENABLE;
  71. TIMER0_DATA^ := TIMER_FREQ_256(md_bpm * 50 div 125);
  72. TIMER0_CR^ := TIMER_ENABLE or TIMER_DIV_256 or TIMER_IRQ_REQ;
  73. irqEnable(IRQ_TIMER0);
  74. // save cursor position
  75. printf(#27 + '[s');
  76. while (Player_Active() <> 0) do
  77. begin
  78. // when using the software driver we could call update
  79. // here instead
  80. //MikMod_Update();
  81. // I need to fix this part, because I get crap values :P
  82. //printf('Time: %i : %02i : %02i' + #27 + '[u', (song^.sngtime div 60000),(song^.sngtime div 1000) mod 60,(song^.sngtime div 10) mod 100);
  83. //printf(#10 + 'Time: %u' + #27 + '[u', [(song^.sngtime div 1000) mod 60]);
  84. swiWaitForVBlank();
  85. end;
  86. printf(#10 + 'Stopping module' + #10);
  87. Player_Stop();
  88. Player_Free(song);
  89. end else
  90. printf('Could not load module, reason: ' + #10 + '%s' + #10, MikMod_strerror(MikMod_errno));
  91. printf(#10 + 'Exit library' + #10);
  92. MikMod_Exit();
  93. end.