File: sound.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (907 lines) | stat: -rw-r--r-- 29,340 bytes parent folder | download | duplicates (2)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/system.h"
#include "common/substream.h"
#include "common/random.h"

#include "math/quat.h"

#include "audio/audiostream.h"
#include "audio/decoders/raw.h"
#include "audio/decoders/vorbis.h"

#include "engines/nancy/nancy.h"
#include "engines/nancy/sound.h"
#include "engines/nancy/iff.h"

#include "engines/nancy/state/scene.h"
#include "engines/nancy/state/map.h"

namespace Nancy {

enum SoundType {
	kSoundTypeDiamondware,
	kSoundTypeRaw,
	kSoundTypeOgg
};

bool readDiamondwareHeader(Common::SeekableReadStream *stream, SoundType &type, uint16 &numChannels,
					uint32 &samplesPerSec, uint16 &bitsPerSample, uint32 &size) {
	stream->skip(2);

	if (stream->readByte() != 1 || stream->readByte() > 1) {
		// Version, up to 1.1 is supported
		return false;
	}

	stream->skip(5); // sound id, reserved

	if (stream->readByte() != 0) {
		// Compression type, only uncompressed (0) is supported
		return false;
	}

	samplesPerSec = stream->readUint16LE();
	numChannels = stream->readByte();
	bitsPerSample = stream->readByte();
	stream->skip(2); // Absolute value of largest sample in file
	size = stream->readUint32LE();
	stream->skip(4); // Number of samples
	uint dataOffset = stream->readUint16LE();
	stream->seek(dataOffset);

	type = kSoundTypeDiamondware;

	return true;
}

bool readWaveHeader(Common::SeekableReadStream *stream, SoundType &type, uint16 &numChannels,
					uint32 &samplesPerSec, uint16 &bitsPerSample, uint32 &size) {
	// The earliest HIS files are just WAVE files with the first 22 bytes of
	// the file overwritten with a string, so most of this is copied from the
	// standard WAVE decoder
	numChannels = stream->readUint16LE();
	samplesPerSec = stream->readUint32LE();
	stream->skip(6);
	bitsPerSample = stream->readUint16LE();

	char buf[4 + 1];
	stream->read(buf, 4);
	buf[4] = 0;

	if (Common::String(buf) != "data") {
		warning("Data chunk not found in HIS file");
		return false;
	}

	size = stream->readUint32LE();

	if (stream->eos() || stream->err()) {
		warning("Error reading HIS file");
		return false;
	}

	type = kSoundTypeRaw;

	return true;
}

bool readHISHeader(Common::SeekableReadStream *stream, SoundType &type, uint16 &numChannels,
					uint32 &samplesPerSec, uint16 &bitsPerSample, uint32 &size) {
	uint32 ver;
	ver = stream->readUint16LE() << 16;
	ver |= stream->readUint16LE();
	bool hasType = false;

	switch (ver) {
	case 0x00010000:
		break;
	case 0x00020000:
		hasType = true;
		break;
	default:
		warning("Unsupported version %d.%d found in HIS file", ver >> 16, ver & 0xffff);
		return false;
	}

	// Same data as Wave fmt chunk
	stream->skip(2); // AudioFormat
	numChannels = stream->readUint16LE();
	samplesPerSec = stream->readUint32LE();
	stream->skip(6); // ByteRate and BlockAlign
	bitsPerSample = stream->readUint16LE();

	size = stream->readUint32LE();

	if (hasType) {
		uint16 tp = stream->readUint16LE();
		switch (tp) {
		case 1:
			type = kSoundTypeRaw;
			break;
		case 2:
			type = kSoundTypeOgg;
			break;
		default:
			warning("Unsupported sound type %d found in HIS file", tp);
			return false;
		}
	} else
		type = kSoundTypeRaw;

	if (stream->eos() || stream->err()) {
		warning("Error reading HIS file");
		return false;
	}

	return true;
}

uint getAdjustedVolume(uint volume) {
	if (g_nancy->getGameType() >= kGameTypeNancy3) {
		return 10 + (volume * 90) / 100;
	} else {
		return volume;
	}
}

Audio::SeekableAudioStream *SoundManager::makeHISStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 overrideSamplesPerSec) {
	char buf[22];

	stream->read(buf, 22);
	buf[21] = 0;
	Common::String headerID(buf);

	uint16 numChannels = 0, bitsPerSample = 0;
	uint32 samplesPerSec = 0, size = 0;
	SoundType type = kSoundTypeRaw;

	if (headerID == "DiamondWare Digitized") {
		if (!readDiamondwareHeader(stream, type, numChannels, samplesPerSec, bitsPerSample, size))
			return nullptr;
	} else if (headerID == "Her Interactive Sound") {
		// Early HIS file
		if (!readWaveHeader(stream, type, numChannels, samplesPerSec, bitsPerSample, size))
			return nullptr;
	} else if (headerID == "HIS") {
		stream->seek(4);
		if (!readHISHeader(stream, type, numChannels, samplesPerSec, bitsPerSample, size))
			return nullptr;
	}

	byte flags = 0;
	if (type == kSoundTypeRaw || type == kSoundTypeDiamondware) {
		if (bitsPerSample == 8) {			// 8 bit data is unsigned in HIS files and signed in DWD files
			flags |= (type == kSoundTypeRaw ? Audio::FLAG_UNSIGNED : Audio::FLAG_LITTLE_ENDIAN);
		} else if (bitsPerSample == 16) {	// 16 bit data is signed little endian
			flags |= (Audio::FLAG_16BITS | Audio::FLAG_LITTLE_ENDIAN);
		} else {
			warning("Unsupported bitsPerSample %d found in HIS file", bitsPerSample);
			return nullptr;
		}

		if (numChannels == 2) {
			flags |= Audio::FLAG_STEREO;
		} else if (numChannels != 1) {
			warning("Unsupported number of channels %d found in HIS file", numChannels);
			return nullptr;
		}

		// Raw PCM, make sure the last packet is complete
		uint sampleSize = (flags & Audio::FLAG_16BITS ? 2 : 1) * (flags & Audio::FLAG_STEREO ? 2 : 1);
		if (size % sampleSize != 0) {
			warning("Trying to play an %s file with an incomplete PCM packet", type == kSoundTypeDiamondware ? "DWD" : "HIS");
			size &= ~(sampleSize - 1);
		}
	}

	Common::SeekableSubReadStream *subStream = new Common::SeekableSubReadStream(
		stream, stream->pos(),
		type == kSoundTypeOgg ? stream->size() : stream->pos() + size,
		disposeAfterUse);

	if (type == kSoundTypeRaw || type == kSoundTypeDiamondware)
		return Audio::makeRawStream(subStream, overrideSamplesPerSec == 0 ? samplesPerSec : overrideSamplesPerSec, flags, DisposeAfterUse::YES);
	else
		return Audio::makeVorbisStream(subStream, DisposeAfterUse::YES);
}

