aboutsummaryrefslogtreecommitdiff
path: root/tfmx/tfmxplayer.cpp
blob: dce7d6180c3fcd5149900665f8da34d479353508 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "common/scummsys.h"
#include "common/system.h"
#include "common/stream.h"
#include "common/file.h"
#include "common/fs.h"
#include "common/endian.h"
#include "common/debug.h"

#include "sound/mixer.h"
#include "sound/mods/protracker.h"
#include "sound/mods/tfmx.h"

#include "tfmx/tfmxdebug.h"

#define FILEDIR "" 

using namespace Common;

void simplePlaybacktest(int argc, const char *const argv[]) {
	const char *modFilename = "mod.protracker";
	if (argc == 2)
		modFilename = argv[1];


	// get Mixer, assume this never fails 
	Audio::Mixer *mixer = g_system->getMixer();

	FSNode fileDir(FILEDIR);
	debug( "searching for Files in Directory: %s", fileDir.getPath().c_str());

	FSNode musicFile = fileDir.getChild(modFilename);

	SeekableReadStream *fileIn = musicFile.createReadStream();
	if (0 == fileIn) {
		debug( "cant open File %s", musicFile.getName().c_str());
		return;
	}

	Audio::AudioStream *stream = Audio::makeProtrackerStream(fileIn);
	delete fileIn;
	if (0 == stream) {
		debug( "cant open File %s as Protacker-Stream", musicFile.getName().c_str());
		return;
	}

	Audio::SoundHandle soundH;

	mixer->playInputStream(Audio::Mixer::kMusicSoundType, &soundH, stream);
	while (mixer->isSoundHandleActive(soundH))
		g_system->delayMillis(1000);
	

	//mixer->stopAll();
}

#define MUSICFILE "mdat.monkey"
#define SAMPLEFILE "smpl.monkey"

//#define MUSICFILE "mdat.tworld_1"
//#define SAMPLEFILE "smpl.tworld_1"
Audio::Tfmx *loadTfmxfile(const char *mdatName, const char *sampleName) {
	FSNode fileDir(FILEDIR);
	FSNode musicNode = fileDir.getChild(mdatName);
	FSNode sampleNode = fileDir.getChild(sampleName);
	SeekableReadStream *musicIn = musicNode.createReadStream();
	if (0 == musicIn) {
		debug("Couldnt load file %s", MUSICFILE);
		return 0;
	}

	SeekableReadStream *sampleIn = sampleNode.createReadStream();
	if (0 == sampleIn) {
		debug("Couldnt load file %s", SAMPLEFILE);
		delete musicIn;
		return 0;
	}

	Audio::Tfmx *player = new Audio::Tfmx(44100, true);
	player->load(*musicIn, *sampleIn);
	delete musicIn;
	delete sampleIn;

	return player;
}

void runFlac(int chan, int bits, int sr, const char *fileName);

void tfmxmain(const int argc, const char *const argv[]) {
	debug("Started Scumm&VM");
	debug("Sound celebrating utility for monkey melodies & Various Malfunctions");
	debug("");

	//simplePlaybacktest(argc, argv);

	Audio::Tfmx *player = loadTfmxfile(MUSICFILE, SAMPLEFILE);
	if (!player) {
		debug("couldnt create TFMX-Player");
		return;
	}

	int i = 1;
	int playflag = 0;

	
	if (i < argc && argv[i][0] == '-' && strlen(argv[i]) == 2) {
		int param;
		switch (argv[i++][1]) {
		case 'm':
			if (i < argc) {
				param = atoi(argv[i]);
				debug( "play Macro %02X", param);
				dumpMacro(*player, 0x11);
				playflag = 1;
				player->doMacro(param,0x40);
				++i;
			}
			break;
		case 's':
			if (i < argc) {
				param = atoi(argv[i]);
				debug( "play Song %02X", param);
				dumpTrackstepsBySong(*player, param);
				playflag = 1;
				player->doSong(param);
				++i;
			}
		}
	}

	if (!playflag) {
		playflag = 1;
		//player->doMacro(20,0x40);
		player->doSong(4);
		dumpTrackstepsBySong(*player, 4);
	}



	
#if 0
	int16 buf[2 * 1024];

	while( true)
		player->readBuffer(buf, ARRAYSIZE(buf));
#endif
	int maxsecs = 60;
	if (playflag == 1) {
		// get Mixer, assume this never fails 
		Audio::Mixer *mixer = g_system->getMixer();

		Audio::SoundHandle soundH;

		mixer->playInputStream(Audio::Mixer::kMusicSoundType, &soundH, player);
		while (mixer->isSoundHandleActive(soundH) && --maxsecs)
			g_system->delayMillis(1000);
//		player->AllOff();

//		while (mixer->isSoundHandleActive(soundH));

		mixer->stopHandle(soundH);
		player = 0;
	}

	if (playflag == 2) {
		Common::FSNode file("out.raw");
		WriteStream *wav = file.createWriteStream();
		int16 buf[2 * 1024];
		int32 maxsamples = (maxsecs <= 0) ? 0 : maxsecs * 44100;
		while (!player->endOfData() && maxsamples > 0) {
			int read = player->readBuffer(buf, ARRAYSIZE(buf));
			wav->write(buf, read * 2);
			maxsamples -= read/2;
		}
		delete wav;

		runFlac(2, 16, 44100, "out.raw");
	}

#ifdef _MSC_VER
	printf("\npress a key");
	getc(stdin);
#endif
	
	delete player;
}

void runFlac( int chan, int bits, int sr, const char *fileName) {
	const char *format = "flac --endian="
#ifdef SCUMM_BIG_ENDIAN
			"big"
#else
			"little"
#endif
			" --channels=%d -f --bps=%d --sample-rate=%d --sign=signed --force-raw-format %s";
	char cmd[1024];
	sprintf(cmd, format, chan, bits, sr, fileName);
			debug(cmd);
	system(cmd);
}