lplayer.cpp 1.83 KB
Newer Older
PoroCYon's avatar
PoroCYon committed
1
2

#include <stdint.h>
3
#include <stdlib.h>
PoroCYon's avatar
PoroCYon committed
4
5
6
#include <stdio.h>
#include <unistd.h>

7
8
9
#ifdef USE_CXX_VERSION
//#define AUTOCONVERT
#endif
10

PoroCYon's avatar
PoroCYon committed
11
12
#include "v2mplayer.h"
#include "synth.h"
13
14
15
16
17
18
19
20
#ifdef AUTOCONVERT
#include "v2mconv.h"
#endif
#ifdef USE_CXX_VERSION
#include "sounddef.h"
#endif

// automatically convert the module to the newest format version
PoroCYon's avatar
PoroCYon committed
21

22
extern "C" /*const*/ uint8_t theTune[];
23
extern "C" const size_t  theTune_size;
PoroCYon's avatar
PoroCYon committed
24
25
26
27

#define BUFFERLEN (8192) /* arbitrary */

static V2MPlayer player;
28
29
30
static float output_buffer[BUFFERLEN*8];

#ifdef AUTOCONVERT
31
static uint8_t* check_and_convert(/*const*/ uint8_t* tune, size_t len) {
32
33
34
35
36
	if (tune[2] != 0 || tune[3] != 0) {
		fprintf(stderr, "Invalid input file\n");
		return NULL;
	}
	int v = CheckV2MVersion(tune, len);
37
	if (!v) return tune; // version is the latest one
38
39
40
41
42
43
44
45
46
47
48
49
	if (v < 0) {
		fprintf(stderr, "Invalid input file (version not recognised)\n");
		return NULL;
	}

	uint8_t* theTune2;
	int len2;

	ConvertV2M(tune, len, &theTune2, &len2);
	return theTune2;
}
#endif
PoroCYon's avatar
PoroCYon committed
50
51

int main() {
52
53
54
55
56
57
58
59
60
61
#ifdef USE_CXX_VERSION
	sdInit();
#endif
#ifdef AUTOCONVERT
	uint8_t* theTune2 = check_and_convert(theTune, theTune_size);
	if (!theTune2) return 1;
#else
	const uint8_t* theTune2 = theTune;
#endif

PoroCYon's avatar
PoroCYon committed
62
	player.Init();
63
	player.Open(theTune2);
PoroCYon's avatar
PoroCYon committed
64
65
66
67

	player.Play();

	while (player.IsPlaying()) {
PoroCYon's avatar
PoroCYon committed
68
69
70
71
72
73
74
75
		/*
		 * Apparently, if I make the sample amount passed to Render larger,
		 * the synth will segfault somewhere in synth.asm, when writing the
		 * output samples back to the buffer. I'm not sure if/how this is
		 * supposed to happen, but keeping the sample count sufficiently low
		 * works around it. ¯\_(ツ)_/¯
		 * If you have a better fix, please tell me.
		 */
76
77
78
		const size_t samples = 64;
		player.Render(output_buffer, samples, false);
		write(STDOUT_FILENO, output_buffer, samples*sizeof(float)*2/*stereo*/);
PoroCYon's avatar
PoroCYon committed
79
80
81
82
83
84
85
	}

	player.Close();

	return 0;
}