SoundManager::SoundManager() : _shouldRecalculate(false), _mixer(g_system->getMixer()) {}

void SoundManager::loadCommonSounds(IFF *boot) {
	// Persistent sounds that are used across the engine. These originally get loaded inside Logo
	Common::String chunkNames[] = {
		"CANT", "CURT", "GLOB", "SLID", "BULS", "BUDE", "BUOK", "TH1", "TH2",
	};

	Common::SeekableReadStream *chunk = nullptr;
	for (auto const &s : chunkNames) {
		chunk = boot->getChunkStream(s);
		if (chunk) {
			SoundDescription &desc = _commonSounds.getOrCreateVal(s);
			desc.readNormal(*chunk);
			g_nancy->_sound->loadSound(desc);
			_channels[desc.channelID].isPersistent = true;

			delete chunk;
		}
	}

	// Menu sound is stored differently
	chunk = boot->getChunkStream("MSND"); // channel 28
	if (chunk) {
		SoundDescription &desc = _commonSounds.getOrCreateVal("MSND");
		desc.readMenu(*chunk);
		g_nancy->_sound->loadSound(desc);
		_channels[desc.channelID].isPersistent = true;

		delete chunk;
	}
}

SoundManager::~SoundManager() {
	stopAllSounds();
}

void SoundManager::loadSound(const SoundDescription &description, SoundEffectDescription **effectData, bool forceReload) {
	if (description.name == "NO SOUND") {
		return;
	}

	Channel &existing = _channels[description.channelID];
	if (!forceReload && existing.stream != nullptr) {
		// There's a channel already loaded. Check if we're trying to reload the exact same sound
		if (	description.name == existing.name &&
				description.numLoops == existing.numLoops &&
				description.playCommands == existing.playCommands) {

			// When the same sound is already playing at a different volume, adjust to new volume (nancy2 scene 599)
			if (existing.volume != getAdjustedVolume(description.volume)) {
				setVolume(description, description.volume);
			}

			return;
		}
	}

	if (_mixer->isSoundHandleActive(_channels[description.channelID].handle)) {
		_mixer->stopHandle(_channels[description.channelID].handle);
	}

	Channel &chan = _channels[description.channelID];

	delete chan.streamForMixer;
	chan.stream = nullptr;
	chan.streamForMixer = nullptr;

	chan.name = description.name;
	chan.playCommands = description.playCommands;
	chan.numLoops = description.numLoops;
	chan.volume = description.volume;
	chan.panAnchorFrame = description.panAnchorFrame;
	chan.isPanning = description.isPanning;

	if (effectData) {
		// Channel takes ownership of the effect data
		delete chan.effectData;
		chan.effectData = *effectData;
		*effectData = nullptr;
	}

	Common::Path path(description.name + (g_nancy->getGameType() == kGameTypeVampire ? ".dwd" : ".his"));
	Common::SeekableReadStream *file = SearchMan.createReadStreamForMember(path);
	if (file) {
		uint numLoops = chan.numLoops;
		if (chan.playCommands & kPlayRandomTime) {
			// We want to add randomized time delays between repeats, which is not doable with
			// a simple LoopingAudioStream. The delays are added in soundEffectMaintenance();
			numLoops = 1;

			// Decrement the number of loops since we start playing immediately after
			--chan.numLoops;
		}
		chan.stream = makeHISStream(file, DisposeAfterUse::YES, description.samplesPerSec);
		chan.streamForMixer = Audio::makeLoopingAudioStream(chan.stream, numLoops);
	}
}

