Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SoftwareDevice.cpp « devices « src « audaspace « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c16c75e8e36d967a1010d83c77dbb4ed69622ea (plain)
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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/*******************************************************************************
 * Copyright 2009-2016 Jörg Müller
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

#include "devices/SoftwareDevice.h"
#include "fx/PitchReader.h"
#include "respec/ChannelMapperReader.h"
#include "respec/JOSResampleReader.h"
#include "respec/LinearResampleReader.h"
#include "respec/Mixer.h"
#include "Exception.h"
#include "ISound.h"

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <mutex>

AUD_NAMESPACE_BEGIN

enum RenderFlags
{
	RENDER_DISTANCE = 0x01,
	RENDER_DOPPLER = 0x02,
	RENDER_CONE = 0x04,
	RENDER_VOLUME = 0x08
};

#define PITCH_MAX 10

/******************************************************************************/
/********************** SoftwareHandle Handle Code ************************/
/******************************************************************************/

bool SoftwareDevice::SoftwareHandle::pause(bool keep)
{
	if(m_status)
	{
		std::lock_guard<ILockable> lock(*m_device);

		if(m_status == STATUS_PLAYING)
		{
			for(auto it = m_device->m_playingSounds.begin(); it != m_device->m_playingSounds.end(); it++)
			{
				if(it->get() == this)
				{
					std::shared_ptr<SoftwareHandle> This = *it;

					m_device->m_playingSounds.erase(it);
					m_device->m_pausedSounds.push_back(This);

					if(m_device->m_playingSounds.empty())
						m_device->playing(m_device->m_playback = false);

					m_status = keep ? STATUS_STOPPED : STATUS_PAUSED;

					return true;
				}
			}
		}
	}

	return false;
}

SoftwareDevice::SoftwareHandle::SoftwareHandle(SoftwareDevice* device, std::shared_ptr<IReader> reader, std::shared_ptr<PitchReader> pitch, std::shared_ptr<ResampleReader> resampler, std::shared_ptr<ChannelMapperReader> mapper, bool keep) :
	m_reader(reader), m_pitch(pitch), m_resampler(resampler), m_mapper(mapper), m_keep(keep), m_user_pitch(1.0f), m_user_volume(1.0f), m_user_pan(0.0f), m_volume(0.0f), m_old_volume(0.0f), m_loopcount(0),
	m_relative(true), m_volume_max(1.0f), m_volume_min(0), m_distance_max(std::numeric_limits<float>::max()),
	m_distance_reference(1.0f), m_attenuation(1.0f), m_cone_angle_outer(M_PI), m_cone_angle_inner(M_PI), m_cone_volume_outer(0),
	m_flags(RENDER_CONE), m_stop(nullptr), m_stop_data(nullptr), m_status(STATUS_PLAYING), m_device(device)
{
}

