xref: /haiku/src/kits/midi/SoftSynth.h (revision e3a74a24bd8ff88576bdf712ad87eb557db5ae3b)
1fbaf754aSmahlzeit /*
2d1c4420eSmahlzeit  * Copyright (c) 2004-2005 Matthijs Hollemans
3fbaf754aSmahlzeit  * Copyright (c) 2003 Jerome Leveque
4fbaf754aSmahlzeit  *
5fbaf754aSmahlzeit  * Permission is hereby granted, free of charge, to any person obtaining a
6fbaf754aSmahlzeit  * copy of this software and associated documentation files (the "Software"),
7fbaf754aSmahlzeit  * to deal in the Software without restriction, including without limitation
8fbaf754aSmahlzeit  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9fbaf754aSmahlzeit  * and/or sell copies of the Software, and to permit persons to whom the
10fbaf754aSmahlzeit  * Software is furnished to do so, subject to the following conditions:
11fbaf754aSmahlzeit  *
12fbaf754aSmahlzeit  * The above copyright notice and this permission notice shall be included in
13fbaf754aSmahlzeit  * all copies or substantial portions of the Software.
14fbaf754aSmahlzeit  *
15fbaf754aSmahlzeit  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16fbaf754aSmahlzeit  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17fbaf754aSmahlzeit  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18fbaf754aSmahlzeit  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19fbaf754aSmahlzeit  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20fbaf754aSmahlzeit  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21fbaf754aSmahlzeit  * DEALINGS IN THE SOFTWARE.
22fbaf754aSmahlzeit  */
23fbaf754aSmahlzeit 
24fbaf754aSmahlzeit #ifndef _SOFT_SYNTH_H
25fbaf754aSmahlzeit #define _SOFT_SYNTH_H
26fbaf754aSmahlzeit 
27d1c4420eSmahlzeit /*
28d1c4420eSmahlzeit 	WORK IN PROGRESS!
29d1c4420eSmahlzeit 
30d1c4420eSmahlzeit 	This version of SoftSynth is a wrapper for Michael Pfeiffer's port
31d1c4420eSmahlzeit 	of TiMidity++ 2.11.3/3 (http://www.bebits.com/app/2736).
32d1c4420eSmahlzeit 
33d1c4420eSmahlzeit 	It works, but not good enough yet. Playback from MidiPlayer sounds
34d1c4420eSmahlzeit 	a lot worse than using TiMidity in standalone mode. Either TiMidity's
35d1c4420eSmahlzeit 	MidiConsumer doesn't work properly or the Midi Kit messes things up;
36d1c4420eSmahlzeit 	I haven't investigated yet.
37d1c4420eSmahlzeit 
38d1c4420eSmahlzeit 	To try it out, download TiMidity and the sound files (30MB):
39d1c4420eSmahlzeit 	http://bepdf.sourceforge.net/download/midi/TiMidity++-2.11.3_3.x86.zip
40d1c4420eSmahlzeit 	http://bepdf.sourceforge.net/download/midi/eawpats11_full_beos.zip
41d1c4420eSmahlzeit 
42d1c4420eSmahlzeit 	Follow the instructions in the archive to install. Then double-click
43d1c4420eSmahlzeit 	"Start TiMidity Server" to launch TiMidity. The server will publish a
44d1c4420eSmahlzeit 	consumer endpoint. You can verify this with PatchBay.
45d1c4420eSmahlzeit 
46d1c4420eSmahlzeit 	Build the Haiku Midi Kit. Put libmidi.so and libmidi2.so in ~/config/lib.
47d1c4420eSmahlzeit 	Quit the BeOS midi_server. Launch the Haiku midi_server.
48d1c4420eSmahlzeit 
49d1c4420eSmahlzeit 	Build the Haiku MidiPlayer (or use the BeOS MidiPlayer). Start it and
50d1c4420eSmahlzeit 	choose a MIDI file. If all went fine, you will hear TiMidity play back
51d1c4420eSmahlzeit 	the song. Just not very well. :-)
52d1c4420eSmahlzeit 
53d1c4420eSmahlzeit 	Note: You can still use the Midi Kit if you don't install TiMidity,
54d1c4420eSmahlzeit 	but the software synth will simply make no sound.
55d1c4420eSmahlzeit  */
56d1c4420eSmahlzeit 
57fbaf754aSmahlzeit #include <Midi.h>
58fbaf754aSmahlzeit #include <Synth.h>
59fbaf754aSmahlzeit 
60*e3a74a24SJérôme Duval #include <SoundPlayer.h>
61*e3a74a24SJérôme Duval 
62*e3a74a24SJérôme Duval #include <fluidsynth.h>
63*e3a74a24SJérôme Duval 
64d1c4420eSmahlzeit class BMidiConsumer;
6565f512e0SIngo Weinhold class BMidiSynth;
6665f512e0SIngo Weinhold class BSynth;
67d1c4420eSmahlzeit 
68fbaf754aSmahlzeit namespace BPrivate {
69fbaf754aSmahlzeit 
70fbaf754aSmahlzeit class BSoftSynth
71fbaf754aSmahlzeit {
72fbaf754aSmahlzeit public:
73fbaf754aSmahlzeit 
74*e3a74a24SJérôme Duval 	bool InitCheck() const;
75d1c4420eSmahlzeit 
76*e3a74a24SJérôme Duval 	void Unload();
77*e3a74a24SJérôme Duval 	bool IsLoaded() const;
78fbaf754aSmahlzeit 
79fbaf754aSmahlzeit 	status_t SetDefaultInstrumentsFile();
80fbaf754aSmahlzeit 	status_t SetInstrumentsFile(const char* path);
81fbaf754aSmahlzeit 
82fbaf754aSmahlzeit 	status_t LoadAllInstruments();
83fbaf754aSmahlzeit 	status_t LoadInstrument(int16 instrument);
84fbaf754aSmahlzeit 	status_t UnloadInstrument(int16 instrument);
85fbaf754aSmahlzeit 	status_t RemapInstrument(int16 from, int16 to);
86fbaf754aSmahlzeit 	void FlushInstrumentCache(bool startStopCache);
87fbaf754aSmahlzeit 
88fbaf754aSmahlzeit 	void SetVolume(double volume);
89fbaf754aSmahlzeit 	double Volume(void) const;
90fbaf754aSmahlzeit 
91fbaf754aSmahlzeit 	status_t SetSamplingRate(int32 rate);
92fbaf754aSmahlzeit 	int32 SamplingRate() const;
93fbaf754aSmahlzeit 
94fbaf754aSmahlzeit 	status_t SetInterpolation(interpolation_mode mode);
95fbaf754aSmahlzeit 	interpolation_mode Interpolation() const;
96fbaf754aSmahlzeit 
97fbaf754aSmahlzeit 	status_t EnableReverb(bool enabled);
98fbaf754aSmahlzeit 	bool IsReverbEnabled() const;
99fbaf754aSmahlzeit 	void SetReverb(reverb_mode mode);
100fbaf754aSmahlzeit 	reverb_mode Reverb() const;
101fbaf754aSmahlzeit 
102fbaf754aSmahlzeit 	status_t SetMaxVoices(int32 max);
103fbaf754aSmahlzeit 	int16 MaxVoices(void) const;
104fbaf754aSmahlzeit 
105fbaf754aSmahlzeit 	status_t SetLimiterThreshold(int32 threshold);
106fbaf754aSmahlzeit 	int16 LimiterThreshold(void) const;
107fbaf754aSmahlzeit 
108fbaf754aSmahlzeit 	void Pause(void);
109fbaf754aSmahlzeit 	void Resume(void);
110fbaf754aSmahlzeit 
111fbaf754aSmahlzeit 	void NoteOff(uchar, uchar, uchar, uint32);
112fbaf754aSmahlzeit 	void NoteOn(uchar, uchar, uchar, uint32);
113fbaf754aSmahlzeit 	void KeyPressure(uchar, uchar, uchar, uint32);
114fbaf754aSmahlzeit 	void ControlChange(uchar, uchar, uchar, uint32);
115fbaf754aSmahlzeit 	void ProgramChange(uchar, uchar, uint32);
116fbaf754aSmahlzeit  	void ChannelPressure(uchar, uchar, uint32);
117fbaf754aSmahlzeit 	void PitchBend(uchar, uchar, uchar, uint32);
118fbaf754aSmahlzeit 	void SystemExclusive(void*, size_t, uint32);
119fbaf754aSmahlzeit 	void SystemCommon(uchar, uchar, uchar, uint32);
120fbaf754aSmahlzeit 	void SystemRealTime(uchar, uint32);
121fbaf754aSmahlzeit 	void TempoChange(int32, uint32);
122fbaf754aSmahlzeit 	void AllNotesOff(bool, uint32);
123fbaf754aSmahlzeit 
124fbaf754aSmahlzeit private:
125fbaf754aSmahlzeit 
12665f512e0SIngo Weinhold 	friend class ::BSynth;
12765f512e0SIngo Weinhold 	friend class ::BMidiSynth;
128fbaf754aSmahlzeit 
129fbaf754aSmahlzeit 	BSoftSynth();
130fbaf754aSmahlzeit 	~BSoftSynth();
131fbaf754aSmahlzeit 
132d1c4420eSmahlzeit 	void Init();
133d1c4420eSmahlzeit 	void Done();
134*e3a74a24SJérôme Duval 	static void PlayBuffer(void * cookie, void * data, size_t size, const media_raw_audio_format & format);
135d1c4420eSmahlzeit 
136*e3a74a24SJérôme Duval 	bool fInitCheck;
137*e3a74a24SJérôme Duval 	char* fInstrumentsFile;
138*e3a74a24SJérôme Duval 	int32 fSampleRate;
139*e3a74a24SJérôme Duval 	interpolation_mode fInterpMode;
140*e3a74a24SJérôme Duval 	int16 fMaxVoices;
141*e3a74a24SJérôme Duval 	int16 fLimiterThreshold;
142*e3a74a24SJérôme Duval 	reverb_mode fReverbMode;
143*e3a74a24SJérôme Duval 	bool fReverbEnabled;
144d1c4420eSmahlzeit 
145*e3a74a24SJérôme Duval 	fluid_synth_t *fSynth;
146*e3a74a24SJérôme Duval 	fluid_settings_t* fSettings;
147d1c4420eSmahlzeit 
148*e3a74a24SJérôme Duval 	BSoundPlayer *fSoundPlayer;
149fbaf754aSmahlzeit };
150fbaf754aSmahlzeit 
151fbaf754aSmahlzeit } // namespace BPrivate
152fbaf754aSmahlzeit 
153fbaf754aSmahlzeit #endif // _SYNTH_CONSUMER_H
154