void SoundManager::playSound(uint16 channelID) {
	if (channelID >= _channels.size() || _channels[channelID].stream == nullptr || isSoundPlaying(channelID))
		return;

	Channel &chan = _channels[channelID];
	chan.stream->seek(0);

	// Set a minimum volume (10 percent was chosen arbitrarily, but sounds reasonably close)
	// Fix for nancy3 scene 6112, but NOT a hack; the original engine also set a minimum volume for all sounds
	chan.volume = getAdjustedVolume(chan.volume);

	// Init 3D sound
	if (chan.playCommands & ~kPlaySequential && chan.effectData) {
		uint16 playCommands = chan.playCommands;

		if (playCommands & kPlayRandomPosition) {
			auto *rand = g_nancy->_randomSource;
			chan.position.set(
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinX, chan.effectData->randomMoveMaxX),
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinY, chan.effectData->randomMoveMaxY),
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinZ, chan.effectData->randomMoveMaxZ));
		}

		if (playCommands == kPlaySequentialPosition) {
			chan.position.set(chan.effectData->fixedPosX, chan.effectData->fixedPosY, chan.effectData->fixedPosZ);
		} else if (playCommands == kPlaySequentialFrameAnchor) {
			// Doesn't seem to be used so we skip implementing it
			warning("Sound play command kPlaySequentialFrameAnchor not implemented");
		} else if (playCommands == kPlayMoveLinear) {
			chan.position.set(chan.effectData->linearMoveStartX, chan.effectData->linearMoveStartY, chan.effectData->linearMoveStartZ);
			chan.positionDelta.set(chan.effectData->linearMoveEndX, chan.effectData->linearMoveEndY, chan.effectData->linearMoveEndZ);
			chan.positionDelta -= chan.position;
			chan.positionDelta /= chan.effectData->numMoveSteps;
			chan.nextStepTime = g_nancy->getTotalPlayTime() + chan.effectData->moveStepTime;
			chan.stepsLeft = chan.effectData->numMoveSteps;
		} else if (playCommands == kPlayRandomMove) {
			auto *rand = g_nancy->_randomSource;
			chan.position.set(
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinX, chan.effectData->randomMoveMaxX),
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinY, chan.effectData->randomMoveMaxY),
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinZ, chan.effectData->randomMoveMaxZ));

			chan.positionDelta.set(
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinX, chan.effectData->randomMoveMaxX),
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinY, chan.effectData->randomMoveMaxY),
				rand->getRandomNumberRngSigned(chan.effectData->randomMoveMinZ, chan.effectData->randomMoveMaxZ));

			chan.positionDelta -= chan.position;
			chan.positionDelta /= chan.effectData->numMoveSteps;
			chan.nextStepTime = g_nancy->getTotalPlayTime() + chan.effectData->moveStepTime;
			chan.stepsLeft = chan.effectData->numMoveSteps;
		} else if (playCommands == kPlayMoveCircular) {
			chan.position.set(chan.effectData->rotateMoveStartX, chan.effectData->rotateMoveStartY, chan.effectData->rotateMoveStartZ);
			chan.nextStepTime = g_nancy->getTotalPlayTime() + chan.effectData->moveStepTime;
			chan.stepsLeft = chan.effectData->numMoveSteps;
		}
	}

	_mixer->playStream(	chan.type,
						&chan.handle,
						chan.streamForMixer,
						channelID,
						(int)chan.volume * 255 / 100,
						0, DisposeAfterUse::NO);

	soundEffectMaintenance(channelID, true);
}