void SoftwareDevice::SoftwareHandle::update()
{
	int flags = 0;

	m_old_volume = m_volume;

	Vector3 SL;
	if(m_relative)
		SL = -m_location;
	else
		SL = m_device->m_location - m_location;
	float distance = SL * SL;

	if(distance > 0)
		distance = sqrt(distance);
	else
		flags |= RENDER_DOPPLER | RENDER_DISTANCE;

	if(m_pitch->getSpecs().channels != CHANNELS_MONO)
	{
		m_volume = m_user_volume;
		m_pitch->setPitch(m_user_pitch);
		return;
	}

	flags = ~(flags | m_flags | m_device->m_flags);

	// Doppler and Pitch

	if(flags & RENDER_DOPPLER)
	{
		float vls;
		if(m_relative)
			vls = 0;
		else
			vls = SL * m_device->m_velocity / distance;
		float vss = SL * m_velocity / distance;
		float max = m_device->m_speed_of_sound / m_device->m_doppler_factor;
		if(vss >= max)
		{
			m_pitch->setPitch(PITCH_MAX);
		}
		else
		{
			if(vls > max)
				vls = max;

			m_pitch->setPitch((m_device->m_speed_of_sound - m_device->m_doppler_factor * vls) / (m_device->m_speed_of_sound - m_device->m_doppler_factor * vss) * m_user_pitch);
		}
	}
	else
		m_pitch->setPitch(m_user_pitch);

	if(flags & RENDER_VOLUME)
	{
		// Distance

		if(flags & RENDER_DISTANCE)
		{
			if(m_device->m_distance_model == DISTANCE_MODEL_INVERSE_CLAMPED ||
			   m_device->m_distance_model == DISTANCE_MODEL_LINEAR_CLAMPED ||
			   m_device->m_distance_model == DISTANCE_MODEL_EXPONENT_CLAMPED)
			{
				distance = std::max(std::min(m_distance_max, distance), m_distance_reference);
			}

			switch(m_device->m_distance_model)
			{
			case DISTANCE_MODEL_INVERSE:
			case DISTANCE_MODEL_INVERSE_CLAMPED:
				m_volume = m_distance_reference / (m_distance_reference + m_attenuation * (distance - m_distance_reference));
				break;
			case DISTANCE_MODEL_LINEAR:
			case DISTANCE_MODEL_LINEAR_CLAMPED:
			{
				float temp = m_distance_max - m_distance_reference;
				if(temp == 0)
				{
					if(distance > m_distance_reference)
						m_volume = 0.0f;
					else
						m_volume = 1.0f;
				}
				else
					m_volume = 1.0f - m_attenuation * (distance - m_distance_reference) / (m_distance_max - m_distance_reference);
				break;
			}
			case DISTANCE_MODEL_EXPONENT:
			case DISTANCE_MODEL_EXPONENT_CLAMPED:
				if(m_distance_reference == 0)
					m_volume = 0;
				else
					m_volume = std::pow(distance / m_distance_reference, -m_attenuation);
				break;
			default:
				m_volume = 1.0f;
			}
		}
		else
			m_volume = 1.0f;

		// Cone

		if(flags & RENDER_CONE)
		{
			Vector3 SZ = m_orientation.getLookAt();

			float phi = std::acos(float(SZ * SL / (SZ.length() * SL.length())));
			float t = (phi - m_cone_angle_inner)/(m_cone_angle_outer - m_cone_angle_inner);

			if(t > 0)
			{
				if(t > 1)
					m_volume *= m_cone_volume_outer;
				else
					m_volume *= 1 + t * (m_cone_volume_outer - 1);
			}
		}

		if(m_volume > m_volume_max)
			m_volume = m_volume_max;
		else if(m_volume < m_volume_min)
			m_volume = m_volume_min;

		// Volume

		m_volume *= m_user_volume;
	}

	// 3D Cue

	Quaternion orientation;

	if(!m_relative)
		orientation = m_device->m_orientation;

	Vector3 Z = orientation.getLookAt();
	Vector3 N = orientation.getUp();
	Vector3 A = N * ((SL * N) / (N * N)) - SL;

	float Asquare = A * A;

	if(Asquare > 0)
	{
		float phi = std::acos(float(Z * A / (Z.length() * std::sqrt(Asquare))));
		if(N.cross(Z) * A > 0)
			phi = -phi;

		m_mapper->setMonoAngle(phi);
	}
	else
		m_mapper->setMonoAngle(m_relative ? m_user_pan * M_PI / 2.0 : 0);
}

void SoftwareDevice::SoftwareHandle::setSpecs(Specs specs)
{
	m_mapper->setChannels(specs.channels);
	m_resampler->setRate(specs.rate);
}

bool SoftwareDevice::SoftwareHandle::pause()
{
	return pause(false);
}

bool SoftwareDevice::SoftwareHandle::resume()
{
	if(m_status)
	{
		std::lock_guard<ILockable> lock(*m_device);

		if(m_status == STATUS_PAUSED)
		{
			for(auto it = m_device->m_pausedSounds.begin(); it != m_device->m_pausedSounds.end(); it++)
			{
				if(it->get() == this)
				{
					std::shared_ptr<SoftwareHandle> This = *it;

					m_device->m_pausedSounds.erase(it);

					m_device->m_playingSounds.push_back(This);

					if(!m_device->m_playback)
						m_device->playing(m_device->m_playback = true);
					m_status = STATUS_PLAYING;

					return true;
				}
			}
		}

	}

	return false;
}

