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

AUD_Sound.cpp « C « bindings « audaspace « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 30860acde624804cca288099feeacb40682c6511 (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
/*******************************************************************************
 * 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 "generator/Sawtooth.h"
#include "generator/Sine.h"
#include "generator/Silence.h"
#include "generator/Square.h"
#include "generator/Triangle.h"
#include "file/File.h"
#include "file/FileWriter.h"
#include "util/StreamBuffer.h"
#include "fx/Accumulator.h"
#include "fx/ADSR.h"
#include "fx/Delay.h"
#include "fx/Envelope.h"
#include "fx/Fader.h"
#include "fx/Highpass.h"
#include "fx/IIRFilter.h"
#include "fx/Limiter.h"
#include "fx/Loop.h"
#include "fx/Lowpass.h"
#include "fx/Pitch.h"
#include "fx/Reverse.h"
#include "fx/Sum.h"
#include "fx/Threshold.h"
#include "fx/Volume.h"
#include "fx/SoundList.h"
#include "fx/MutableSound.h"
#include "sequence/Double.h"
#include "sequence/Superpose.h"
#include "sequence/PingPong.h"
#include "respec/LinearResample.h"
#include "respec/JOSResample.h"
#include "respec/JOSResampleReader.h"
#include "respec/ChannelMapper.h"
#include "respec/ChannelMapperReader.h"
#include "util/Buffer.h"
#include "Exception.h"

#ifdef WITH_CONVOLUTION
#include "fx/BinauralSound.h"
#include "fx/ConvolverSound.h"
#endif

#include <cassert>
#include <cstring>

using namespace aud;

#define AUD_CAPI_IMPLEMENTATION
#include "AUD_Sound.h"

static inline AUD_Specs convSpecToC(aud::Specs specs)
{
	AUD_Specs s;
	s.channels = static_cast<AUD_Channels>(specs.channels);
	s.rate = static_cast<AUD_SampleRate>(specs.rate);
	return s;
}

static inline aud::Specs convCToSpec(AUD_Specs specs)
{
	aud::Specs s;
	s.channels = static_cast<Channels>(specs.channels);
	s.rate = static_cast<SampleRate>(specs.rate);
	return s;
}

AUD_API AUD_Specs AUD_Sound_getSpecs(AUD_Sound* sound)
{
	assert(sound);

	return convSpecToC((*sound)->createReader()->getSpecs());
}

AUD_API int AUD_Sound_getLength(AUD_Sound* sound)
{
	assert(sound);

	return (*sound)->createReader()->getLength();
}

AUD_API sample_t* AUD_Sound_data(AUD_Sound* sound, int* length, AUD_Specs* specs)
{
	assert(sound);
	assert(length);
	assert(specs);

	auto stream_buffer = std::dynamic_pointer_cast<StreamBuffer>(*sound);
	if(!stream_buffer)
		stream_buffer = std::make_shared<StreamBuffer>(*sound);
	*specs = convSpecToC(stream_buffer->getSpecs());
	auto buffer = stream_buffer->getBuffer();

	*length = buffer->getSize() / AUD_SAMPLE_SIZE((*specs));

	sample_t* data = new sample_t[buffer->getSize()];

	std::memcpy(data, buffer->getBuffer(), buffer->getSize());

	return data;
}

AUD_API void AUD_Sound_freeData(sample_t* data)
{
	delete[] data;
}

AUD_API const char* AUD_Sound_write(AUD_Sound* sound, const char* filename, AUD_SampleRate rate, AUD_Channels channels, AUD_SampleFormat format, AUD_Container container, AUD_Codec codec, int bitrate, int buffersize)
{
	assert(sound);
	assert(filename);

	try
	{
		std::shared_ptr<IReader> reader = (*sound)->createReader();

		DeviceSpecs specs;
		specs.specs = reader->getSpecs();

		if((rate != RATE_INVALID) && (specs.rate != rate))
		{
			specs.rate = rate;
			reader = std::make_shared<JOSResampleReader>(reader, rate);
		}

		if((channels != AUD_CHANNELS_INVALID) && (specs.channels != static_cast<Channels>(channels)))
		{
			specs.channels = static_cast<Channels>(channels);
			reader = std::make_shared<ChannelMapperReader>(reader, specs.channels);
		}

		if(format == AUD_FORMAT_INVALID)
			format = AUD_FORMAT_S16;
		specs.format = static_cast<SampleFormat>(format);

		const char* invalid_container_error = "Container could not be determined from filename.";

		if(container == AUD_CONTAINER_INVALID)
		{
			std::string path = filename;

			if(path.length() < 4)
				return invalid_container_error;

			std::string extension = path.substr(path.length() - 4);

			if(extension == ".ac3")
				container = AUD_CONTAINER_AC3;
			else if(extension == "flac")
				container = AUD_CONTAINER_FLAC;
			else if(extension == ".mkv")
				container = AUD_CONTAINER_MATROSKA;
			else if(extension == ".mp2")
				container = AUD_CONTAINER_MP2;
			else if(extension == ".mp3")
				container = AUD_CONTAINER_MP3;
			else if(extension == ".ogg")
				container = AUD_CONTAINER_OGG;
			else if(extension == ".wav")
				container = AUD_CONTAINER_WAV;
			else
				return invalid_container_error;
		}

		if(codec == AUD_CODEC_INVALID)
		{
			switch(container)
			{
			case AUD_CONTAINER_AC3:
				codec = AUD_CODEC_AC3;
				break;
			case AUD_CONTAINER_FLAC:
				codec = AUD_CODEC_FLAC;
				break;
			case AUD_CONTAINER_MATROSKA:
				codec = AUD_CODEC_OPUS;
				break;
			case AUD_CONTAINER_MP2:
				codec = AUD_CODEC_MP2;
				break;
			case AUD_CONTAINER_MP3:
				codec = AUD_CODEC_MP3;
				break;
			case AUD_CONTAINER_OGG:
				codec = AUD_CODEC_VORBIS;
				break;
			case AUD_CONTAINER_WAV:
				codec = AUD_CODEC_PCM;
				break;
			default:
				return "Unknown container, cannot select default codec.";
			}
		}

		if(buffersize <= 0)
			buffersize = AUD_DEFAULT_BUFFER_SIZE;

		std::shared_ptr<IWriter> writer = FileWriter::createWriter(filename, specs, static_cast<Container>(container), static_cast<Codec>(codec), bitrate);
		FileWriter::writeReader(reader, writer, 0, buffersize);
	}
	catch(Exception& e)
	{
		return "An exception occured while writing.";
	}

	return nullptr;
}

AUD_API AUD_Sound* AUD_Sound_buffer(sample_t* data, int length, AUD_Specs specs)
{
	assert(data);

	if(length <= 0 || specs.rate <= 0 || specs.channels <= 0)
	{
		return nullptr;
	}

	int size = length * AUD_SAMPLE_SIZE(specs);

	std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>(size);

	std::memcpy(buffer->getBuffer(), data, size);

	try
	{
		return new AUD_Sound(new StreamBuffer(buffer, convCToSpec(specs)));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_bufferFile(unsigned char* buffer, int size)
{
	assert(buffer);
	return new AUD_Sound(new File(buffer, size));
}

AUD_API AUD_Sound* AUD_Sound_cache(AUD_Sound* sound)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new StreamBuffer(*sound));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_file(const char* filename)
{
	assert(filename);
	return new AUD_Sound(new File(filename));
}

AUD_API AUD_Sound* AUD_Sound_sawtooth(float frequency, AUD_SampleRate rate)
{
	return new AUD_Sound(new Sawtooth(frequency, rate));
}

AUD_API AUD_Sound*AUD_Sound_silence()
{
	return new AUD_Sound(new Silence());
}

AUD_API AUD_Sound* AUD_Sound_sine(float frequency, AUD_SampleRate rate)
{
	return new AUD_Sound(new Sine(frequency, rate));
}

AUD_API AUD_Sound* AUD_Sound_square(float frequency, AUD_SampleRate rate)
{
	return new AUD_Sound(new Square(frequency, rate));
}

AUD_API AUD_Sound* AUD_Sound_triangle(float frequency, AUD_SampleRate rate)
{
	return new AUD_Sound(new Triangle(frequency, rate));
}

AUD_API AUD_Sound* AUD_Sound_accumulate(AUD_Sound* sound, int additive)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Accumulator(*sound, additive));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_ADSR(AUD_Sound* sound, float attack, float decay, float sustain, float release)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new ADSR(*sound, attack, decay, sustain, release));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_delay(AUD_Sound* sound, float delay)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Delay(*sound, delay));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_envelope(AUD_Sound* sound, float attack, float release, float threshold, float arthreshold)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Envelope(*sound, attack, release, threshold, arthreshold));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_fadein(AUD_Sound* sound, float start, float length)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Fader(*sound, FADE_IN, start, length));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_fadeout(AUD_Sound* sound, float start, float length)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Fader(*sound, FADE_OUT, start, length));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_filter(AUD_Sound* sound, float* b, int b_length, float* a, int a_length)
{
	assert(sound);

	try
	{
		std::vector<float> a_coeff, b_coeff;

		if(b)
			for(int i = 0; i < b_length; i++)
				b_coeff.push_back(b[i]);

		if(a)
		{
			for(int i = 0; i < a_length; i++)
				a_coeff.push_back(a[i]);

			if(*a == 0.0f)
				a_coeff[0] = 1.0f;
		}

		return new AUD_Sound(new IIRFilter(*sound, b_coeff, a_coeff));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_highpass(AUD_Sound* sound, float frequency, float Q)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Highpass(*sound, frequency, Q));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_limit(AUD_Sound* sound, float start, float end)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Limiter(*sound, start, end));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_loop(AUD_Sound* sound, int count)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Loop(*sound, count));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_lowpass(AUD_Sound* sound, float frequency, float Q)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Lowpass(*sound, frequency, Q));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_pitch(AUD_Sound* sound, float factor)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Pitch(*sound, factor));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_rechannel(AUD_Sound* sound, AUD_Channels channels)
{
	assert(sound);

	try
	{
		DeviceSpecs specs;
		specs.channels = static_cast<Channels>(channels);
		specs.rate = RATE_INVALID;
		specs.format = FORMAT_INVALID;
		return new AUD_Sound(new ChannelMapper(*sound, specs));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_resample(AUD_Sound* sound, AUD_SampleRate rate, bool high_quality)
{
	assert(sound);

	try
	{
		DeviceSpecs specs;
		specs.channels = CHANNELS_INVALID;
		specs.rate = rate;
		specs.format = FORMAT_INVALID;
		if(high_quality)
			return new AUD_Sound(new JOSResample(*sound, specs));
		else
			return new AUD_Sound(new LinearResample(*sound, specs));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_reverse(AUD_Sound* sound)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Reverse(*sound));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_sum(AUD_Sound* sound)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Sum(*sound));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_threshold(AUD_Sound* sound, float threshold)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Threshold(*sound, threshold));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_volume(AUD_Sound* sound, float volume)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new Volume(*sound, volume));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_join(AUD_Sound* first, AUD_Sound* second)
{
	assert(first);
	assert(second);

	try
	{
		return new AUD_Sound(new Double(*first, *second));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_mix(AUD_Sound* first, AUD_Sound* second)
{
	assert(first);
	assert(second);

	try
	{
		return new AUD_Sound(new Superpose(*first, *second));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_pingpong(AUD_Sound* sound)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new PingPong(*sound));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API void AUD_Sound_free(AUD_Sound* sound)
{
	assert(sound);
	delete sound;
}

AUD_API AUD_Sound* AUD_Sound_copy(AUD_Sound* sound)
{
	return new std::shared_ptr<ISound>(*sound);
}

AUD_API AUD_Sound* AUD_Sound_list(int random)
{
	try
	{
		return new AUD_Sound(new SoundList(random));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API int AUD_SoundList_addSound(AUD_Sound* list, AUD_Sound* sound)
{
	assert(sound);
	assert(list);

	std::shared_ptr<SoundList> s = std::dynamic_pointer_cast<SoundList>(*list);
	if(s.get())
	{
		s->addSound(*sound);
		return 1;
	}
	else
		return 0;

}

AUD_API AUD_Sound* AUD_Sound_mutable(AUD_Sound* sound)
{
	assert(sound);

	try
	{
		return new AUD_Sound(new MutableSound(*sound));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

#ifdef WITH_CONVOLUTION

AUD_API AUD_Sound* AUD_Sound_Convolver(AUD_Sound* sound, AUD_ImpulseResponse* filter, AUD_ThreadPool* threadPool)
{
	assert(sound);
	assert(filter);
	assert(threadPool);

	try
	{
		return new AUD_Sound(new ConvolverSound(*sound, *filter, *threadPool));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

AUD_API AUD_Sound* AUD_Sound_Binaural(AUD_Sound* sound, AUD_HRTF* hrtfs, AUD_Source* source, AUD_ThreadPool* threadPool)
{
	assert(sound);
	assert(hrtfs);
	assert(source);
	assert(threadPool);

	try
	{
		return new AUD_Sound(new BinauralSound(*sound, *hrtfs, *source, *threadPool));
	}
	catch(Exception&)
	{
		return nullptr;
	}
}

#endif