void SoundManager::playSound(const SoundDescription &description) {
	if (description.name != "NO SOUND") {
		playSound(description.channelID);
	}
}

void SoundManager::playSound(const Common::String &chunkName) {
	const SoundDescription &desc = _commonSounds[chunkName];

	if (!isSoundPlaying(desc)) {
		loadSound(desc);
	}

	playSound(desc);
}

void SoundManager::pauseSound(uint16 channelID, bool pause) {
	if (channelID >= _channels.size())
		return;

	if (isSoundPlaying(channelID)) {
		g_system->getMixer()->pauseHandle(_channels[channelID].handle, pause);
	}
}

void SoundManager::pauseSound(const SoundDescription &description, bool pause) {
	if (description.name != "NO SOUND") {
		pauseSound(description.channelID, pause);
	}
}

void SoundManager::pauseSound(const Common::String &chunkName, bool pause) {
	pauseSound(_commonSounds[chunkName], pause);
}

void SoundManager::pauseAllSounds(bool pause) {
	_mixer->pauseAll(pause);
}

bool SoundManager::isSoundPlaying(uint16 channelID) const {
	if (channelID >= _channels.size() || !_channels[channelID].stream)
		return false;

	const Channel &chan = _channels[channelID];
	if (chan.playCommands & kPlayRandomTime) {
		return _mixer->isSoundHandleActive(chan.handle) || chan.numLoops != 0;
	} else {
		return _mixer->isSoundHandleActive(chan.handle);
	}
}

bool SoundManager::isSoundPlaying(const SoundDescription &description) const {
	if (description.name == "NO SOUND") {
		return false;
	} else {
		return isSoundPlaying(description.channelID);
	}
}

bool SoundManager::isSoundPlaying(const Common::String &chunkName) const {
	return isSoundPlaying(_commonSounds[chunkName]);
}