bool SoftwareDevice::SoftwareHandle::stop()
{
	if(!m_status)
		return false;

	std::lock_guard<ILockable> lock(*m_device);

	if(!m_status)
		return false;

	m_status = STATUS_INVALID;

	for(auto it = m_device->m_playingSounds.begin(); it != m_device->m_playingSounds.end(); it++)
	{
		if(it->get() == this)
		{
			std::shared_ptr<SoftwareHandle> This = *it;

			m_device->m_playingSounds.erase(it);

			if(m_device->m_playingSounds.empty())
				m_device->playing(m_device->m_playback = false);

			return true;
		}
	}

	for(auto it = m_device->m_pausedSounds.begin(); it != m_device->m_pausedSounds.end(); it++)
	{
		if(it->get() == this)
		{
			std::shared_ptr<SoftwareHandle> This = *it;

			m_device->m_pausedSounds.erase(it);

			return true;
		}
	}

	return false;
}

bool SoftwareDevice::SoftwareHandle::getKeep()
{
	if(m_status)
		return m_keep;

	return false;
}

bool SoftwareDevice::SoftwareHandle::setKeep(bool keep)
{
	if(!m_status)
		return false;

	std::lock_guard<ILockable> lock(*m_device);

	if(!m_status)
		return false;

	m_keep = keep;

	return true;
}

bool SoftwareDevice::SoftwareHandle::seek(float position)
{
	if(!m_status)
		return false;

	std::lock_guard<ILockable> lock(*m_device);

	if(!m_status)
		return false;

	m_pitch->setPitch(m_user_pitch);
	m_reader->seek((int)(position * m_reader->getSpecs().rate));

	if(m_status == STATUS_STOPPED)
		m_status = STATUS_PAUSED;

	return true;
}

float SoftwareDevice::SoftwareHandle::getPosition()
{
	if(!m_status)
		return false;

	std::lock_guard<ILockable> lock(*m_device);

	if(!m_status)
		return 0.0f;

	float position = m_reader->getPosition() / (float)m_device->m_specs.rate;

	return position;
}

Status SoftwareDevice::SoftwareHandle::getStatus()
{
	return m_status;
}

float SoftwareDevice::SoftwareHandle::getVolume()
{
	return m_user_volume;
}

bool SoftwareDevice::SoftwareHandle::setVolume(float volume)
{
	if(!m_status)
		return false;
	m_user_volume = volume;

	if(volume == 0)
	{
		m_old_volume = m_volume = volume;
		m_flags |= RENDER_VOLUME;
	}
	else
		m_flags &= ~RENDER_VOLUME;

	return true;
}

float SoftwareDevice::SoftwareHandle::getPitch()
{
	return m_user_pitch;
}

bool SoftwareDevice::SoftwareHandle::setPitch(float pitch)
{
	if(!m_status)
		return false;
	if(pitch > 0.0f)
		m_user_pitch = pitch;
	return true;
}

int SoftwareDevice::SoftwareHandle::getLoopCount()
{
	if(!m_status)
		return 0;
	return m_loopcount;
}

bool SoftwareDevice::SoftwareHandle::setLoopCount(int count)
{
	if(!m_status)
		return false;

	if(m_status == STATUS_STOPPED && (count > m_loopcount || count < 0))
		m_status = STATUS_PAUSED;

	m_loopcount = count;

	return true;
}

bool SoftwareDevice::SoftwareHandle::setStopCallback(stopCallback callback, void* data)
{
	if(!m_status)
		return false;

	std::lock_guard<ILockable> lock(*m_device);

	if(!m_status)
		return false;

	m_stop = callback;
	m_stop_data = data;

	return true;
}



/******************************************************************************/
/******************** SoftwareHandle 3DHandle Code ************************/
/******************************************************************************/

Vector3 SoftwareDevice::SoftwareHandle::getLocation()
{
	if(!m_status)
		return Vector3();

	return m_location;
}

