aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/libopenmpt/libopenmpt_ext_impl.cpp
blob: 1de804bb8a0f56441da20cf7e7e5d23f66819113 (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * libopenmpt_ext_impl.cpp
 * -----------------------
 * Purpose: libopenmpt extensions - implementation
 * Notes  :
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */

#include "common/stdafx.h"

#include "libopenmpt_internal.h"
#include "libopenmpt_ext.hpp"

#include "libopenmpt_ext_impl.hpp"

#include "mpt/base/saturate_round.hpp"

#include "soundlib/Sndfile.h"

// assume OPENMPT_NAMESPACE is OpenMPT

namespace openmpt {

	module_ext_impl::module_ext_impl( callback_stream_wrapper stream, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( stream, std::move(log), ctls ) {
		ctor();
	}
	module_ext_impl::module_ext_impl( std::istream & stream, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( stream, std::move(log), ctls ) {
		ctor();
	}
	module_ext_impl::module_ext_impl( const std::vector<std::byte> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( data, std::move(log), ctls ) {
		ctor();
	}
	module_ext_impl::module_ext_impl( const std::vector<std::uint8_t> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( data, std::move(log), ctls ) {
		ctor();
	}
	module_ext_impl::module_ext_impl( const std::vector<char> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( data, std::move(log), ctls ) {
		ctor();
	}
	module_ext_impl::module_ext_impl( const std::byte * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( data, size, std::move(log), ctls ) {
		ctor();
	}
	module_ext_impl::module_ext_impl( const std::uint8_t * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( data, size, std::move(log), ctls ) {
		ctor();
	}
	module_ext_impl::module_ext_impl( const char * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( data, size, std::move(log), ctls ) {
		ctor();
	}
	module_ext_impl::module_ext_impl( const void * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : module_impl( data, size, std::move(log), ctls ) {
		ctor();
	}



	void module_ext_impl::ctor() {



		/* add stuff here */



	}



	module_ext_impl::~module_ext_impl() {



		/* add stuff here */



	}



	void * module_ext_impl::get_interface( const std::string & interface_id ) {
		if ( interface_id.empty() ) {
			return 0;
		} else if ( interface_id == ext::pattern_vis_id ) {
			return dynamic_cast< ext::pattern_vis * >( this );
		} else if ( interface_id == ext::interactive_id ) {
			return dynamic_cast< ext::interactive * >( this );
		} else if ( interface_id == ext::interactive2_id ) {
			return dynamic_cast< ext::interactive2 * >( this );



			/* add stuff here */



		} else {
			return 0;
		}
	}

	// pattern_vis

	module_ext_impl::effect_type module_ext_impl::get_pattern_row_channel_volume_effect_type( std::int32_t pattern, std::int32_t row, std::int32_t channel ) const {
		std::uint8_t byte = get_pattern_row_channel_command( pattern, row, channel, module::command_volumeffect );
		switch ( OpenMPT::ModCommand::GetVolumeEffectType( byte ) ) {
			case OpenMPT::EFFECT_TYPE_NORMAL : return effect_general; break;
			case OpenMPT::EFFECT_TYPE_GLOBAL : return effect_global ; break;
			case OpenMPT::EFFECT_TYPE_VOLUME : return effect_volume ; break;
			case OpenMPT::EFFECT_TYPE_PANNING: return effect_panning; break;
			case OpenMPT::EFFECT_TYPE_PITCH  : return effect_pitch  ; break;
			default: return effect_unknown; break;
		}
	}

	module_ext_impl::effect_type module_ext_impl::get_pattern_row_channel_effect_type( std::int32_t pattern, std::int32_t row, std::int32_t channel ) const {
		std::uint8_t byte = get_pattern_row_channel_command( pattern, row, channel, module::command_effect );
		switch (OpenMPT::ModCommand::GetEffectType( byte ) ) {
			case OpenMPT::EFFECT_TYPE_NORMAL : return effect_general; break;
			case OpenMPT::EFFECT_TYPE_GLOBAL : return effect_global ; break;
			case OpenMPT::EFFECT_TYPE_VOLUME : return effect_volume ; break;
			case OpenMPT::EFFECT_TYPE_PANNING: return effect_panning; break;
			case OpenMPT::EFFECT_TYPE_PITCH  : return effect_pitch  ; break;
			default: return effect_unknown; break;
		}
	}

	// interactive

	void module_ext_impl::set_current_speed( std::int32_t speed ) {
		if ( speed < 1 || speed > 65535 ) {
			throw openmpt::exception("invalid tick count");
		}
		m_sndFile->m_PlayState.m_nMusicSpeed = speed;
	}

	void module_ext_impl::set_current_tempo( std::int32_t tempo ) {
		if ( tempo < 32 || tempo > 512 ) {
			throw openmpt::exception("invalid tempo");
		}
		m_sndFile->m_PlayState.m_nMusicTempo.Set( tempo );
	}

	void module_ext_impl::set_tempo_factor( double factor ) {
		if ( factor <= 0.0 || factor > 4.0 ) {
			throw openmpt::exception("invalid tempo factor");
		}
		m_sndFile->m_nTempoFactor = mpt::saturate_round<uint32_t>( 65536.0 / factor );
		m_sndFile->RecalculateSamplesPerTick();
	}

	double module_ext_impl::get_tempo_factor( ) const {
		return 65536.0 / m_sndFile->m_nTempoFactor;
	}

	void module_ext_impl::set_pitch_factor( double factor ) {
		if ( factor <= 0.0 || factor > 4.0 ) {
			throw openmpt::exception("invalid pitch factor");
		}
		m_sndFile->m_nFreqFactor = mpt::saturate_round<uint32_t>( 65536.0 * factor );
		m_sndFile->RecalculateSamplesPerTick();
	}

	double module_ext_impl::get_pitch_factor( ) const {
		return m_sndFile->m_nFreqFactor / 65536.0;
	}

	void module_ext_impl::set_global_volume( double volume ) {
		if ( volume < 0.0 || volume > 1.0 ) {
			throw openmpt::exception("invalid global volume");
		}
		m_sndFile->m_PlayState.m_nGlobalVolume = mpt::saturate_round<uint32_t>( volume * MAX_GLOBAL_VOLUME );
	}

	double module_ext_impl::get_global_volume( ) const {
		return m_sndFile->m_PlayState.m_nGlobalVolume / static_cast<double>( MAX_GLOBAL_VOLUME );
	}
	
	void module_ext_impl::set_channel_volume( std::int32_t channel, double volume ) {
		if ( channel < 0 || channel >= get_num_channels() ) {
			throw openmpt::exception("invalid channel");
		}
		if ( volume < 0.0 || volume > 1.0 ) {
			throw openmpt::exception("invalid global volume");
		}
		m_sndFile->m_PlayState.Chn[channel].nGlobalVol = mpt::saturate_round<std::int32_t>(volume * 64.0);
	}

	double module_ext_impl::get_channel_volume( std::int32_t channel ) const {
		if ( channel < 0 || channel >= get_num_channels() ) {
			throw openmpt::exception("invalid channel");
		}
		return m_sndFile->m_PlayState.Chn[channel].nGlobalVol / 64.0;
	}

	void module_ext_impl::set_channel_mute_status( std::int32_t channel, bool mute ) {
		if ( channel < 0 || channel >= get_num_channels() ) {
			throw openmpt::exception("invalid channel");
		}
		m_sndFile->ChnSettings[channel].dwFlags.set( OpenMPT::CHN_MUTE | OpenMPT::CHN_SYNCMUTE , mute );
		m_sndFile->m_PlayState.Chn[channel].dwFlags.set( OpenMPT::CHN_MUTE | OpenMPT::CHN_SYNCMUTE , mute );

		// Also update NNA channels
		for ( OpenMPT::CHANNELINDEX i = m_sndFile->GetNumChannels(); i < OpenMPT::MAX_CHANNELS; i++)
		{
			if ( m_sndFile->m_PlayState.Chn[i].nMasterChn == channel + 1)
			{
				m_sndFile->m_PlayState.Chn[i].dwFlags.set( OpenMPT::CHN_MUTE | OpenMPT::CHN_SYNCMUTE, mute );
			}
		}
	}

	bool module_ext_impl::get_channel_mute_status( std::int32_t channel ) const {
		if ( channel < 0 || channel >= get_num_channels() ) {
			throw openmpt::exception("invalid channel");
		}
		return m_sndFile->m_PlayState.Chn[channel].dwFlags[OpenMPT::CHN_MUTE | OpenMPT::CHN_SYNCMUTE];
	}
	
	void module_ext_impl::set_instrument_mute_status( std::int32_t instrument, bool mute ) {
		const bool instrument_mode = get_num_instruments() != 0;
		const std::int32_t max_instrument = instrument_mode ? get_num_instruments() : get_num_samples();
		if ( instrument < 0 || instrument >= max_instrument ) {
			throw openmpt::exception("invalid instrument");
		}
		if ( instrument_mode ) {
			if ( m_sndFile->Instruments[instrument + 1] != nullptr ) {
				m_sndFile->Instruments[instrument + 1]->dwFlags.set( OpenMPT::INS_MUTE, mute );
			}
		} else {
			m_sndFile->GetSample( static_cast<OpenMPT::SAMPLEINDEX>( instrument + 1 ) ).uFlags.set( OpenMPT::CHN_MUTE, mute ) ;
		}
	}

	bool module_ext_impl::get_instrument_mute_status( std::int32_t instrument ) const {
		const bool instrument_mode = get_num_instruments() != 0;
		const std::int32_t max_instrument = instrument_mode ? get_num_instruments() : get_num_samples();
		if ( instrument < 0 || instrument >= max_instrument ) {
			throw openmpt::exception("invalid instrument");
		}
		if ( instrument_mode ) {
			if ( m_sndFile->Instruments[instrument + 1] != nullptr ) {
				return m_sndFile->Instruments[instrument + 1]->dwFlags[OpenMPT::INS_MUTE];
			}
			return true;
		} else {
			return m_sndFile->GetSample( static_cast<OpenMPT::SAMPLEINDEX>( instrument + 1 ) ).uFlags[OpenMPT::CHN_MUTE];
		}
	}

	std::int32_t module_ext_impl::play_note( std::int32_t instrument, std::int32_t note, double volume, double panning ) {
		const bool instrument_mode = get_num_instruments() != 0;
		const std::int32_t max_instrument = instrument_mode ? get_num_instruments() : get_num_samples();
		if ( instrument < 0 || instrument >= max_instrument ) {
			throw openmpt::exception("invalid instrument");
		}
		note += OpenMPT::NOTE_MIN;
		if ( note < OpenMPT::NOTE_MIN || note > OpenMPT::NOTE_MAX ) {
			throw openmpt::exception("invalid note");
		}

		// Find a free channel
		OpenMPT::CHANNELINDEX free_channel = m_sndFile->GetNNAChannel( OpenMPT::CHANNELINDEX_INVALID );
		if ( free_channel == OpenMPT::CHANNELINDEX_INVALID ) {
			free_channel = OpenMPT::MAX_CHANNELS - 1;
		}

		OpenMPT::ModChannel &chn = m_sndFile->m_PlayState.Chn[free_channel];
		chn.Reset( OpenMPT::ModChannel::resetTotal, *m_sndFile, OpenMPT::CHANNELINDEX_INVALID, OpenMPT::CHN_MUTE );
		chn.nMasterChn = 0;	// remove NNA association
		chn.nNewNote = chn.nLastNote = static_cast<std::uint8_t>(note);
		chn.ResetEnvelopes();
		m_sndFile->InstrumentChange(chn, instrument + 1);
		chn.nFadeOutVol = 0x10000;
		m_sndFile->NoteChange(chn, note, false, true, true);
		chn.nPan = mpt::saturate_round<std::int32_t>( OpenMPT::Clamp( panning * 128.0, -128.0, 128.0 ) + 128.0 );
		chn.nVolume = mpt::saturate_round<std::int32_t>( OpenMPT::Clamp( volume * 256.0, 0.0, 256.0 ) );

		// Remove channel from list of mixed channels to fix https://bugs.openmpt.org/view.php?id=209
		// This is required because a previous note on the same channel might have just stopped playing,
		// but the channel is still in the mix list.
		// Since the channel volume / etc is only updated every tick in CSoundFile::ReadNote, and we
		// do not want to duplicate mixmode-dependant logic here, CSoundFile::CreateStereoMix may already
		// try to mix our newly set up channel at volume 0 if we don't remove it from the list.
		auto mix_begin = std::begin( m_sndFile->m_PlayState.ChnMix );
		auto mix_end = std::remove( mix_begin, mix_begin + m_sndFile->m_nMixChannels, free_channel );
		m_sndFile->m_nMixChannels = static_cast<OpenMPT::CHANNELINDEX>( std::distance( mix_begin, mix_end ) );

		return free_channel;
	}

	void module_ext_impl::stop_note( std::int32_t channel ) {
		if ( channel < 0 || channel >= OpenMPT::MAX_CHANNELS ) {
			throw openmpt::exception("invalid channel");
		}
		auto & chn = m_sndFile->m_PlayState.Chn[channel];
		chn.nLength = 0;
		chn.pCurrentSample = nullptr;
	}

	void module_ext_impl::note_off(int32_t channel ) {
		if ( channel < 0 || channel >= OpenMPT::MAX_CHANNELS ) {
			throw openmpt::exception( "invalid channel" );
		}
		auto & chn = m_sndFile->m_PlayState.Chn[channel];
		chn.dwFlags |= OpenMPT::CHN_KEYOFF;
	}

	void module_ext_impl::note_fade(int32_t channel ) {
		if ( channel < 0 || channel >= OpenMPT::MAX_CHANNELS ) {
			throw openmpt::exception( "invalid channel" );
		}
		auto & chn = m_sndFile->m_PlayState.Chn[channel];
		chn.dwFlags |= OpenMPT::CHN_NOTEFADE;
	}

	void module_ext_impl::set_channel_panning( int32_t channel, double panning ) {
		if ( channel < 0 || channel >= OpenMPT::MAX_CHANNELS ) {
			throw openmpt::exception( "invalid channel" );
		}
		auto & chn = m_sndFile->m_PlayState.Chn[channel];
		chn.nPan = mpt::saturate_round<int32_t>( std::clamp( panning, -1.0, 1.0 ) * 128.0 + 128.0 );
	}

	double module_ext_impl::get_channel_panning( int32_t channel ) {
		if ( channel < 0 || channel >= OpenMPT::MAX_CHANNELS ) {
			throw openmpt::exception( "invalid channel" );
		}
		auto & chn = m_sndFile->m_PlayState.Chn[channel];
		return ( chn.nPan - 128 ) / 128.0;
	}

	void module_ext_impl::set_note_finetune( int32_t channel, double finetune ) {
		if ( channel < 0 || channel >= OpenMPT::MAX_CHANNELS ) {
			throw openmpt::exception( "invalid channel" );
		}
		auto & chn = m_sndFile->m_PlayState.Chn[channel];
		chn.microTuning = mpt::saturate_round<int16_t>( finetune * 32768.0 );
	}

	double module_ext_impl::get_note_finetune( int32_t channel ) {
		if ( channel < 0 || channel >= OpenMPT::MAX_CHANNELS ) {
			throw openmpt::exception( "invalid channel" );
		}
		auto & chn = m_sndFile->m_PlayState.Chn[channel];
		return chn.microTuning / 32768.0;
	}

	/* add stuff here */



} // namespace openmpt