diskop.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "diskop.h"
  2. void load_instrument_inner(Stream* stream, Instrument* inst)
  3. {
  4. size_t rwops = stream_read(stream, (uint8_t*)inst->name, sizeof(inst->name));
  5. rwops = stream_read(stream, (uint8_t*)&inst->waveform, sizeof(inst->waveform));
  6. rwops = stream_read(stream, (uint8_t*)&inst->flags, sizeof(inst->flags));
  7. rwops = stream_read(stream, (uint8_t*)&inst->sound_engine_flags, sizeof(inst->sound_engine_flags));
  8. rwops = stream_read(stream, (uint8_t*)&inst->base_note, sizeof(inst->base_note));
  9. rwops = stream_read(stream, (uint8_t*)&inst->finetune, sizeof(inst->finetune));
  10. rwops = stream_read(stream, (uint8_t*)&inst->slide_speed, sizeof(inst->slide_speed));
  11. rwops = stream_read(stream, (uint8_t*)&inst->adsr, sizeof(inst->adsr));
  12. rwops = stream_read(stream, (uint8_t*)&inst->pw, sizeof(inst->pw));
  13. if(inst->sound_engine_flags & SE_ENABLE_RING_MOD)
  14. {
  15. rwops = stream_read(stream, (uint8_t*)&inst->ring_mod, sizeof(inst->ring_mod));
  16. }
  17. if(inst->sound_engine_flags & SE_ENABLE_HARD_SYNC)
  18. {
  19. rwops = stream_read(stream, (uint8_t*)&inst->hard_sync, sizeof(inst->hard_sync));
  20. }
  21. uint8_t progsteps = 0;
  22. rwops = stream_read(stream, (uint8_t*)&progsteps, sizeof(progsteps));
  23. if(progsteps > 0)
  24. {
  25. rwops = stream_read(stream, (uint8_t*)inst->program, progsteps * sizeof(inst->program[0]));
  26. }
  27. rwops = stream_read(stream, (uint8_t*)&inst->program_period, sizeof(inst->program_period));
  28. if(inst->flags & TE_ENABLE_VIBRATO)
  29. {
  30. rwops = stream_read(stream, (uint8_t*)&inst->vibrato_speed, sizeof(inst->vibrato_speed));
  31. rwops = stream_read(stream, (uint8_t*)&inst->vibrato_depth, sizeof(inst->vibrato_depth));
  32. rwops = stream_read(stream, (uint8_t*)&inst->vibrato_delay, sizeof(inst->vibrato_delay));
  33. }
  34. if(inst->flags & TE_ENABLE_PWM)
  35. {
  36. rwops = stream_read(stream, (uint8_t*)&inst->pwm_speed, sizeof(inst->pwm_speed));
  37. rwops = stream_read(stream, (uint8_t*)&inst->pwm_depth, sizeof(inst->pwm_depth));
  38. rwops = stream_read(stream, (uint8_t*)&inst->pwm_delay, sizeof(inst->pwm_delay));
  39. }
  40. if(inst->sound_engine_flags & SE_ENABLE_FILTER)
  41. {
  42. rwops = stream_read(stream, (uint8_t*)&inst->filter_cutoff, sizeof(inst->filter_cutoff));
  43. rwops = stream_read(stream, (uint8_t*)&inst->filter_resonance, sizeof(inst->filter_resonance));
  44. rwops = stream_read(stream, (uint8_t*)&inst->filter_type, sizeof(inst->filter_type));
  45. }
  46. UNUSED(rwops);
  47. }
  48. bool load_song_inner(TrackerSong* song, Stream* stream)
  49. {
  50. uint8_t version = 0;
  51. size_t rwops = stream_read(stream, (uint8_t*)&version, sizeof(version));
  52. if(version > TRACKER_ENGINE_VERSION) //if song is of newer version this version of tracker engine can't support
  53. {
  54. return false;
  55. }
  56. tracker_engine_deinit_song(song, false);
  57. memset(song, 0, sizeof(TrackerSong));
  58. rwops = stream_read(stream, (uint8_t*)song->song_name, sizeof(song->song_name));
  59. rwops = stream_read(stream, (uint8_t*)&song->loop_start, sizeof(song->loop_start));
  60. rwops = stream_read(stream, (uint8_t*)&song->loop_end, sizeof(song->loop_end));
  61. rwops = stream_read(stream, (uint8_t*)&song->pattern_length, sizeof(song->pattern_length));
  62. rwops = stream_read(stream, (uint8_t*)&song->speed, sizeof(song->speed));
  63. rwops = stream_read(stream, (uint8_t*)&song->rate, sizeof(song->rate));
  64. rwops = stream_read(stream, (uint8_t*)&song->num_sequence_steps, sizeof(song->num_sequence_steps));
  65. for(uint16_t i = 0; i < song->num_sequence_steps; i++)
  66. {
  67. rwops = stream_read(stream, (uint8_t*)&song->sequence.sequence_step[i], sizeof(song->sequence.sequence_step[0]));
  68. }
  69. rwops = stream_read(stream, (uint8_t*)&song->num_patterns, sizeof(song->num_patterns));
  70. for(uint16_t i = 0; i < song->num_patterns; i++)
  71. {
  72. song->pattern[i].step = (TrackerSongPatternStep*)malloc(sizeof(TrackerSongPatternStep) * (song->pattern_length - 1));
  73. set_empty_pattern(&song->pattern[i], song->pattern_length);
  74. rwops = stream_read(stream, (uint8_t*)song->pattern[i].step, sizeof(TrackerSongPatternStep) * (song->pattern_length - 1));
  75. }
  76. rwops = stream_read(stream, (uint8_t*)&song->num_instruments, sizeof(song->num_instruments));
  77. for(uint16_t i = 0; i < song->num_instruments; i++)
  78. {
  79. song->instrument[i] = (Instrument*)malloc(sizeof(Instrument));
  80. set_default_instrument(song->instrument[i]);
  81. load_instrument_inner(stream, song->instrument[i]);
  82. }
  83. UNUSED(rwops);
  84. return false;
  85. }
  86. bool load_song(TrackerSong* song, Stream* stream)
  87. {
  88. char header[sizeof(SONG_FILE_SIG) + 2] = {0};
  89. size_t rwops = stream_read(stream, (uint8_t*)&header, sizeof(SONG_FILE_SIG) - 1);
  90. header[sizeof(SONG_FILE_SIG)] = '\0';
  91. if(strcmp(header, SONG_FILE_SIG) == 0)
  92. {
  93. bool result = load_song_inner(song, stream);
  94. UNUSED(result);
  95. }
  96. UNUSED(rwops);
  97. return false;
  98. }