bool SoftwareDevice::SoftwareHandle::setLocation(const Vector3& location)
{
	if(!m_status)
		return false;

	m_location = location;

	return true;
}

Vector3 SoftwareDevice::SoftwareHandle::getVelocity()
{
	if(!m_status)
		return Vector3();

	return m_velocity;
}

bool SoftwareDevice::SoftwareHandle::setVelocity(const Vector3& velocity)
{
	if(!m_status)
		return false;

	m_velocity = velocity;

	return true;
}

Quaternion SoftwareDevice::SoftwareHandle::getOrientation()
{
	if(!m_status)
		return Quaternion();

	return m_orientation;
}

bool SoftwareDevice::SoftwareHandle::setOrientation(const Quaternion& orientation)
{
	if(!m_status)
		return false;

	m_orientation = orientation;

	return true;
}

bool SoftwareDevice::SoftwareHandle::isRelative()
{
	if(!m_status)
		return false;

	return m_relative;
}

bool SoftwareDevice::SoftwareHandle::setRelative(bool relative)
{
	if(!m_status)
		return false;

	m_relative = relative;

	return true;
}

float SoftwareDevice::SoftwareHandle::getVolumeMaximum()
{
	if(!m_status)
		return std::numeric_limits<float>::quiet_NaN();

	return m_volume_max;
}

bool SoftwareDevice::SoftwareHandle::setVolumeMaximum(float volume)
{
	if(!m_status)
		return false;

	m_volume_max = volume;

	return true;
}

float SoftwareDevice::SoftwareHandle::getVolumeMinimum()
{
	if(!m_status)
		return std::numeric_limits<float>::quiet_NaN();

	return m_volume_min;
}

bool SoftwareDevice::SoftwareHandle::setVolumeMinimum(float volume)
{
	if(!m_status)
		return false;

	m_volume_min = volume;

	return true;
}

float SoftwareDevice::SoftwareHandle::getDistanceMaximum()
{
	if(!m_status)
		return std::numeric_limits<float>::quiet_NaN();

	return m_distance_max;
}

bool SoftwareDevice::SoftwareHandle::setDistanceMaximum(float distance)
{
	if(!m_status)
		return false;

	m_distance_max = distance;

	return true;
}

float SoftwareDevice::SoftwareHandle::getDistanceReference()
{
	if(!m_status)
		return std::numeric_limits<float>::quiet_NaN();

	return m_distance_reference;
}

bool SoftwareDevice::SoftwareHandle::setDistanceReference(float distance)
{
	if(!m_status)
		return false;

	m_distance_reference = distance;

	return true;
}

float SoftwareDevice::SoftwareHandle::getAttenuation()
{
	if(!m_status)
		return std::numeric_limits<float>::quiet_NaN();

	return m_attenuation;
}

bool SoftwareDevice::SoftwareHandle::setAttenuation(float factor)
{
	if(!m_status)
		return false;

	m_attenuation = factor;

	if(factor == 0)
		m_flags |= RENDER_DISTANCE;
	else
		m_flags &= ~RENDER_DISTANCE;

	return true;
}

float SoftwareDevice::SoftwareHandle::getConeAngleOuter()
{
	if(!m_status)
		return std::numeric_limits<float>::quiet_NaN();

	return m_cone_angle_outer * 360.0f / M_PI;
}

bool SoftwareDevice::SoftwareHandle::setConeAngleOuter(float angle)
{
	if(!m_status)
		return false;

	m_cone_angle_outer = angle * M_PI / 360.0f;

	return true;
}

float SoftwareDevice::SoftwareHandle::getConeAngleInner()
{
	if(!m_status)
		return std::numeric_limits<float>::quiet_NaN();

	return m_cone_angle_inner * 360.0f / M_PI;
}

bool SoftwareDevice::SoftwareHandle::setConeAngleInner(float angle)
{
	if(!m_status)
		return false;

	if(angle >= 360)
		m_flags |= RENDER_CONE;
	else
		m_flags &= ~RENDER_CONE;

	m_cone_angle_inner = angle * M_PI / 360.0f;

	return true;
}

