1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 /*
24 * This code is based on IBXM mod player
25 *
26 * Copyright (c) 2015, Martin Cameron
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or
30 * without modification, are permitted provided that the
31 * following conditions are met:
32 *
33 * * Redistributions of source code must retain the above
34 * copyright notice, this list of conditions and the
35 * following disclaimer.
36 *
37 * * Redistributions in binary form must reproduce the
38 * above copyright notice, this list of conditions and the
39 * following disclaimer in the documentation and/or other
40 * materials provided with the distribution.
41 *
42 * * Neither the name of the organization nor the names of
43 * its contributors may be used to endorse or promote
44 * products derived from this software without specific
45 * prior written permission.
46
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
48 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
49 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
55 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
56 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
57 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 *
62 */
63
64 #include "common/debug.h"
65 #include "common/file.h"
66 #include "common/memstream.h"
67
68 #include "audio/audiostream.h"
69 #include "audio/mods/mod_xm_s3m.h"
70 #include "audio/mods/module_mod_xm_s3m.h"
71
72 namespace Modules {
73
74 class ModXmS3mStream : public Audio::RewindableAudioStream {
75 private:
76 struct Channel {
77 Instrument *instrument;
78 Sample *sample;
79 Note note;
80 int keyOn, randomSeed, plRow;
81 int sampleOff, sampleIdx, sampleFra, freq, ampl, pann;
82 int volume, panning, fadeoutVol, volEnvTick, panEnvTick;
83 int period, portaPeriod, retrigCount, fxCount, avCount;
84 int portaUpParam, portaDownParam, tonePortaParam, offsetParam;
85 int finePortaUpParam, finePortaDownParam, xfinePortaParam;
86 int arpeggioParam, volSlideParam, gvolSlideParam, panSlideParam;
87 int fineVslideUpParam, fineVslideDownParam;
88 int retrigVolume, retrigTicks, tremorOnTicks, tremorOffTicks;
89 int vibratoType, vibratoPhase, vibratoSpeed, vibratoDepth;
90 int tremoloType, tremoloPhase, tremoloSpeed, tremoloDepth;
91 int tremoloAdd, vibratoAdd, arpeggioAdd;
92 };
93
94 ModuleModXmS3m _module;
95 bool _loadSuccess;
96 int _sampleRate, _interpolation, _globalVol;
97 int _seqPos, _breakPos, _row, _nextRow, _tick;
98 int _speed, _tempo, _plCount, _plChan;
99 int *_rampBuf;
100 int8 **_playCount;
101 Channel *_channels;
102 int _dataLeft;
103 int _initialDataLength;
104 bool _finished;
105
106 // mix buffer to keep a partially consumed decoded tick.
107 int *_mixBuffer;
108 int _mixBufferSamples; // number of samples kept in _mixBuffer
109
110 static const int FP_SHIFT;
111 static const int FP_ONE;
112 static const int FP_MASK;
113 static const int16 sinetable[];
114
115 int calculateDuration();
calculateTickLength() const116 int calculateTickLength() const { return (_sampleRate * 5) / (_tempo * 2); }
calculateMixBufLength() const117 int calculateMixBufLength() const { return (calculateTickLength() + 65) * 4; }
118
119 int initPlayCount(int8 **playCount);
120 void setSequencePos(int pos);
121 int tick();
122 void updateRow();
123 int seek(int samplePos);
rewind()124 bool rewind() override { setSequencePos(0); _dataLeft = _initialDataLength; return true; }
125
126 // Sample
127 void downsample(int *buf, int count);
128 void resample(const Channel &channel, int *mixBuf, int offset, int count, int sampleRate);
129 void updateSampleIdx(Channel &channel, int count, int sampleRate);
130
131 // Channel
132 void initChannel(int idx);
133 void tickChannel(Channel &channel);
134 void updateChannelRow(Channel &channel, Note note);
135
136 // Effects
137 int waveform(Channel &channel, int phase, int type);
138 void vibrato(Channel &channel, int fine);
139 void autoVibrato(Channel &channel);
140 void portaUp(Channel &channel, int param);
141 void portaDown(Channel &channel, int param);
142 void tonePorta(Channel &channel);
143 void volumeSlide(Channel &channel);
144 void retrigVolSlide(Channel &channel);
145 void tremolo(Channel &channel);
146 void tremor(Channel &channel);
147 void trigger(Channel &channel);
148 void calculateFreq(Channel &channel);
149 void calculateAmpl(Channel &channel);
150
151 // Envelopes
152 void updateEnvelopes(Channel &channel);
153 int envelopeNextTick(const Envelope &envelope, int tick, int keyOn);
154 int calculateAmpl(const Envelope &envelope, int tick);
155
156 // Read stream
157 int getAudio(int *mixBuf);
158 void volumeRamp(int *mixBuf, int tickLen);
159
160 public:
161 // Check if module loading succeeds
loadSuccess() const162 bool loadSuccess() const { return _loadSuccess; }
163
164 // Implement virtual functions
165 virtual int readBuffer(int16 *buffer, const int numSamples) override;
isStereo() const166 virtual bool isStereo() const override { return true; }
getRate() const167 virtual int getRate() const override { return _sampleRate; }
endOfData() const168 virtual bool endOfData() const override { return _dataLeft <= 0; }
169
170 ModXmS3mStream(Common::SeekableReadStream *stream, int initialPos, int rate, int interpolation);
171 ~ModXmS3mStream();
172 };
173
174 const int ModXmS3mStream::FP_SHIFT = 0xF;
175 const int ModXmS3mStream::FP_ONE = 0x8000;
176 const int ModXmS3mStream::FP_MASK = 0x7FFF;
177 const short ModXmS3mStream::sinetable[] = {
178 0, 24, 49, 74, 97, 120, 141, 161, 180, 197, 212, 224, 235, 244, 250, 253,
179 255, 253, 250, 244, 235, 224, 212, 197, 180, 161, 141, 120, 97, 74, 49, 24
180 };
181
ModXmS3mStream(Common::SeekableReadStream * stream,int initialPos,int rate,int interpolation)182 ModXmS3mStream::ModXmS3mStream(Common::SeekableReadStream *stream, int initialPos, int rate, int interpolation) :
183 _rampBuf(nullptr), _playCount(nullptr), _channels(nullptr),
184 _mixBuffer(nullptr), _sampleRate(rate), _interpolation(interpolation),
185 _seqPos(initialPos), _mixBufferSamples(0), _finished(false) {
186 if (!_module.load(*stream)) {
187 warning("It's not a valid Mod/S3m/Xm sound file");
188 _loadSuccess = false;
189 return;
190 }
191
192 _loadSuccess = true;
193 _rampBuf = new int[128];
194 _channels = new Channel[_module.numChannels];
195 _initialDataLength = _dataLeft = calculateDuration() * 4; // stereo and uint16
196 }
197
~ModXmS3mStream()198 ModXmS3mStream::~ModXmS3mStream() {
199 if (_rampBuf) {
200 delete[] _rampBuf;
201 _rampBuf = nullptr;
202 }
203
204 if (_playCount) {
205 delete[] _playCount;
206 _playCount = nullptr;
207 }
208
209 if (_channels) {
210 delete[] _channels;
211 _channels = nullptr;
212 }
213
214 if (_mixBuffer) {
215 delete []_mixBuffer;
216 _mixBuffer = nullptr;
217 }
218 }
219
initPlayCount(int8 ** playCount)220 int ModXmS3mStream::initPlayCount(int8 **playCount) {
221 int len = 0;
222 for (int idx = 0; idx < _module.sequenceLen; ++idx) {
223 int pat = _module.sequence[idx];
224 int rows = (pat < _module.numPatterns) ? _module.patterns[pat].numRows : 0;
225 if (playCount) {
226 playCount[idx] = playCount[0] ? &playCount[0][len] : nullptr;
227 }
228 len += rows;
229 }
230 return len;
231 }
232
initChannel(int idx)233 void ModXmS3mStream::initChannel(int idx) {
234 memset(&_channels[idx], 0, sizeof(Channel));
235 _channels[idx].panning = _module.defaultPanning[idx];
236 _channels[idx].instrument = &_module.instruments[0];
237 _channels[idx].sample = &_module.instruments[0].samples[0];
238 _channels[idx].randomSeed = (idx + 1) * 0xABCDEF;
239 }
240
tickChannel(Channel & channel)241 void ModXmS3mStream::tickChannel(Channel &channel) {
242 channel.vibratoAdd = 0;
243 channel.fxCount++;
244 channel.retrigCount++;
245 if (!(channel.note.effect == 0x7D && channel.fxCount <= channel.note.param)) {
246 switch (channel.note.volume & 0xF0) {
247 case 0x60: /* Vol Slide Down.*/
248 channel.volume -= channel.note.volume & 0xF;
249 if (channel.volume < 0) {
250 channel.volume = 0;
251 }
252 break;
253 case 0x70: /* Vol Slide Up.*/
254 channel.volume += channel.note.volume & 0xF;
255 if (channel.volume > 64) {
256 channel.volume = 64;
257 }
258 break;
259 case 0xB0: /* Vibrato.*/
260 channel.vibratoPhase += channel.vibratoSpeed;
261 vibrato(channel, 0);
262 break;
263 case 0xD0: /* Pan Slide Left.*/
264 channel.panning -= channel.note.volume & 0xF;
265 if (channel.panning < 0) {
266 channel.panning = 0;
267 }
268 break;
269 case 0xE0: /* Pan Slide Right.*/
270 channel.panning += channel.note.volume & 0xF;
271 if (channel.panning > 255) {
272 channel.panning = 255;
273 }
274 break;
275 case 0xF0: /* Tone Porta.*/
276 tonePorta(channel);
277 break;
278 default:
279 break;
280 }
281 }
282 switch (channel.note.effect) {
283 case 0x01:
284 case 0x86: /* Porta Up. */
285 portaUp(channel, channel.portaUpParam);
286 break;
287 case 0x02:
288 case 0x85: /* Porta Down. */
289 portaDown(channel, channel.portaDownParam);
290 break;
291 case 0x03:
292 case 0x87: /* Tone Porta. */
293 tonePorta(channel);
294 break;
295 case 0x04:
296 case 0x88: /* Vibrato. */
297 channel.vibratoPhase += channel.vibratoSpeed;
298 vibrato(channel, 0);
299 break;
300 case 0x05:
301 case 0x8C: /* Tone Porta + Vol Slide. */
302 tonePorta(channel);
303 volumeSlide(channel);
304 break;
305 case 0x06:
306 case 0x8B: /* Vibrato + Vol Slide. */
307 channel.vibratoPhase += channel.vibratoSpeed;
308 vibrato(channel, 0);
309 volumeSlide(channel);
310 break;
311 case 0x07:
312 case 0x92: /* Tremolo. */
313 channel.tremoloPhase += channel.tremoloSpeed;
314 tremolo(channel);
315 break;
316 case 0x0A:
317 case 0x84: /* Vol Slide. */
318 volumeSlide(channel);
319 break;
320 case 0x11: /* Global Volume Slide. */
321 _globalVol = _globalVol + (channel.gvolSlideParam >> 4) - (channel.gvolSlideParam & 0xF);
322 if (_globalVol < 0) {
323 _globalVol = 0;
324 }
325 if (_globalVol > 64) {
326 _globalVol = 64;
327 }
328 break;
329 case 0x19: /* Panning Slide. */
330 channel.panning = channel.panning + (channel.panSlideParam >> 4) - (channel.panSlideParam & 0xF);
331 if (channel.panning < 0) {
332 channel.panning = 0;
333 }
334 if (channel.panning > 255) {
335 channel.panning = 255;
336 }
337 break;
338 case 0x1B:
339 case 0x91: /* Retrig + Vol Slide. */
340 retrigVolSlide(channel);
341 break;
342 case 0x1D:
343 case 0x89: /* Tremor. */
344 tremor(channel);
345 break;
346 case 0x79: /* Retrig. */
347 if (channel.fxCount >= channel.note.param) {
348 channel.fxCount = 0;
349 channel.sampleIdx = channel.sampleFra = 0;
350 }
351 break;
352 case 0x7C:
353 case 0xFC: /* Note Cut. */
354 if (channel.note.param == channel.fxCount) {
355 channel.volume = 0;
356 }
357 break;
358 case 0x7D:
359 case 0xFD: /* Note Delay. */
360 if (channel.note.param == channel.fxCount) {
361 trigger(channel);
362 }
363 break;
364 case 0x8A: /* Arpeggio. */
365 if (channel.fxCount == 1) {
366 channel.arpeggioAdd = channel.arpeggioParam >> 4;
367 } else if (channel.fxCount == 2) {
368 channel.arpeggioAdd = channel.arpeggioParam & 0xF;
369 } else {
370 channel.arpeggioAdd = channel.fxCount = 0;
371 }
372 break;
373 case 0x95: /* Fine Vibrato. */
374 channel.vibratoPhase += channel.vibratoSpeed;
375 vibrato(channel, 1);
376 break;
377 default:
378 break;
379 }
380 autoVibrato(channel);
381 calculateFreq(channel);
382 calculateAmpl(channel);
383 updateEnvelopes(channel);
384 }
385
volumeSlide(Channel & channel)386 void ModXmS3mStream::volumeSlide(Channel &channel) {
387 int up = channel.volSlideParam >> 4;
388 int down = channel.volSlideParam & 0xF;
389 if (down == 0xF && up > 0) {
390 /* Fine slide up.*/
391 if (channel.fxCount == 0) {
392 channel.volume += up;
393 }
394 } else if (up == 0xF && down > 0) {
395 /* Fine slide down.*/
396 if (channel.fxCount == 0) {
397 channel.volume -= down;
398 }
399 } else if (channel.fxCount > 0 || _module.fastVolSlides) {
400 /* Normal.*/
401 channel.volume += up - down;
402 }
403 if (channel.volume > 64) {
404 channel.volume = 64;
405 }
406 if (channel.volume < 0) {
407 channel.volume = 0;
408 }
409 }
410
portaUp(Channel & channel,int param)411 void ModXmS3mStream::portaUp(Channel &channel, int param) {
412 switch (param & 0xF0) {
413 case 0xE0: /* Extra-fine porta.*/
414 if (channel.fxCount == 0) {
415 channel.period -= param & 0xF;
416 }
417 break;
418 case 0xF0: /* Fine porta.*/
419 if (channel.fxCount == 0) {
420 channel.period -= (param & 0xF) << 2;
421 }
422 break;
423 default:/* Normal porta.*/
424 if (channel.fxCount > 0) {
425 channel.period -= param << 2;
426 }
427 break;
428 }
429 if (channel.period < 0) {
430 channel.period = 0;
431 }
432 }
433
portaDown(Channel & channel,int param)434 void ModXmS3mStream::portaDown(Channel &channel, int param) {
435 if (channel.period > 0) {
436 switch (param & 0xF0) {
437 case 0xE0: /* Extra-fine porta.*/
438 if (channel.fxCount == 0) {
439 channel.period += param & 0xF;
440 }
441 break;
442 case 0xF0: /* Fine porta.*/
443 if (channel.fxCount == 0) {
444 channel.period += (param & 0xF) << 2;
445 }
446 break;
447 default:/* Normal porta.*/
448 if (channel.fxCount > 0) {
449 channel.period += param << 2;
450 }
451 break;
452 }
453 if (channel.period > 65535) {
454 channel.period = 65535;
455 }
456 }
457 }
458
tonePorta(Channel & channel)459 void ModXmS3mStream::tonePorta(Channel &channel) {
460 if (channel.period > 0) {
461 if (channel.period < channel.portaPeriod) {
462 channel.period += channel.tonePortaParam << 2;
463 if (channel.period > channel.portaPeriod) {
464 channel.period = channel.portaPeriod;
465 }
466 } else {
467 channel.period -= channel.tonePortaParam << 2;
468 if (channel.period < channel.portaPeriod) {
469 channel.period = channel.portaPeriod;
470 }
471 }
472 }
473 }
474
waveform(Channel & channel,int phase,int type)475 int ModXmS3mStream::waveform(Channel &channel, int phase, int type) {
476 int amplitude = 0;
477 switch (type) {
478 default: /* Sine. */
479 amplitude = sinetable[phase & 0x1F];
480 if ((phase & 0x20) > 0) {
481 amplitude = -amplitude;
482 }
483 break;
484 case 6: /* Saw Up.*/
485 amplitude = (((phase + 0x20) & 0x3F) << 3) - 255;
486 break;
487 case 1:
488 case 7: /* Saw Down. */
489 amplitude = 255 - (((phase + 0x20) & 0x3F) << 3);
490 break;
491 case 2:
492 case 5: /* Square. */
493 amplitude = (phase & 0x20) > 0 ? 255 : -255;
494 break;
495 case 3:
496 case 8: /* Random. */
497 amplitude = (channel.randomSeed >> 20) - 255;
498 channel.randomSeed = (channel.randomSeed * 65 + 17) & 0x1FFFFFFF;
499 break;
500 }
501 return amplitude;
502 }
503
vibrato(Channel & channel,int fine)504 void ModXmS3mStream::vibrato(Channel &channel, int fine) {
505 int wave = waveform(channel, channel.vibratoPhase, channel.vibratoType & 0x3);
506 channel.vibratoAdd = wave * channel.vibratoDepth >> (fine ? 7 : 5);
507 }
508
tremolo(Channel & channel)509 void ModXmS3mStream::tremolo(Channel &channel) {
510 int wave = waveform(channel, channel.tremoloPhase, channel.tremoloType & 0x3);
511 channel.tremoloAdd = wave * channel.tremoloDepth >> 6;
512 }
513
tremor(Channel & channel)514 void ModXmS3mStream::tremor(Channel &channel) {
515 if (channel.retrigCount >= channel.tremorOnTicks) {
516 channel.tremoloAdd = -64;
517 }
518 if (channel.retrigCount >= (channel.tremorOnTicks + channel.tremorOffTicks)) {
519 channel.tremoloAdd = channel.retrigCount = 0;
520 }
521 }
522
retrigVolSlide(Channel & channel)523 void ModXmS3mStream::retrigVolSlide(Channel &channel) {
524 if (channel.retrigCount >= channel.retrigTicks) {
525 channel.retrigCount = channel.sampleIdx = channel.sampleFra = 0;
526 switch (channel.retrigVolume) {
527 case 0x1:
528 channel.volume = channel.volume - 1;
529 break;
530 case 0x2:
531 channel.volume = channel.volume - 2;
532 break;
533 case 0x3:
534 channel.volume = channel.volume - 4;
535 break;
536 case 0x4:
537 channel.volume = channel.volume - 8;
538 break;
539 case 0x5:
540 channel.volume = channel.volume - 16;
541 break;
542 case 0x6:
543 channel.volume = channel.volume * 2 / 3;
544 break;
545 case 0x7:
546 channel.volume = channel.volume >> 1;
547 break;
548 case 0x8: /* ? */
549 break;
550 case 0x9:
551 channel.volume = channel.volume + 1;
552 break;
553 case 0xA:
554 channel.volume = channel.volume + 2;
555 break;
556 case 0xB:
557 channel.volume = channel.volume + 4;
558 break;
559 case 0xC:
560 channel.volume = channel.volume + 8;
561 break;
562 case 0xD:
563 channel.volume = channel.volume + 16;
564 break;
565 case 0xE:
566 channel.volume = channel.volume * 3 / 2;
567 break;
568 case 0xF:
569 channel.volume = channel.volume << 1;
570 break;
571 default:
572 break;
573 }
574 if (channel.volume < 0) {
575 channel.volume = 0;
576 }
577 if (channel.volume > 64) {
578 channel.volume = 64;
579 }
580 }
581 }
582
trigger(Channel & channel)583 void ModXmS3mStream::trigger(Channel &channel) {
584 int ins = channel.note.instrument;
585 if (ins > 0 && ins <= _module.numInstruments) {
586 channel.instrument = &_module.instruments[ins];
587 int key = channel.note.key < 97 ? channel.note.key : 0;
588 int sam = channel.instrument->keyToSample[key];
589 Sample *sample = &channel.instrument->samples[sam];
590 channel.volume = sample->volume >= 64 ? 64 : sample->volume & 0x3F;
591 if (sample->panning > 0) {
592 channel.panning = (sample->panning - 1) & 0xFF;
593 }
594 if (channel.period > 0 && sample->loopLength > 1) {
595 /* Amiga trigger.*/
596 channel.sample = sample;
597 }
598 channel.sampleOff = 0;
599 channel.volEnvTick = channel.panEnvTick = 0;
600 channel.fadeoutVol = 32768;
601 channel.keyOn = 1;
602 }
603 if (channel.note.effect == 0x09 || channel.note.effect == 0x8F) {
604 /* Set Sample Offset. */
605 if (channel.note.param > 0) {
606 channel.offsetParam = channel.note.param;
607 }
608 channel.sampleOff = channel.offsetParam << 8;
609 }
610 if (channel.note.volume >= 0x10 && channel.note.volume < 0x60) {
611 channel.volume = channel.note.volume < 0x50 ? channel.note.volume - 0x10 : 64;
612 }
613 switch (channel.note.volume & 0xF0) {
614 case 0x80: /* Fine Vol Down.*/
615 channel.volume -= channel.note.volume & 0xF;
616 if (channel.volume < 0) {
617 channel.volume = 0;
618 }
619 break;
620 case 0x90: /* Fine Vol Up.*/
621 channel.volume += channel.note.volume & 0xF;
622 if (channel.volume > 64) {
623 channel.volume = 64;
624 }
625 break;
626 case 0xA0: /* Set Vibrato Speed.*/
627 if ((channel.note.volume & 0xF) > 0) {
628 channel.vibratoSpeed = channel.note.volume & 0xF;
629 }
630 break;
631 case 0xB0: /* Vibrato.*/
632 if ((channel.note.volume & 0xF) > 0) {
633 channel.vibratoDepth = channel.note.volume & 0xF;
634 }
635 vibrato(channel, 0);
636 break;
637 case 0xC0: /* Set Panning.*/
638 channel.panning = (channel.note.volume & 0xF) * 17;
639 break;
640 case 0xF0: /* Tone Porta.*/
641 if ((channel.note.volume & 0xF) > 0) {
642 channel.tonePortaParam = channel.note.volume & 0xF;
643 }
644 break;
645 default:
646 break;
647 }
648 if (channel.note.key > 0) {
649 if (channel.note.key > 96) {
650 channel.keyOn = 0;
651 } else {
652 int porta = (channel.note.volume & 0xF0) == 0xF0 || channel.note.effect == 0x03 || channel.note.effect == 0x05 || channel.note.effect == 0x87 || channel.note.effect == 0x8C;
653 if (!porta) {
654 ins = channel.instrument->keyToSample[channel.note.key];
655 channel.sample = &channel.instrument->samples[ins];
656 }
657 int finetune = channel.sample->finetune;
658 if (channel.note.effect == 0x75 || channel.note.effect == 0xF2) {
659 /* Set Fine Tune. */
660 finetune = ((channel.note.param & 0xF) << 4) - 128;
661 }
662 int key = channel.note.key + channel.sample->relNote;
663 if (key < 1) {
664 key = 1;
665 }
666 if (key > 120) {
667 key = 120;
668 }
669 int period = (key << 6) + (finetune >> 1);
670 if (_module.linearPeriods) {
671 channel.portaPeriod = 7744 - period;
672 } else {
673 channel.portaPeriod = 29021 * ModuleModXmS3m::moduleExp2((period << FP_SHIFT) / -768) >> FP_SHIFT;
674 }
675 if (!porta) {
676 channel.period = channel.portaPeriod;
677 channel.sampleIdx = channel.sampleOff;
678 channel.sampleFra = 0;
679 if (channel.vibratoType < 4) {
680 channel.vibratoPhase = 0;
681 }
682 if (channel.tremoloType < 4) {
683 channel.tremoloPhase = 0;
684 }
685 channel.retrigCount = channel.avCount = 0;
686 }
687 }
688 }
689 }
690
updateEnvelopes(Channel & channel)691 void ModXmS3mStream::updateEnvelopes(Channel &channel) {
692 if (channel.instrument->volEnv.enabled) {
693 if (!channel.keyOn) {
694 channel.fadeoutVol -= channel.instrument->volFadeout;
695 if (channel.fadeoutVol < 0) {
696 channel.fadeoutVol = 0;
697 }
698 }
699 channel.volEnvTick = envelopeNextTick(channel.instrument->volEnv, channel.volEnvTick, channel.keyOn);
700 }
701 if (channel.instrument->panEnv.enabled) {
702 channel.panEnvTick = envelopeNextTick(channel.instrument->panEnv, channel.panEnvTick, channel.keyOn);
703 }
704 }
705
autoVibrato(Channel & channel)706 void ModXmS3mStream::autoVibrato(Channel &channel) {
707 int depth = channel.instrument->vibDepth & 0x7F;
708 if (depth > 0) {
709 int sweep = channel.instrument->vibSweep & 0x7F;
710 int rate = channel.instrument->vibRate & 0x7F;
711 int type = channel.instrument->vibType;
712 if (channel.avCount < sweep) {
713 depth = depth * channel.avCount / sweep;
714 }
715 int wave = waveform(channel, channel.avCount * rate >> 2, type + 4);
716 channel.vibratoAdd += wave * depth >> 8;
717 channel.avCount++;
718 }
719 }
720
calculateFreq(Channel & channel)721 void ModXmS3mStream::calculateFreq(Channel &channel) {
722 int per = channel.period + channel.vibratoAdd;
723 if (_module.linearPeriods) {
724 per = per - (channel.arpeggioAdd << 6);
725 if (per < 28 || per > 7680) {
726 per = 7680;
727 }
728 channel.freq = ((_module.c2Rate >> 4) * ModuleModXmS3m::moduleExp2(((4608 - per) << FP_SHIFT) / 768)) >> (FP_SHIFT - 4);
729 } else {
730 if (per > 29021) {
731 per = 29021;
732 }
733 per = (per << FP_SHIFT) / ModuleModXmS3m::moduleExp2((channel.arpeggioAdd << FP_SHIFT) / 12);
734 if (per < 28) {
735 per = 29021;
736 }
737 channel.freq = _module.c2Rate * 1712 / per;
738 }
739 }
740
calculateAmpl(Channel & channel)741 void ModXmS3mStream::calculateAmpl(Channel &channel) {
742 int envPan = 32, envVol = channel.keyOn ? 64 : 0;
743 if (channel.instrument->volEnv.enabled) {
744 envVol = calculateAmpl(channel.instrument->volEnv, channel.volEnvTick);
745 }
746 int vol = channel.volume + channel.tremoloAdd;
747 if (vol > 64) {
748 vol = 64;
749 }
750 if (vol < 0) {
751 vol = 0;
752 }
753 vol = (vol * _module.gain * FP_ONE) >> 13;
754 vol = (vol * channel.fadeoutVol) >> 15;
755 channel.ampl = (vol * _globalVol * envVol) >> 12;
756 if (channel.instrument->panEnv.enabled) {
757 envPan = calculateAmpl(channel.instrument->panEnv, channel.panEnvTick);
758 }
759 int range = (channel.panning < 128) ? channel.panning : (255 - channel.panning);
760 channel.pann = channel.panning + (range * (envPan - 32) >> 5);
761 }
762
updateChannelRow(Channel & channel,Note note)763 void ModXmS3mStream::updateChannelRow(Channel &channel, Note note) {
764 channel.note = note;
765 channel.retrigCount++;
766 channel.vibratoAdd = channel.tremoloAdd = channel.arpeggioAdd = channel.fxCount = 0;
767 if (!((note.effect == 0x7D || note.effect == 0xFD) && note.param > 0)) {
768 /* Not note delay.*/
769 trigger(channel);
770 }
771 switch (channel.note.effect) {
772 case 0x01:
773 case 0x86: /* Porta Up. */
774 if (channel.note.param > 0) {
775 channel.portaUpParam = channel.note.param;
776 }
777 portaUp(channel, channel.portaUpParam);
778 break;
779 case 0x02:
780 case 0x85: /* Porta Down. */
781 if (channel.note.param > 0) {
782 channel.portaDownParam = channel.note.param;
783 }
784 portaDown(channel, channel.portaDownParam);
785 break;
786 case 0x03:
787 case 0x87: /* Tone Porta. */
788 if (channel.note.param > 0) {
789 channel.tonePortaParam = channel.note.param;
790 }
791 break;
792 case 0x04:
793 case 0x88: /* Vibrato. */
794 if ((channel.note.param >> 4) > 0) {
795 channel.vibratoSpeed = channel.note.param >> 4;
796 }
797 if ((channel.note.param & 0xF) > 0) {
798 channel.vibratoDepth = channel.note.param & 0xF;
799 }
800 vibrato(channel, 0);
801 break;
802 case 0x05:
803 case 0x8C: /* Tone Porta + Vol Slide. */
804 if (channel.note.param > 0) {
805 channel.volSlideParam = channel.note.param;
806 }
807 volumeSlide(channel);
808 break;
809 case 0x06:
810 case 0x8B: /* Vibrato + Vol Slide. */
811 if (channel.note.param > 0) {
812 channel.volSlideParam = channel.note.param;
813 }
814 vibrato(channel, 0);
815 volumeSlide(channel);
816 break;
817 case 0x07:
818 case 0x92: /* Tremolo. */
819 if ((channel.note.param >> 4) > 0) {
820 channel.tremoloSpeed = channel.note.param >> 4;
821 }
822 if ((channel.note.param & 0xF) > 0) {
823 channel.tremoloDepth = channel.note.param & 0xF;
824 }
825 tremolo(channel);
826 break;
827 case 0x08: /* Set Panning.*/
828 channel.panning = channel.note.param & 0xFF;
829 break;
830 case 0x0A:
831 case 0x84: /* Vol Slide. */
832 if (channel.note.param > 0) {
833 channel.volSlideParam = channel.note.param;
834 }
835 volumeSlide(channel);
836 break;
837 case 0x0C: /* Set Volume. */
838 channel.volume = channel.note.param >= 64 ? 64 : channel.note.param & 0x3F;
839 break;
840 case 0x10:
841 case 0x96: /* Set Global Volume. */
842 _globalVol = channel.note.param >= 64 ? 64 : channel.note.param & 0x3F;
843 break;
844 case 0x11: /* Global Volume Slide. */
845 if (channel.note.param > 0) {
846 channel.gvolSlideParam = channel.note.param;
847 }
848 break;
849 case 0x14: /* Key Off. */
850 channel.keyOn = 0;
851 break;
852 case 0x15: /* Set Envelope Tick. */
853 channel.volEnvTick = channel.panEnvTick = channel.note.param & 0xFF;
854 break;
855 case 0x19: /* Panning Slide. */
856 if (channel.note.param > 0) {
857 channel.panSlideParam = channel.note.param;
858 }
859 break;
860 case 0x1B:
861 case 0x91: /* Retrig + Vol Slide. */
862 if ((channel.note.param >> 4) > 0) {
863 channel.retrigVolume = channel.note.param >> 4;
864 }
865 if ((channel.note.param & 0xF) > 0) {
866 channel.retrigTicks = channel.note.param & 0xF;
867 }
868 retrigVolSlide(channel);
869 break;
870 case 0x1D:
871 case 0x89: /* Tremor. */
872 if ((channel.note.param >> 4) > 0) {
873 channel.tremorOnTicks = channel.note.param >> 4;
874 }
875 if ((channel.note.param & 0xF) > 0) {
876 channel.tremorOffTicks = channel.note.param & 0xF;
877 }
878 tremor(channel);
879 break;
880 case 0x21: /* Extra Fine Porta. */
881 if (channel.note.param > 0) {
882 channel.xfinePortaParam = channel.note.param;
883 }
884 switch (channel.xfinePortaParam & 0xF0) {
885 case 0x10:
886 portaUp(channel, 0xE0 | (channel.xfinePortaParam & 0xF));
887 break;
888 case 0x20:
889 portaDown(channel, 0xE0 | (channel.xfinePortaParam & 0xF));
890 break;
891 default:
892 break;
893 }
894 break;
895 case 0x71: /* Fine Porta Up. */
896 if (channel.note.param > 0) {
897 channel.finePortaUpParam = channel.note.param;
898 }
899 portaUp(channel, 0xF0 | (channel.finePortaUpParam & 0xF));
900 break;
901 case 0x72: /* Fine Porta Down. */
902 if (channel.note.param > 0) {
903 channel.finePortaDownParam = channel.note.param;
904 }
905 portaDown(channel, 0xF0 | (channel.finePortaDownParam & 0xF));
906 break;
907 case 0x74:
908 case 0xF3: /* Set Vibrato Waveform. */
909 if (channel.note.param < 8) {
910 channel.vibratoType = channel.note.param;
911 }
912 break;
913 case 0x77:
914 case 0xF4: /* Set Tremolo Waveform. */
915 if (channel.note.param < 8) {
916 channel.tremoloType = channel.note.param;
917 }
918 break;
919 case 0x7A: /* Fine Vol Slide Up. */
920 if (channel.note.param > 0) {
921 channel.fineVslideUpParam = channel.note.param;
922 }
923 channel.volume += channel.fineVslideUpParam;
924 if (channel.volume > 64) {
925 channel.volume = 64;
926 }
927 break;
928 case 0x7B: /* Fine Vol Slide Down. */
929 if (channel.note.param > 0) {
930 channel.fineVslideDownParam = channel.note.param;
931 }
932 channel.volume -= channel.fineVslideDownParam;
933 if (channel.volume < 0) {
934 channel.volume = 0;
935 }
936 break;
937 case 0x7C:
938 case 0xFC: /* Note Cut. */
939 if (channel.note.param <= 0) {
940 channel.volume = 0;
941 }
942 break;
943 case 0x8A: /* Arpeggio. */
944 if (channel.note.param > 0) {
945 channel.arpeggioParam = channel.note.param;
946 }
947 break;
948 case 0x95: /* Fine Vibrato.*/
949 if ((channel.note.param >> 4) > 0) {
950 channel.vibratoSpeed = channel.note.param >> 4;
951 }
952 if ((channel.note.param & 0xF) > 0) {
953 channel.vibratoDepth = channel.note.param & 0xF;
954 }
955 vibrato(channel, 1);
956 break;
957 case 0xF8: /* Set Panning. */
958 channel.panning = channel.note.param * 17;
959 break;
960 default:
961 break;
962 }
963 autoVibrato(channel);
964 calculateFreq(channel);
965 calculateAmpl(channel);
966 updateEnvelopes(channel);
967 }
968
tick()969 int ModXmS3mStream::tick() {
970 int count = 1;
971 if (--_tick <= 0) {
972 _tick = _speed;
973 updateRow();
974 } else {
975 for (int idx = 0; idx < _module.numChannels; idx++) {
976 tickChannel(_channels[idx]);
977 }
978 }
979 if (_playCount && _playCount[0]) {
980 count = _playCount[_seqPos][_row] - 1;
981 }
982 return count;
983 }
984
updateRow()985 void ModXmS3mStream::updateRow() {
986 if (_nextRow < 0) {
987 _breakPos = _seqPos + 1;
988 _nextRow = 0;
989 }
990 if (_breakPos >= 0) {
991 _finished = false;
992 if (_breakPos >= _module.sequenceLen) {
993 // Hit the end.
994 _breakPos = _nextRow = 0;
995 _finished = true;
996 }
997 while (_module.sequence[_breakPos] >= _module.numPatterns) {
998 _breakPos++;
999 if (_breakPos >= _module.sequenceLen) {
1000 // Hit the end.
1001 _breakPos = _nextRow = 0;
1002 _finished = true;
1003 }
1004 }
1005 _seqPos = _breakPos;
1006 for (int idx = 0; idx < _module.numChannels; idx++) {
1007 _channels[idx].plRow = 0;
1008 }
1009 _breakPos = -1;
1010 }
1011 const Pattern &pattern = _module.patterns[_module.sequence[_seqPos]];
1012 _row = _nextRow;
1013 if (_row >= pattern.numRows) {
1014 _row = 0;
1015 }
1016 if (_playCount && _playCount[0]) {
1017 int count = _playCount[_seqPos][_row];
1018 if (_plCount < 0 && count < 127) {
1019 _playCount[_seqPos][_row] = count + 1;
1020 }
1021 }
1022 _nextRow = _row + 1;
1023 if (_nextRow >= pattern.numRows) {
1024 _nextRow = -1;
1025 }
1026 for (int idx = 0; idx < _module.numChannels; ++idx) {
1027 Note note = pattern.getNote(_row, idx);
1028 if (note.effect == 0xE) {
1029 note.effect = 0x70 | (note.param >> 4);
1030 note.param &= 0xF;
1031 }
1032 if (note.effect == 0x93) {
1033 note.effect = 0xF0 | (note.param >> 4);
1034 note.param &= 0xF;
1035 }
1036 if (note.effect == 0 && note.param > 0) {
1037 note.effect = 0x8A;
1038 }
1039
1040 Channel &channel = _channels[idx];
1041 updateChannelRow(channel, note);
1042 switch (note.effect) {
1043 case 0x81: /* Set Speed. */
1044 if (note.param > 0) {
1045 _tick = _speed = note.param;
1046 }
1047 break;
1048 case 0xB:
1049 case 0x82: /* Pattern Jump.*/
1050 if (_plCount < 0) {
1051 _breakPos = note.param;
1052 _nextRow = 0;
1053 }
1054 break;
1055 case 0xD:
1056 case 0x83: /* Pattern Break.*/
1057 if (_plCount < 0) {
1058 if (_breakPos < 0) {
1059 _breakPos = _seqPos + 1;
1060 }
1061 _nextRow = (note.param >> 4) * 10 + (note.param & 0xF);
1062 }
1063 break;
1064 case 0xF: /* Set Speed/Tempo.*/
1065 if (note.param > 0) {
1066 if (note.param < 32) {
1067 _tick = _speed = note.param;
1068 } else {
1069 _tempo = note.param;
1070 }
1071 }
1072 break;
1073 case 0x94: /* Set Tempo.*/
1074 if (note.param > 32) {
1075 _tempo = note.param;
1076 }
1077 break;
1078 case 0x76:
1079 case 0xFB: /* Pattern Loop.*/
1080 if (note.param == 0) {
1081 /* Set loop marker on this channel. */
1082 channel.plRow = _row;
1083 }
1084 if (channel.plRow < _row && _breakPos < 0) {
1085 /* Marker valid. */
1086 if (_plCount < 0) {
1087 /* Not already looping, begin. */
1088 _plCount = note.param;
1089 _plChan = idx;
1090 }
1091 if (_plChan == idx) {
1092 /* Next Loop.*/
1093 if (_plCount == 0) {
1094 /* Loop finished. Invalidate current marker. */
1095 channel.plRow = _row + 1;
1096 } else {
1097 /* Loop. */
1098 _nextRow = channel.plRow;
1099 }
1100 _plCount--;
1101 }
1102 }
1103 break;
1104 case 0x7E:
1105 case 0xFE: /* Pattern Delay.*/
1106 _tick = _speed + _speed * note.param;
1107 break;
1108 default:
1109 break;
1110 }
1111 }
1112 }
1113
envelopeNextTick(const Envelope & envelope,int currentTick,int keyOn)1114 int ModXmS3mStream::envelopeNextTick(const Envelope &envelope, int currentTick, int keyOn) {
1115 int nextTick = currentTick + 1;
1116 if (envelope.looped && nextTick >= envelope.loopEndTick) {
1117 nextTick = envelope.loopStartTick;
1118 }
1119 if (envelope.sustain && keyOn && nextTick >= envelope.sustainTick) {
1120 nextTick = envelope.sustainTick;
1121 }
1122 return nextTick;
1123 }
1124
calculateAmpl(const Envelope & envelope,int currentTick)1125 int ModXmS3mStream::calculateAmpl(const Envelope &envelope, int currentTick) {
1126 int idx, point, dt, da;
1127 int ampl = envelope.pointsAmpl[envelope.numPoints - 1];
1128 if (currentTick < envelope.pointsTick[envelope.numPoints - 1]) {
1129 point = 0;
1130 for (idx = 1; idx < envelope.numPoints; idx++) {
1131 if (envelope.pointsTick[idx] <= currentTick) {
1132 point = idx;
1133 }
1134 }
1135 dt = envelope.pointsTick[point + 1] - envelope.pointsTick[point];
1136 da = envelope.pointsAmpl[point + 1] - envelope.pointsAmpl[point];
1137 ampl = envelope.pointsAmpl[point];
1138 ampl += ((da << 24) / dt) * (currentTick - envelope.pointsTick[point]) >> 24;
1139 }
1140 return ampl;
1141 }
1142
calculateDuration()1143 int ModXmS3mStream::calculateDuration() {
1144 int count = 0, duration = 0;
1145 setSequencePos(0);
1146 // Count the length of sequences until we hit a bit we've played before (or the end).
1147 while (count < 1) {
1148 duration += calculateTickLength();
1149 count = tick();
1150 }
1151 setSequencePos(0);
1152 return duration;
1153 }
1154
1155 /* Seek to approximately the specified sample position.
1156 The actual sample position reached is returned. */
seek(int samplePos)1157 int ModXmS3mStream::seek(int samplePos) {
1158 int currentPos = 0;
1159 setSequencePos(0);
1160 int tickLen = calculateTickLength();
1161 while ((samplePos - currentPos) >= tickLen) {
1162 for (int idx = 0; idx < _module.numChannels; ++idx) {
1163 updateSampleIdx(_channels[idx], tickLen * 2, _sampleRate * 2);
1164 }
1165 currentPos += tickLen;
1166 tick();
1167 tickLen = calculateTickLength();
1168 }
1169 return currentPos;
1170 }
1171
resample(const Channel & channel,int * mixBuf,int offset,int count,int sampleRate)1172 void ModXmS3mStream::resample(const Channel &channel, int *mixBuf, int offset, int count, int sampleRate) {
1173 Sample *sample = channel.sample;
1174 int lGain = 0, rGain = 0, samIdx = 0, samFra = 0, step = 0;
1175 int loopLen = 0, loopEnd = 0, outIdx = 0, outEnd = 0, y = 0, m = 0, c = 0;
1176 int16 *sampleData = channel.sample->data;
1177 if (channel.ampl > 0) {
1178 lGain = channel.ampl * (255 - channel.pann) >> 8;
1179 rGain = channel.ampl * channel.pann >> 8;
1180 samIdx = channel.sampleIdx;
1181 samFra = channel.sampleFra;
1182 step = (channel.freq << (FP_SHIFT - 3)) / (sampleRate >> 3);
1183 loopLen = sample->loopLength;
1184 loopEnd = sample->loopStart + loopLen;
1185 outIdx = offset * 2;
1186 outEnd = (offset + count) * 2;
1187 if (_interpolation) {
1188 while (outIdx < outEnd) {
1189 if (samIdx >= loopEnd) {
1190 if (loopLen > 1) {
1191 while (samIdx >= loopEnd) {
1192 samIdx -= loopLen;
1193 }
1194 } else {
1195 break;
1196 }
1197 }
1198 c = sampleData[samIdx];
1199 m = sampleData[samIdx + 1] - c;
1200 y = ((m * samFra) >> FP_SHIFT) + c;
1201 mixBuf[outIdx++] += (y * lGain) >> FP_SHIFT;
1202 mixBuf[outIdx++] += (y * rGain) >> FP_SHIFT;
1203 samFra += step;
1204 samIdx += samFra >> FP_SHIFT;
1205 samFra &= FP_MASK;
1206 }
1207 } else {
1208 while (outIdx < outEnd) {
1209 if (samIdx >= loopEnd) {
1210 if (loopLen > 1) {
1211 while (samIdx >= loopEnd) {
1212 samIdx -= loopLen;
1213 }
1214 } else {
1215 break;
1216 }
1217 }
1218 if (samIdx < 0)
1219 samIdx = 0;
1220 y = sampleData[samIdx];
1221 mixBuf[outIdx++] += (y * lGain) >> FP_SHIFT;
1222 mixBuf[outIdx++] += (y * rGain) >> FP_SHIFT;
1223 samFra += step;
1224 samIdx += samFra >> FP_SHIFT;
1225 samFra &= FP_MASK;
1226 }
1227 }
1228 }
1229 }
1230
updateSampleIdx(Channel & channel,int count,int sampleRate)1231 void ModXmS3mStream::updateSampleIdx(Channel &channel, int count, int sampleRate) {
1232 Sample *sample = channel.sample;
1233 int step = (channel.freq << (FP_SHIFT - 3)) / (sampleRate >> 3);
1234 channel.sampleFra += step * count;
1235 channel.sampleIdx += channel.sampleFra >> FP_SHIFT;
1236 if (channel.sampleIdx > (int)sample->loopStart) {
1237 if (sample->loopLength > 1) {
1238 channel.sampleIdx = sample->loopStart + (channel.sampleIdx - sample->loopStart) % sample->loopLength;
1239 } else {
1240 channel.sampleIdx = sample->loopStart;
1241 }
1242 }
1243 channel.sampleFra &= FP_MASK;
1244 }
1245
volumeRamp(int * mixBuf,int tickLen)1246 void ModXmS3mStream::volumeRamp(int *mixBuf, int tickLen) {
1247 int rampRate = 256 * 2048 / _sampleRate;
1248 for (int idx = 0, a1 = 0; a1 < 256; idx += 2, a1 += rampRate) {
1249 int a2 = 256 - a1;
1250 mixBuf[idx] = (mixBuf[idx] * a1 + _rampBuf[idx] * a2) >> 8;
1251 mixBuf[idx + 1] = (mixBuf[idx + 1] * a1 + _rampBuf[idx + 1] * a2) >> 8;
1252 }
1253 memcpy(_rampBuf, &mixBuf[tickLen * 2], 128 * sizeof(int));
1254 }
1255
1256 /* 2:1 downsampling with simple but effective anti-aliasing. Buf must contain count * 2 + 1 stereo samples. */
downsample(int * buf,int count)1257 void ModXmS3mStream::downsample(int *buf, int count) {
1258 int outLen = count * 2;
1259 for (int idx = 0, outIdx = 0; outIdx < outLen; idx += 4, outIdx += 2) {
1260 buf[outIdx] = (buf[idx] >> 2) + (buf[idx + 2] >> 1) + (buf[idx + 4] >> 2);
1261 buf[outIdx + 1] = (buf[idx + 1] >> 2) + (buf[idx + 3] >> 1) + (buf[idx + 5] >> 2);
1262 }
1263 }
1264
1265 /* Generates audio and returns the number of stereo samples written into mixBuf. */
getAudio(int * mixBuf)1266 int ModXmS3mStream::getAudio(int *mixBuf) {
1267 if (_mixBuffer) {
1268 memcpy(mixBuf, _mixBuffer, _mixBufferSamples * sizeof(int));
1269 delete []_mixBuffer;
1270 _mixBuffer = nullptr;
1271 return _mixBufferSamples;
1272 }
1273
1274 int tickLen = calculateTickLength();
1275 /* Clear output buffer. */
1276 memset(mixBuf, 0, (tickLen + 65) * 4 * sizeof(int));
1277 /* Resample. */
1278 for (int idx = 0; idx < _module.numChannels; idx++) {
1279 Channel &channel = _channels[idx];
1280 resample(channel, mixBuf, 0, (tickLen + 65) * 2, _sampleRate * 2);
1281 updateSampleIdx(channel, tickLen * 2, _sampleRate * 2);
1282 }
1283 downsample(mixBuf, tickLen + 64);
1284 volumeRamp(mixBuf, tickLen);
1285 tick();
1286 return tickLen * 2; // stereo samples
1287 }
1288
readBuffer(int16 * buffer,const int numSamples)1289 int ModXmS3mStream::readBuffer(int16 *buffer, const int numSamples) {
1290 int samplesRead = 0;
1291 while (samplesRead < numSamples && _dataLeft > 0) {
1292 int *mixBuf = new int[calculateMixBufLength()];
1293 int samples = getAudio(mixBuf);
1294 if (samplesRead + samples > numSamples) {
1295 _mixBufferSamples = samplesRead + samples - numSamples;
1296 samples -= _mixBufferSamples;
1297 _mixBuffer = new int[_mixBufferSamples];
1298 memcpy(_mixBuffer, mixBuf + samples, _mixBufferSamples * sizeof(int));
1299 }
1300 for (int idx = 0; idx < samples; ++idx) {
1301 int ampl = mixBuf[idx];
1302 if (ampl > 32767) {
1303 ampl = 32767;
1304 }
1305 if (ampl < -32768) {
1306 ampl = -32768;
1307 }
1308 *buffer++ = ampl;
1309 }
1310 samplesRead += samples;
1311
1312 _dataLeft -= samples * 2;
1313
1314 delete []mixBuf; // free
1315 }
1316
1317 if (_dataLeft <= 0 && !_finished) {
1318 //
1319 // The original data length calculation was used up but didn't hit
1320 // the end, so this mod loops.
1321 // From here can just reset data left to some sensible value, but
1322 // it's actually infinite and we'll come back here.
1323 //
1324 _dataLeft = _initialDataLength;
1325 }
1326
1327 return samplesRead;
1328 }
1329
setSequencePos(int pos)1330 void ModXmS3mStream::setSequencePos(int pos) {
1331 if (pos >= _module.sequenceLen) {
1332 pos = 0;
1333 }
1334 _breakPos = pos;
1335 _nextRow = 0;
1336 _tick = 1;
1337 _globalVol = _module.defaultGvol;
1338 _speed = _module.defaultSpeed > 0 ? _module.defaultSpeed : 6;
1339 _tempo = _module.defaultTempo > 0 ? _module.defaultTempo : 125;
1340 _plCount = _plChan = -1;
1341
1342 // play count
1343 if (_playCount) {
1344 delete[] _playCount[0];
1345 delete[] _playCount;
1346 }
1347 _playCount = new int8 *[_module.sequenceLen];
1348 memset(_playCount, 0, _module.sequenceLen * sizeof(int8 *));
1349 int len = initPlayCount(_playCount);
1350 _playCount[0] = new int8[len];
1351 memset(_playCount[0], 0, len * sizeof(int8));
1352 initPlayCount(_playCount);
1353
1354 for (int idx = 0; idx < _module.numChannels; ++idx) {
1355 initChannel(idx);
1356 }
1357 memset(_rampBuf, 0, 128 * sizeof(int));
1358 tick();
1359 }
1360
1361 } // End of namespace Modules
1362
1363 namespace Audio {
1364
makeModXmS3mStream(Common::SeekableReadStream * stream,DisposeAfterUse::Flag disposeAfterUse,int initialPos,int rate,int interpolation)1365 RewindableAudioStream *makeModXmS3mStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, int initialPos, int rate, int interpolation) {
1366 Modules::ModXmS3mStream *soundStream = new Modules::ModXmS3mStream(stream, initialPos, rate, interpolation);
1367
1368 if (disposeAfterUse == DisposeAfterUse::YES)
1369 delete stream;
1370
1371 if (!soundStream->loadSuccess()) {
1372 delete soundStream;
1373 return nullptr;
1374 }
1375
1376 return soundStream;
1377 }
1378
1379 } // End of namespace Audio
1380