void SoundManager::stopSound(uint16 channelID) {
	if (channelID >= _channels.size())
		return;

	Channel &chan = _channels[channelID];

	if (isSoundPlaying(channelID)) {
		_mixer->stopHandle(chan.handle);
	}

	// Persistent sounds only stop playing but do not get unloaded
	if (!chan.isPersistent) {
		chan.name = Common::String();
		delete chan.streamForMixer;
		chan.stream = nullptr;
		chan.streamForMixer = nullptr;
		delete chan.effectData;
		chan.effectData = nullptr;
		chan.position.set(0, 0, 0);
		chan.positionDelta.set(0, 0, 0);
		chan.stepsLeft = 0;
		chan.nextStepTime = 0;
	}
}

void SoundManager::stopSound(const SoundDescription &description) {
	if (description.name != "NO SOUND") {
		stopSound(description.channelID);
	}
}

void SoundManager::stopSound(const Common::String &chunkName) {
	stopSound(_commonSounds[chunkName]);
}

void SoundManager::stopAllSounds() {
	for (uint i = 0; i < _channels.size(); ++i) {
		stopSound(i);
	}
}

byte SoundManager::getVolume(uint16 channelID) {
	if (channelID >= _channels.size())
		return 0;

	return _mixer->getChannelVolume(_channels[channelID].handle);
}

byte SoundManager::getVolume(const SoundDescription &description) {
	if (description.name != "NO SOUND") {
		return getVolume(description.channelID);
	}

	return 0;
}

byte SoundManager::getVolume(const Common::String &chunkName) {
	return getVolume(_commonSounds[chunkName]);
}

void SoundManager::setVolume(uint16 channelID, uint16 volume) {
	if (channelID >= _channels.size() || !isSoundPlaying(channelID))
		return;

	_mixer->setChannelVolume(_channels[channelID].handle, getAdjustedVolume(volume) * 255 / 100);
}

void SoundManager::setVolume(const SoundDescription &description, uint16 volume) {
	if (description.name != "NO SOUND") {
		setVolume(description.channelID, volume);
	}
}

void SoundManager::setVolume(const Common::String &chunkName, uint16 volume) {
	setVolume(_commonSounds[chunkName], volume);
}

uint32 SoundManager::getRate(uint16 channelID) {
	if (channelID >= _channels.size())
		return 0;

	return _mixer->getChannelRate(_channels[channelID].handle);
}

uint32 SoundManager::getRate(const SoundDescription &description) {
	if (description.name != "NO SOUND") {
		return getRate(description.channelID);
	}

	return 0;
}

uint32 SoundManager::getRate(const Common::String &chunkName) {
	return getRate(_commonSounds[chunkName]);
}

uint32 SoundManager::getBaseRate(uint16 channelID) {
	if (channelID >= _channels.size() || !_channels[channelID].stream)
		return 0;

	return _channels[channelID].stream->getRate();
}

uint32 SoundManager::getBaseRate(const SoundDescription &description) {
	if (description.name != "NO SOUND") {
		return getBaseRate(description.channelID);
	}

	return 0;
}

uint32 SoundManager::getBaseRate(const Common::String &chunkName) {
	return getBaseRate(_commonSounds[chunkName]);
}

void SoundManager::setRate(uint16 channelID, uint32 rate) {
	if (channelID >= _channels.size())
		return;

	_mixer->setChannelRate(_channels[channelID].handle, rate);
}

void SoundManager::setRate(const SoundDescription &description, uint32 rate) {
	if (description.name != "NO SOUND") {
		setRate(description.channelID, rate);
	}
}

void SoundManager::setRate(const Common::String &chunkName, uint32 rate) {
	setRate(_commonSounds[chunkName], rate);
}

Audio::Timestamp SoundManager::getLength(uint16 channelID) {
	if (channelID >= _channels.size() || _channels[channelID].stream == nullptr) {
		return Audio::Timestamp();
	}

	return _channels[channelID].stream->getLength().convertToFramerate(getRate(channelID));
}

Audio::Timestamp SoundManager::getLength(const SoundDescription &description) {
	if (description.name != "NO SOUND") {
		return getLength(description.channelID);
	}

	return Audio::Timestamp();
}

Audio::Timestamp SoundManager::getLength(const Common::String &chunkName) {
	return getLength(_commonSounds[chunkName]);
}

