Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayerdyne committed Dec 22, 2019
0 parents commit 086f51b
Show file tree
Hide file tree
Showing 6 changed files with 663 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# txt->midi

Short program I wrote to convert .txt format of music used in the game Azimuth from Matthew D. Steele (see https://github.com/mdsteele/azimuth) into .mid files.

This is given without any kind of warranty.

> /*=============================================================================
> | Written by Rayerdyne (2019) to make midi file from custom music format used |
> | in Azimuth, using parts of Azimuth's functionnalities. |
> | |
> | Do wathever you want with this part of the code, its only initial goal was |
> | to make music arrangement easier. The code is given without any warranty. |
> | |
> | /!\ See Azimuth's sources files for their licenses. |
> | |
> =============================================================================*/
114 changes: 114 additions & 0 deletions data.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*=============================================================================
| Written by Rayerdyne (2019) to make midi file from custom music format used |
| in Azimuth, using parts of Azimuth's functionnalities. |
| |
| Do wathever you want with this part of the code, its only initial goal was |
| to make music arrangement easier. The code is given without any warranty. |
| |
| /!\ See Azimuth's sources files for their licenses. |
| |
=============================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "data.h"

#define LENGTH 5

static char get_char(char c[LENGTH], FILE *file) {
int i;
for (i = 0; i < LENGTH - 1; i++)
c[i] = c[i+1];
c[LENGTH-1] = fgetc(file);
return c[LENGTH -1];
}

static bool is_num(char c){
return ('0' <= c && c <= '9');
}

midi_music_voices_data *midi_read_voices_data(char *file_name){
FILE *file = fopen(file_name, "r");
if (!file) {
fprintf(stderr, "could not open file %s\n", file_name);
exit(EXIT_FAILURE);
}

midi_music_voices_data *data = (midi_music_voices_data*) malloc (sizeof(midi_music_voices_data));
data->parts = (midi_part_data**) malloc (sizeof(midi_part_data *));

char line[1024];
char *s;
do {
s = fgets(line, 1024, file);
s = strstr(line, "=tempo ");
} while ( s == NULL && !feof(file));

s = s + 7;// length of '=tempo '
int tempo = atoi(s);
if (tempo == 0) {
fprintf(stderr, "ERROR reading the tempo\n");
exit(EXIT_FAILURE);
}
data->tempo = tempo;
data->num_parts = 0;
int i = 0;

char c[LENGTH+1] = {0};
for (i = 0; i < LENGTH; i++)
get_char(c, file);
do {
get_char(c, file);
} while (strcmp (c, "!Part") != 0 && !feof(file));

i = 0;
do {
data->num_parts++;
// fprintf(stderr, "reading: part index: %d\n", i);
data->parts = (midi_part_data **) realloc(data->parts, data->num_parts*sizeof(midi_part_data *));
data->parts[i] = (midi_part_data *) malloc(sizeof(midi_part_data));
data->parts[i]->num_voices = 0;
int j;
for (j = 0; j < AZ_MUSIC_NUM_TRACKS; j++)
data->parts[i]->voices[j] = false;
do {
get_char(c, file);
if (c[0] == '\n' && is_num(c[1]) && c[2] == '|' && c[3] == ' ' &&
! data->parts[i]->voices[c[1]-'1']) {
data->parts[i]->voices[c[1]-'1'] = true;
data->parts[i]->num_voices++;
if (data->parts[i]->num_voices > AZ_MUSIC_NUM_TRACKS) {
fprintf(stderr, "number of voices is too high !");
exit(EXIT_FAILURE);
}
}
} while (strcmp (c, "!Part") != 0 && !feof(file));
i++;
} while (!feof(file));

printf("read %d parts\n", i);
// fclose(file);
return data;
}


void midi_free_music_voices_data(midi_music_voices_data *data) {
int i;
for (i = 0; i < data->num_parts; i++)
free(data->parts[i]);
free(data->parts);
free(data);
}

void midi_print_music_voices_data(midi_music_voices_data *data) {
int i;
printf("num_parts: %d\n", data->num_parts);
for (i = 0; i < data->num_parts; i++)
printf("part %d: [%d|%d|%d|%d|%d]\n", i, data->parts[i]->voices[0], data->parts[i]->voices[1],
data->parts[i]->voices[2], data->parts[i]->voices[3],
data->parts[i]->voices[4]);
}


35 changes: 35 additions & 0 deletions data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*=============================================================================
| Written by Rayerdyne (2019) to make midi file from custom music format used |
| in Azimuth, using parts of Azimuth's functionnalities. |
| |
| Do wathever you want with this part of the code, its only initial goal was |
| to make music arrangement easier. The code is given without any warranty. |
| |
| /!\ See Azimuth's sources files for their licenses. |
| |
=============================================================================*/

#ifndef MIDI_DATA
#define MIDI_DATA

#include <stdbool.h>

//#include "azimuth/util/music.h"
#define AZ_MUSIC_NUM_TRACKS 5

typedef struct {
bool voices[AZ_MUSIC_NUM_TRACKS];
int num_voices;
} midi_part_data;

typedef struct {
midi_part_data **parts;
int num_parts;
int tempo;
} midi_music_voices_data;

midi_music_voices_data *midi_read_voices_data(char *file_name);
void midi_free_music_voices_data(midi_music_voices_data *voices_data);
void midi_print_music_voices_data(midi_music_voices_data *voices_data);

#endif //MIDI_DATA
68 changes: 68 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*=============================================================================
| Written by Rayerdyne (2019) to make midi file from custom music format used |
| in Azimuth, using parts of Azimuth's functionnalities. |
| |
| Do wathever you want with this part of the code, its only initial goal was |
| to make music arrangement easier. The code is given without any warranty. |
| |
| /!\ See Azimuth's sources files for their licenses. |
| |
=============================================================================*/

#include <stdbool.h>
#include <stdlib.h>

#include "azimuth/util/music.h"
#include "azimuth/state/music.h"
#include "midi/midi.h"
#include "midi/data.h"


// <copied>
az_music_t music;
static void destroy_music(void) {
az_destroy_music(&music);
}
// </copied>

int main(int argc, char **argv)
{
if (argc < 3) {
fprintf(stderr, "Usage: <input file> <output file>\n");
return EXIT_SUCCESS;
}

midi_music_voices_data *data = midi_read_voices_data(argv[1]);

// <copied>
// Initialize drum kit:
int num_drums = 0;
const az_sound_data_t *drums = NULL;
az_get_drum_kit(&num_drums, &drums);

// Load music:
az_reader_t reader;
if (!az_file_reader(argv[1], &reader)) {
fprintf(stderr, "ERROR: could not open %s\n", argv[1]);
return EXIT_FAILURE;
}
if (!az_read_music(&reader, num_drums, drums, &music)) {
fprintf(stderr, "ERROR: failed to parse music.\n");
return EXIT_FAILURE;
}
az_rclose(&reader);
atexit(destroy_music);
// </copied>

FILE *file = fopen(argv[2], "w");
if (!file) {
fprintf(stderr, "ERROR: could not open %s\n", argv[2]);
return EXIT_FAILURE;
}
midi_write_music(&music, data, file);

fclose(file);
midi_free_music_voices_data(data);
printf("Done.\n");
return EXIT_SUCCESS;
}
Loading

0 comments on commit 086f51b

Please sign in to comment.