float SoftwareDevice::SoftwareHandle::getConeVolumeOuter()
{
	if(!m_status)
		return std::numeric_limits<float>::quiet_NaN();

	return m_cone_volume_outer;
}

bool SoftwareDevice::SoftwareHandle::setConeVolumeOuter(float volume)
{
	if(!m_status)
		return false;

	m_cone_volume_outer = volume;

	return true;
}

/******************************************************************************/
/**************************** IDevice Code ************************************/
/******************************************************************************/

void SoftwareDevice::create()
{
	m_playback = false;
	m_volume = 1.0f;
	m_mixer = std::shared_ptr<Mixer>(new Mixer(m_specs));
	m_speed_of_sound = 343.3f;
	m_doppler_factor = 1.0f;
	m_distance_model = DISTANCE_MODEL_INVERSE_CLAMPED;
	m_flags = 0;
	m_quality = false;
}

void SoftwareDevice::destroy()
{
	if(m_playback)
		playing(m_playback = false);

	while(!m_playingSounds.empty())
		m_playingSounds.front()->stop();

	while(!m_pausedSounds.empty())
		m_pausedSounds.front()->stop();
}

void SoftwareDevice::mix(data_t* buffer, int length)
{
	m_buffer.assureSize(length * AUD_SAMPLE_SIZE(m_specs));

	std::lock_guard<std::recursive_mutex> lock(m_mutex);

	{
		std::shared_ptr<SoftwareDevice::SoftwareHandle> sound;
		int len;
		int pos;
		bool eos;
		std::list<std::shared_ptr<SoftwareDevice::SoftwareHandle> > stopSounds;
		std::list<std::shared_ptr<SoftwareDevice::SoftwareHandle> > pauseSounds;
		sample_t* buf = m_buffer.getBuffer();

		m_mixer->clear(length);

		// for all sounds
		for(auto& sound : m_playingSounds)
		{
			// get the buffer from the source
			pos = 0;
			len = length;

			// update 3D Info
			sound->update();

			try
			{
				sound->m_reader->read(len, eos, buf);

				// in case of looping
				while(pos + len < length && sound->m_loopcount && eos)
				{
					m_mixer->mix(buf, pos, len, sound->m_volume, sound->m_old_volume);

					pos += len;

					if(sound->m_loopcount > 0)
						sound->m_loopcount--;

					sound->m_reader->seek(0);

					len = length - pos;
					sound->m_reader->read(len, eos, buf);

					// prevent endless loop
					if(!len)
						break;
				}
			}
			catch(Exception& e)
			{
				len = 0;
				std::cerr << "Caught exception while reading sound data during playback with software mixing: " << e.getMessage() << std::endl;
			}

			m_mixer->mix(buf, pos, len, sound->m_volume, sound->m_old_volume);

			// in case the end of the sound is reached
			if(eos && !sound->m_loopcount)
			{
				if(sound->m_stop)
					sound->m_stop(sound->m_stop_data);

				if(sound->m_keep)
					pauseSounds.push_back(sound);
				else
					stopSounds.push_back(sound);
			}
		}

		// superpose
		m_mixer->read(buffer, m_volume);

		// cleanup
		for(auto& sound : pauseSounds)
			sound->pause(true);

		for(auto& sound :  stopSounds)
			sound->stop();

		pauseSounds.clear();
		stopSounds.clear();
	}
}

void SoftwareDevice::setPanning(IHandle* handle, float pan)
{
	SoftwareDevice::SoftwareHandle* h = dynamic_cast<SoftwareDevice::SoftwareHandle*>(handle);
	h->m_user_pan = pan;
}

void SoftwareDevice::setQuality(bool quality)
{
	m_quality = quality;
}

void SoftwareDevice::setSpecs(Specs specs)
{
	m_specs.specs = specs;
	m_mixer->setSpecs(specs);

	for(auto& sound : m_playingSounds)
	{
		sound->setSpecs(specs);
	}
}

SoftwareDevice::SoftwareDevice()
{
}

DeviceSpecs SoftwareDevice::getSpecs() const
{
	return m_specs;
}