void SoundManager::recalculateSoundEffects() {
	_shouldRecalculate = true;

	_positionLerp = 0;

	if (g_nancy->getGameType() >= kGameTypeNancy3) {
		const Nancy::State::Scene::SceneSummary &sceneSummary = NancySceneState.getSceneSummary();
		SceneChangeDescription &sceneInfo = NancySceneState.getSceneInfo();
		Math::Vector3d rotatedFrontVector = NancySceneState.getSceneInfo().listenerFrontVector;
		rotatedFrontVector.normalize();

		int rotation = sceneInfo.frontVectorFrameID < sceneInfo.frameID ?
			360 - (sceneInfo.frameID - sceneInfo.frontVectorFrameID) * sceneSummary.degreesPerRotation :
			(sceneInfo.frontVectorFrameID - sceneInfo.frameID) * sceneSummary.degreesPerRotation;

		Math::Quaternion quat = Math::Quaternion::yAxis(rotation);
		quat.transform(rotatedFrontVector);

		_orientation = rotatedFrontVector;

		for (uint i = 0; i < 3; ++i) {
			if (abs(_orientation.getValue(i)) < Math::epsilon) {
				_orientation.setValue(i, 0);
			}
		}
	}
}

void SoundManager::stopAndUnloadSceneSpecificSounds() {
	byte numSSChans = g_nancy->getStaticData().soundChannelInfo.numSceneSpecificChannels;

	if (g_nancy->getGameType() == kGameTypeVampire && Nancy::State::Map::hasInstance()) {
		// Don't stop the map sound in certain scenes
		uint nextScene = NancySceneState.getNextSceneInfo().sceneID;
		if (nextScene != 0 && (nextScene < 15 || nextScene > 27)) {
			stopSound(NancyMapState.getSound());
		}
	}

	for (uint i = 0; i < numSSChans; ++i) {
		stopSound(i);
	}

	stopSound("MSND");
}

void SoundManager::pauseSceneSpecificSounds(bool pause) {
	byte numSSChans = g_nancy->getStaticData().soundChannelInfo.numSceneSpecificChannels;
	if (g_nancy->getGameType() == kGameTypeVampire && Nancy::State::Map::hasInstance()) {
		if (!pause || g_nancy->getState() != NancyState::kMap) {
			// Stop the map sound in certain scenes
			uint currentScene = NancySceneState.getSceneInfo().sceneID;
			if (currentScene == 0 || (currentScene >= 15 && currentScene <= 27)) {
				g_nancy->_sound->pauseSound(NancyMapState.getSound(), pause);
			}
		}
	}

	for (uint i = 0; i < numSSChans; ++i) {
		g_nancy->_sound->pauseSound(i, pause);
	}
}

void SoundManager::initSoundChannels() {
	const SoundChannelInfo &channelInfo = g_nancy->getStaticData().soundChannelInfo;

	_channels.resize(channelInfo.numChannels);

	for (const short id : channelInfo.speechChannels) {
		_channels[id].type = Audio::Mixer::SoundType::kSpeechSoundType;
	}

	for (const short id : channelInfo.musicChannels) {
		_channels[id].type = Audio::Mixer::SoundType::kMusicSoundType;
	}

	for (const short id : channelInfo.sfxChannels) {
		_channels[id].type = Audio::Mixer::SoundType::kSFXSoundType;
	}
}

SoundManager::Channel::~Channel() {
	delete streamForMixer;
	delete effectData;
}

void SoundManager::soundEffectMaintenance() {
	// Interpolate position and rotation when scene has changed to avoid audible chop in sound
	if (_position != NancySceneState.getSceneSummary().listenerPosition && _positionLerp == 0) {
		++_positionLerp;
	}

	if (_positionLerp > 1) {
		++_positionLerp;
		if (_positionLerp > 10) {
			_position = NancySceneState.getSceneSummary().listenerPosition;
			_positionLerp = 0;
		}
	}

	for (uint i = 0; i < _channels.size(); ++i) {
		soundEffectMaintenance(i);
	}

	_shouldRecalculate = false;
}

