1/*
2 * Copyright 2006, Haiku.
3 *
4 * Copyright (c) 2002-2004 Matthijs Hollemans
5 * Copyright (c) 2003 Jerome Leveque
6 * Distributed under the terms of the MIT License.
7 *
8 * Authors:
9 *
10 *		Matthijs Hollemans
11 *		J��r��me Leveque
12 */
13
14#include "debug.h"
15#include <MidiStore.h>
16#include <MidiSynthFile.h>
17#include "SoftSynth.h"
18
19using namespace BPrivate;
20
21
22BMidiSynthFile::BMidiSynthFile()
23{
24	fStore = new BMidiStore();
25}
26
27
28BMidiSynthFile::~BMidiSynthFile()
29{
30	Stop();
31	delete fStore;
32}
33
34
35status_t
36BMidiSynthFile::LoadFile(const entry_ref* midi_entry_ref)
37{
38	if (midi_entry_ref == NULL)
39		return B_BAD_VALUE;
40
41	EnableInput(true, false);
42	fStore->fFinished = true;
43
44	status_t err = fStore->Import(midi_entry_ref);
45
46	if (err == B_OK) {
47		for (int t = 0; t < 128; ++t) {
48			if (fStore->fInstruments[t]) {
49				be_synth->fSynth->LoadInstrument(t);
50			}
51		}
52	}
53
54	return err;
55}
56
57
58void
59BMidiSynthFile::UnloadFile(void)
60{
61	delete fStore;
62	fStore = new BMidiStore();
63}
64
65
66status_t
67BMidiSynthFile::Start(void)
68{
69	fStore->Connect(this);
70	fStore->SetCurrentEvent(0);
71	return fStore->Start();
72}
73
74
75void
76BMidiSynthFile::Stop(void)
77{
78	fStore->Stop();
79	fStore->Disconnect(this);
80}
81
82
83void
84BMidiSynthFile::Fade(void)
85{
86	Stop();  // really quick fade :P
87}
88
89
90void
91BMidiSynthFile::Pause(void)
92{
93	fStore->fPaused = true;
94}
95
96
97void
98BMidiSynthFile::Resume(void)
99{
100	fStore->fPaused = false;
101}
102
103
104int32
105BMidiSynthFile::Duration(void) const
106{
107	return fStore->DeltaOfEvent(fStore->CountEvents());
108}
109
110
111int32
112BMidiSynthFile::Position(int32 ticks) const
113{
114	return fStore->EventAtDelta(ticks);
115}
116
117
118int32
119BMidiSynthFile::Seek()
120{
121	return fStore->CurrentEvent();
122}
123
124
125status_t
126BMidiSynthFile::GetPatches(
127	int16* pArray768, int16* pReturnedCount) const
128{
129	int16 count = 0;
130
131	for (int t = 0; t < 128; ++t) {
132		if (fStore->fInstruments[t]) {
133			pArray768[count++] = t;
134		}
135	}
136
137	*pReturnedCount = count;
138	return B_OK;
139}
140
141
142void
143BMidiSynthFile::SetFileHook(synth_file_hook pSongHook, int32 arg)
144{
145	fStore->fHookFunc = pSongHook;
146	fStore->fHookArg = arg;
147}
148
149
150bool
151BMidiSynthFile::IsFinished() const
152{
153	return fStore->fFinished;
154}
155
156
157void
158BMidiSynthFile::ScaleTempoBy(double tempoFactor)
159{
160	fStore->SetTempo((int32) (Tempo() * tempoFactor));
161}
162
163
164void
165BMidiSynthFile::SetTempo(int32 newTempoBPM)
166{
167	fStore->SetTempo(newTempoBPM);
168}
169
170
171int32
172BMidiSynthFile::Tempo(void) const
173{
174	return fStore->Tempo();
175}
176
177
178void
179BMidiSynthFile::EnableLooping(bool loop)
180{
181	fStore->fLooping = loop;
182}
183
184
185void
186BMidiSynthFile::MuteTrack(int16 track, bool do_mute)
187{
188	fprintf(stderr, "[midi] MuteTrack is broken; don't use it\n");
189}
190
191
192void
193BMidiSynthFile::GetMuteMap(char* pTracks) const
194{
195	fprintf(stderr, "[midi] GetMuteMap is broken; don't use it\n");
196}
197
198
199void
200BMidiSynthFile::SoloTrack(int16 track, bool do_solo)
201{
202	fprintf(stderr, "[midi] SoloTrack is broken; don't use it\n");
203}
204
205
206void
207BMidiSynthFile::GetSoloMap(char* pTracks) const
208{
209	fprintf(stderr, "[midi] GetSoloMap is broken; don't use it\n");
210}
211
212
213void BMidiSynthFile::_ReservedMidiSynthFile1() { }
214void BMidiSynthFile::_ReservedMidiSynthFile2() { }
215void BMidiSynthFile::_ReservedMidiSynthFile3() { }
216
217