std::shared_ptr<IHandle> SoftwareDevice::play(std::shared_ptr<IReader> reader, bool keep)
{
	// prepare the reader
	// pitch

	std::shared_ptr<PitchReader> pitch = std::shared_ptr<PitchReader>(new PitchReader(reader, 1));
	reader = std::shared_ptr<IReader>(pitch);

	std::shared_ptr<ResampleReader> resampler;

	// resample
	if(m_quality)
		resampler = std::shared_ptr<ResampleReader>(new JOSResampleReader(reader, m_specs.rate));
	else
		resampler = std::shared_ptr<ResampleReader>(new LinearResampleReader(reader, m_specs.rate));
	reader = std::shared_ptr<IReader>(resampler);

	// rechannel
	std::shared_ptr<ChannelMapperReader> mapper = std::shared_ptr<ChannelMapperReader>(new ChannelMapperReader(reader, m_specs.channels));
	reader = std::shared_ptr<IReader>(mapper);

	if(!reader.get())
		return std::shared_ptr<IHandle>();

	// play sound
	std::shared_ptr<SoftwareDevice::SoftwareHandle> sound = std::shared_ptr<SoftwareDevice::SoftwareHandle>(new SoftwareDevice::SoftwareHandle(this, reader, pitch, resampler, mapper, keep));

	std::lock_guard<std::recursive_mutex> lock(m_mutex);

	m_playingSounds.push_back(sound);

	if(!m_playback)
		playing(m_playback = true);

	return std::shared_ptr<IHandle>(sound);
}

std::shared_ptr<IHandle> SoftwareDevice::play(std::shared_ptr<ISound> sound, bool keep)
{
	return play(sound->createReader(), keep);
}

void SoftwareDevice::stopAll()
{
	std::lock_guard<std::recursive_mutex> lock(m_mutex);

	while(!m_playingSounds.empty())
		m_playingSounds.front()->stop();

	while(!m_pausedSounds.empty())
		m_pausedSounds.front()->stop();
}

void SoftwareDevice::lock()
{
	m_mutex.lock();
}

void SoftwareDevice::unlock()
{
	m_mutex.unlock();
}

float SoftwareDevice::getVolume() const
{
	return m_volume;
}

void SoftwareDevice::setVolume(float volume)
{
	m_volume = volume;
}

ISynchronizer* SoftwareDevice::getSynchronizer()
{
	return &m_synchronizer;
}

/******************************************************************************/
/**************************** 3D Device Code **********************************/
/******************************************************************************/

Vector3 SoftwareDevice::getListenerLocation() const
{
	return m_location;
}

void SoftwareDevice::setListenerLocation(const Vector3& location)
{
	m_location = location;
}

Vector3 SoftwareDevice::getListenerVelocity() const
{
	return m_velocity;
}

void SoftwareDevice::setListenerVelocity(const Vector3& velocity)
{
	m_velocity = velocity;
}

Quaternion SoftwareDevice::getListenerOrientation() const
{
	return m_orientation;
}

void SoftwareDevice::setListenerOrientation(const Quaternion& orientation)
{
	m_orientation = orientation;
}

float SoftwareDevice::getSpeedOfSound() const
{
	return m_speed_of_sound;
}

void SoftwareDevice::setSpeedOfSound(float speed)
{
	m_speed_of_sound = speed;
}

float SoftwareDevice::getDopplerFactor() const
{
	return m_doppler_factor;
}

void SoftwareDevice::setDopplerFactor(float factor)
{
	m_doppler_factor = factor;
	if(factor == 0)
		m_flags |= RENDER_DOPPLER;
	else
		m_flags &= ~RENDER_DOPPLER;
}

DistanceModel SoftwareDevice::getDistanceModel() const
{
	return m_distance_model;
}

void SoftwareDevice::setDistanceModel(DistanceModel model)
{
	m_distance_model = model;
	if(model == DISTANCE_MODEL_INVALID)
		m_flags |= RENDER_DISTANCE;
	else
		m_flags &= ~RENDER_DISTANCE;
}

AUD_NAMESPACE_END