sfxdump.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "structs.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. Fmt_chunk read_Fmt_chunk(FILE* fmt)
  6. {
  7. Fmt_chunk chunk = {.size = sizeof chunk.adpcm};
  8. memcpy(chunk.id, "fmt ", sizeof chunk.id);
  9. fread(&chunk.adpcm, sizeof chunk.adpcm, 1, fmt);
  10. return chunk;
  11. }
  12. Loop_chunk read_Loop_chunk(FfWav_header const* header)
  13. {
  14. Loop_chunk chunk = {
  15. .size = sizeof(uint32_t) * 2,
  16. .start = header->start,
  17. .end = header->end
  18. };
  19. memcpy(chunk.id, "fflp", sizeof chunk.id);
  20. return chunk;
  21. }
  22. Data_chunk* read_Data_chunk(FfWav_header const* header, FILE* data)
  23. {
  24. Data_chunk* chunk = malloc(sizeof *chunk * header->len);
  25. memcpy(chunk->id, "data", sizeof chunk->id);
  26. chunk->size = header->len;
  27. fread(chunk->data, header->len, 1, data);
  28. return chunk;
  29. }
  30. Riff_header init_riff_header()
  31. {
  32. Riff_header riff;
  33. memcpy(&riff.id, "RIFF", sizeof riff.id);
  34. memcpy(&riff.format, "WAVE", sizeof riff.format);
  35. riff.size = 0;
  36. return riff;
  37. }
  38. int main(int argc, char* argv[])
  39. {
  40. if (argc != 4) {
  41. printf("Usage: sfxdump fmt_path dat_path target_dir");
  42. return 1;
  43. }
  44. FILE* fmt = fopen(argv[1], "rb");
  45. FILE* dat = fopen(argv[2], "rb");
  46. if (!fmt || !dat) {
  47. printf("Could not open .fmt and / or .dat file");
  48. return 1;
  49. }
  50. //printf("Dumping sfx 0 - 750 to %s\n", argv[3]);
  51. for (int count = 0; count < 750; ++count) {
  52. FfWav_header header = {0};
  53. fread(&header, sizeof header, 1, fmt);
  54. if (!header.len) {
  55. fseek(fmt, sizeof(WAVEFORMATEX), SEEK_CUR);
  56. continue;
  57. }
  58. Riff_header riff = init_riff_header();
  59. Fmt_chunk format = read_Fmt_chunk(fmt);
  60. Loop_chunk loop = read_Loop_chunk(&header);
  61. Data_chunk* data = read_Data_chunk(&header, dat);
  62. riff.size = sizeof riff.format + sizeof format + sizeof *data + data->size;
  63. if (header.loop)
  64. riff.size += sizeof loop;
  65. char path[260];
  66. sprintf(path, "%s/%d.wav", argv[3], count);
  67. FILE* out_wav = fopen(path, "wb");
  68. if (!out_wav) {
  69. //printf("Error opening %s\n", path);
  70. continue;
  71. }
  72. fwrite(&riff, sizeof riff, 1, out_wav);
  73. fwrite(&format, sizeof format, 1, out_wav);
  74. fwrite(data, sizeof *data + data->size, 1, out_wav);
  75. if (header.loop) {
  76. //printf("Appending loop data for %d\n", count);
  77. fwrite(&loop, sizeof loop, 1, out_wav);
  78. }
  79. fclose(out_wav);
  80. free(data);
  81. }
  82. fclose(fmt);
  83. fclose(dat);
  84. }