1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <stdio.h>
#include <stdlib.h>
#include <libmodplug/modplug.h>
struct var_buffer
{
void* buf;
int length;
};
// Read entire file into buffer
// buf is NULL on failure
struct var_buffer read_file(const char* filename)
{
struct var_buffer result = { NULL, 0 };
FILE* in_file = fopen(filename, "rb");
if (in_file == NULL)
{
perror("Error opening input file");
return result;
}
// Get file size
fseek(in_file, 0, SEEK_END);
int size = ftell(in_file);
rewind(in_file);
// Allocate buffer and read data into it
void* buf = malloc(size);
if (buf == NULL)
{
fputs("Error allocating memory", stderr);
goto done;
}
if (fread(buf, size, 1, in_file) != 1)
{
perror("Error reading file");
goto done;
}
result.buf = buf;
result.length = size;
done:
fclose(in_file);
return result;
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s <module>\n", argv[0]);
return 1;
}
// Read entire input file into buffer
struct var_buffer buffer = read_file(argv[1]);
if (buffer.buf == NULL)
return 1;
// Load module
ModPlugFile* mod = ModPlug_Load(buffer.buf, buffer.length);
if (mod == NULL)
{
fputs("Error loading module", stderr);
free(buffer.buf);
return 1;
}
// Get first pattern number
ModPlug_SeekOrder(mod, 0);
int pattern = ModPlug_GetCurrentPattern(mod);
// Dump first line of first pattern
int channels = ModPlug_NumChannels(mod);
const ModPlugNote* note_data = ModPlug_GetPattern(mod, pattern, NULL);
for (int i = 0; i < channels; i++)
{
printf("%d %d%c",
note_data[i].Note,
note_data[i].Instrument,
(i == channels - 1) ? '\n' : ',');
}
ModPlug_Unload(mod);
free(buffer.buf);
return 0;
}
|