git ssb

0+

kode54 / syntrax-c



Tree: ff356ac03b9201b62881c0c01ab0b732d81ebb2f

Files: ff356ac03b9201b62881c0c01ab0b732d81ebb2f / src / syntrax / file.c

2930 bytesRaw
1#include <stdint.h>
2#include <stdio.h>
3#include "syntrax.h"
4
5size_t filesize;
6
7Song* loadSong(char *path)
8{
9 int i, j, k;
10 int songVer;
11 Subsong *subs;
12 Order *orderCol;
13 Order *order;
14 Row *row;
15 Instrument *instr;
16 Song *synSong;
17
18 FILE *f;
19 if (!(f = fopen(path, "rb"))) return NULL;
20
21 synSong = malloc(sizeof(Song));
22
23 /*
24 //unused vars
25 int8_t _local5[] = [0, 0, 0, 0, 0, 0];
26 bool _local2 = false;
27 int _local7 = 0;
28 bool _local8 = true;
29 */
30
31
32 fread(&synSong->h, sizeof(SongHeader), 1, f);
33 songVer = synSong->h.version;
34 if ((songVer >= 3456) && (songVer <= 3457)){
35 if (synSong->h.subsongNum > 0){
36 synSong->subsongs = malloc(synSong->h.subsongNum *sizeof(Subsong));
37 fread(synSong->subsongs, sizeof(Subsong), synSong->h.subsongNum, f);
38
39 synSong->rows = malloc(synSong->h.patNum * 64 *sizeof(Row));
40 fread(synSong->rows, sizeof(Row), synSong->h.patNum * 64, f);
41
42 synSong->patNameSizes = malloc(synSong->h.patNum *4);
43 synSong->patternNames = malloc(synSong->h.patNum *sizeof(char *));
44
45 for (i = 0; i < synSong->h.patNum; i++) {
46 fread(&synSong->patNameSizes[i], 4, 1, f);
47 synSong->patternNames[i] = malloc(synSong->patNameSizes[i]);
48 fread(synSong->patternNames[i], synSong->patNameSizes[i], 1, f);
49 synSong->patternNames[i][synSong->patNameSizes[i]] = 0x00;
50 }
51
52 synSong->instruments = malloc(synSong->h.instrNum *sizeof(Instrument));
53 synSong->samples = malloc(synSong->h.instrNum * sizeof(int16_t *));
54 for (i = 0; i < synSong->h.instrNum; i++) {
55 instr = &synSong->instruments[i];
56 fread(instr, sizeof(Instrument), 1, f);
57 if (songVer == 3456){
58 instr->shareSmpDataFromInstr = 0;
59 instr->hasLoop = 0;
60 instr->hasBidiLoop = 0;
61 instr->smpStartPoint = 0;
62 instr->smpLoopPoint = 0;
63 instr->smpEndPoint = 0;
64 if (instr->hasSample){
65 instr->smpStartPoint = 0;
66 instr->smpEndPoint = (instr->smpLength / 2);
67 instr->smpLoopPoint = 0;
68 }
69 }
70 if (instr->hasSample){
71 //instr->smpLength is in bytes, I think
72 synSong->samples[i] = malloc(instr->smpLength);
73 fread(synSong->samples[i], 2, instr->smpLength / 2, f);
74 } else {
75 synSong->samples[i] = NULL;
76 }
77
78 }
79 fread(&synSong->arpTable, 1, 0x100, f);
80 } else goto FAIL;
81 } else goto FAIL;
82 fclose(f);
83 return synSong;
84
85 FAIL:
86 fclose(f);
87 free(synSong);
88 return NULL;
89}
90

Built with git-ssb-web