void SoundManager::soundEffectMaintenance(uint16 channelID, bool force) {
	if (channelID >= _channels.size() || !isSoundPlaying(channelID))
		return;

	uint32 gameTime = g_nancy->getTotalPlayTime();
	Channel &chan = _channels[channelID];

	// Handle sound effects and 3D sound, which started being used from nancy3.
	// The original engine used DirectSound 3D, whose effects are only approximated.
	// In particular, there are some slight but noticeable differences in panning
	bool hasStepped = force;
	if (g_nancy->getGameType() >= 3 && chan.effectData) {
		uint16 playCommands = chan.playCommands;
		SoundEffectDescription *effectData = chan.effectData;

		// Handle randomized time delay between repeats
		if (playCommands & kPlayRandomTime && !_mixer->isSoundHandleActive(chan.handle) && chan.numLoops != 0) {
			if (chan.nextRepeatTime == 0) {
				// Channel just stopped playing, add a randomized delay
				chan.nextRepeatTime = g_nancy->_randomSource->getRandomNumberRng(effectData->minTimeDelay, effectData->maxTimeDelay) + gameTime;
			} else if (chan.nextRepeatTime < gameTime) {
				// Delay is over, start playing again
				_mixer->playStream(	chan.type,
						&chan.handle,
						chan.stream,
						channelID,
						chan.volume * 255 / 100,
						0, DisposeAfterUse::NO);

				--chan.numLoops;
				chan.nextRepeatTime = 0;
			}
		}

		// Move sound in space
		if (playCommands & kPlayMoveLinear && chan.stepsLeft && gameTime > chan.nextStepTime) {
			chan.nextStepTime = gameTime + chan.effectData->moveStepTime;
			--chan.stepsLeft;
			hasStepped = true;

			if (playCommands == kPlayMoveCircular) {
				// No real uses at least up to nancy5, so this is untested
				Math::Quaternion quat;
				switch (chan.effectData->rotateMoveAxis) {
				case kRotateAroundX:
					quat = Math::Quaternion::xAxis(360.0 / chan.effectData->numMoveSteps);
					break;
				case kRotateAroundY:
					quat = Math::Quaternion::yAxis(360.0 / chan.effectData->numMoveSteps);
					break;
				case kRotateAroundZ:
					quat = Math::Quaternion::zAxis(360.0 / chan.effectData->numMoveSteps);
					break;
				}

				quat.transform(chan.position);
			} else {
				chan.position += chan.positionDelta;
			}
		}
	}

	// Check if the player has moved OR if the sound itself has moved, OR, if we're still interpolating
	// Also, make sure we don't accidentally create a Scene state during game startup
	if (!State::Scene::hasInstance() || (!_shouldRecalculate && !hasStepped && _positionLerp == 0)) {
		return;
	}

	uint16 viewportFrameID = NancySceneState.getSceneInfo().frameID;

	// Old panning algorithm, used in The Vampire Diaries
	if (g_nancy->getGameType() <= kGameTypeNancy2 && chan.isPanning) {
		const State::Scene::SceneSummary &sceneSummary = NancySceneState.getSceneSummary();

		switch (sceneSummary.totalViewAngle) {
		case 180:
			_mixer->setChannelBalance(chan.handle, CLIP<int32>((viewportFrameID - chan.panAnchorFrame) * sceneSummary.degreesPerRotation * 364, -32768, 32767) / 256);
			break;
		case 360: {
			int16 adjustedViewportFrame = viewportFrameID - chan.panAnchorFrame;
			if (adjustedViewportFrame < 0) {
				adjustedViewportFrame += sceneSummary.numberOfVideoFrames;
			}

			// Divide the virtual space into quarters
			uint16 q1 = sceneSummary.numberOfVideoFrames / 4;
			uint16 q2 = sceneSummary.numberOfVideoFrames / 2;
			uint16 q3 = sceneSummary.numberOfVideoFrames * 3 / 4;

			float balance;

			if (adjustedViewportFrame < q1) {
				balance = (float)adjustedViewportFrame / q1;
				balance *= 32767;
				balance = 32768 - balance;
			} else if (adjustedViewportFrame < q2) {
				balance = (float)(adjustedViewportFrame - q1) / q1;
				balance *= 32767;
			} else if (adjustedViewportFrame < q3) {
				balance = (float)(adjustedViewportFrame - q2) / q1;
				balance *= 32767;
				balance += 32768;
			} else {
				balance = (float)(adjustedViewportFrame - q3) / q1;
				balance *= 32767;
				balance = 65535 - balance;
			}

			// The original engine's algorithm is broken and results in flipped
			// stereo; the following line fixes this bug
			balance = 65535 - balance;

			_mixer->setChannelBalance(chan.handle, (balance - 32768) / 256);
			break;
		}
		default:
			_mixer->setChannelBalance(chan.handle, 0);
			break;
		}
	}

	// Panning/volume/rate adjustment used in nancy3 and up. Originally handled by DirectSound 3D
	if (g_nancy->getGameType() >= 3 && chan.effectData &&
			(chan.playCommands & ~kPlaySequential) & (kPlaySequentialFrameAnchor | kPlayRandomPosition | kPlayMoveLinear)) {

		// Interpolate position when we've changed scenes
		Math::Vector3d listenerPos = Math::Vector3d::interpolate(_position, NancySceneState.getSceneSummary().listenerPosition, (float)_positionLerp / 10.0);
		float dist = listenerPos.getDistanceTo(chan.position);
		float volume;

		// Panning is linear, so we calculate it from the difference in degrees
		Math::Vector3d relativeSoundPos = chan.position - listenerPos;
		float pan = Math::Vector3d::angle(_orientation, relativeSoundPos.getNormalized()).getDegrees();
		if (pan > 90) {
			pan = 90 - (pan - 90);
		}

		pan /= 90;

		if (Math::Vector3d::crossProduct(_orientation, relativeSoundPos).y() < 0) {
			pan = -pan;
		}

		// Attenuate sound based on distance
		if (dist <= chan.effectData->minDistance) {
			volume = 255;
		} else if (dist >= chan.effectData->maxDistance) {
			volume = 255.0 / (2 * log2f(chan.effectData->maxDistance - chan.effectData->minDistance + 1));
		} else {
			float dlog = (2 * log2f(dist - chan.effectData->minDistance + 1));
			volume = 255.0 / dlog;

			// Sounds that are closer to the listener shouldn't pan as hard
			// note: slightly inaccurate, compare the ticking sound in nancy3 scene 4015
			pan -= pan / dlog;
		}

		// (Non-linearly) interpolate pan as well
		if (_positionLerp) {
			float lastPan = _mixer->getChannelBalance(chan.handle) / 127.0;
			pan = lastPan + (pan - lastPan) * ((float)_positionLerp / 10.0);
		}

		// Doppler effect is affected by the velocities of the source and listener,
		// as projected onto the vector between source and listener
		Math::Vector3d listenerToSource = chan.position - listenerPos;
		if (listenerToSource.isZero()) {
			return;
		}

		float projectedListenerVelocity, projectedSrcVelocity;

		float soundSpeed = 343.0;

		// It appears the original engine's devs either didn't know or didn't care
		// what the velocity parameters do, so they used the doubled orientation vector
		// as listener velocity, and the doubled position vector of the sound as source velocity.
		// This results in physically incorrect behavior which we replicate
		projectedListenerVelocity = Math::Vector3d::dotProduct(_orientation * 2, listenerToSource) / listenerToSource.length();
		projectedSrcVelocity = -Math::Vector3d::dotProduct(chan.position * 2, listenerToSource) / listenerToSource.length();

		// Calculate the final rate of the sound with doppler effect applied
		uint32 rate = chan.stream->getRate() * (1 + projectedListenerVelocity / soundSpeed) / (1 - projectedSrcVelocity / soundSpeed);

		_mixer->setChannelVolume(chan.handle, ((byte)volume * chan.volume) / 100);
		_mixer->setChannelBalance(chan.handle, pan * 127);
		_mixer->setChannelRate(chan.handle, rate);
	}
}

} // End of namespace Nancy