1 /* GSequencer - Advanced GTK Sequencer
2 * Copyright (C) 2005-2021 Joël Krähemann
3 *
4 * This file is part of GSequencer.
5 *
6 * GSequencer is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GSequencer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GSequencer. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <ags/audio/ags_synth_util.h>
21
22 #include <ags/audio/ags_fourier_transform_util.h>
23
24 #include <math.h>
25 #include <complex.h>
26
27 /**
28 * SECTION:ags_synth_util
29 * @short_description: synth util
30 * @title: AgsSynthUtil
31 * @section_id:
32 * @include: ags/audio/ags_synth_util.h
33 *
34 * Utility functions to compute synths.
35 */
36
37 GType
ags_synth_util_get_type(void)38 ags_synth_util_get_type(void)
39 {
40 static volatile gsize g_define_type_id__volatile = 0;
41
42 if(g_once_init_enter (&g_define_type_id__volatile)){
43 GType ags_type_synth_util = 0;
44
45 ags_type_synth_util =
46 g_boxed_type_register_static("AgsSynthUtil",
47 (GBoxedCopyFunc) ags_synth_util_copy,
48 (GBoxedFreeFunc) ags_synth_util_free);
49
50 g_once_init_leave(&g_define_type_id__volatile, ags_type_synth_util);
51 }
52
53 return g_define_type_id__volatile;
54 }
55
56 /**
57 * ags_synth_util_alloc:
58 *
59 * Allocate #AgsSynthUtil-struct
60 *
61 * Returns: a new #AgsSynthUtil-struct
62 *
63 * Since: 3.9.3
64 */
65 AgsSynthUtil*
ags_synth_util_alloc()66 ags_synth_util_alloc()
67 {
68 AgsSynthUtil *ptr;
69
70 ptr = (AgsSynthUtil *) g_new(AgsSynthUtil,
71 1);
72
73 ptr->source = NULL;
74 ptr->source_stride = 1;
75
76 ptr->buffer_length = 0;
77 ptr->audio_buffer_util_format = AGS_SYNTH_UTIL_DEFAULT_AUDIO_BUFFER_UTIL_FORMAT;
78 ptr->samplerate = 0;
79
80 ptr->synth_oscillator_mode = AGS_SYNTH_OSCILLATOR_SIN;
81
82 ptr->frequency = AGS_SYNTH_UTIL_DEFAULT_FREQUENCY;
83 ptr->phase = 0.0;
84 ptr->volume = 1.0;
85
86 ptr->frame_count = 0;
87 ptr->offset = 0;
88
89 return(ptr);
90 }
91
92 /**
93 * ags_synth_util_copy:
94 * @ptr: the original #AgsSynthUtil-struct
95 *
96 * Create a copy of @ptr.
97 *
98 * Returns: a pointer of the new #AgsSynthUtil-struct
99 *
100 * Since: 3.9.3
101 */
102 gpointer
ags_synth_util_copy(AgsSynthUtil * ptr)103 ags_synth_util_copy(AgsSynthUtil *ptr)
104 {
105 AgsSynthUtil *new_ptr;
106
107 new_ptr = (AgsSynthUtil *) g_new(AgsSynthUtil,
108 1);
109
110 new_ptr->source = ptr->source;
111 new_ptr->source_stride = ptr->source_stride;
112
113 new_ptr->buffer_length = ptr->buffer_length;
114 new_ptr->audio_buffer_util_format = ptr->audio_buffer_util_format;
115 new_ptr->samplerate = ptr->samplerate;
116
117 new_ptr->synth_oscillator_mode = ptr->synth_oscillator_mode;
118
119 new_ptr->frequency = ptr->frequency;
120 new_ptr->phase = ptr->phase;
121 new_ptr->volume = ptr->volume;
122
123 new_ptr->frame_count = ptr->frame_count;
124 new_ptr->offset = ptr->offset;
125
126 return(new_ptr);
127 }
128
129 /**
130 * ags_synth_util_free:
131 * @ptr: the #AgsSynthUtil-struct
132 *
133 * Free the memory of @ptr.
134 *
135 * Since: 3.9.3
136 */
137 void
ags_synth_util_free(AgsSynthUtil * ptr)138 ags_synth_util_free(AgsSynthUtil *ptr)
139 {
140 g_free(ptr->source);
141
142 g_free(ptr);
143 }
144
145 /**
146 * ags_synth_util_get_source:
147 * @synth_util: the #AgsSynthUtil-struct
148 *
149 * Get source buffer of @synth_util.
150 *
151 * Returns: the source buffer
152 *
153 * Since: 3.9.3
154 */
155 gpointer
ags_synth_util_get_source(AgsSynthUtil * synth_util)156 ags_synth_util_get_source(AgsSynthUtil *synth_util)
157 {
158 if(synth_util == NULL){
159 return(NULL);
160 }
161
162 return(synth_util->source);
163 }
164
165 /**
166 * ags_synth_util_set_source:
167 * @synth_util: the #AgsSynthUtil-struct
168 * @source: the source buffer
169 *
170 * Set @source buffer of @synth_util.
171 *
172 * Since: 3.9.3
173 */
174 void
ags_synth_util_set_source(AgsSynthUtil * synth_util,gpointer source)175 ags_synth_util_set_source(AgsSynthUtil *synth_util,
176 gpointer source)
177 {
178 if(synth_util == NULL){
179 return;
180 }
181
182 synth_util->source = source;
183 }
184
185 /**
186 * ags_synth_util_get_source_stride:
187 * @synth_util: the #AgsSynthUtil-struct
188 *
189 * Get source stride of @synth_util.
190 *
191 * Returns: the source buffer stride
192 *
193 * Since: 3.9.3
194 */
195 guint
ags_synth_util_get_source_stride(AgsSynthUtil * synth_util)196 ags_synth_util_get_source_stride(AgsSynthUtil *synth_util)
197 {
198 if(synth_util == NULL){
199 return(0);
200 }
201
202 return(synth_util->source_stride);
203 }
204
205 /**
206 * ags_synth_util_set_source_stride:
207 * @synth_util: the #AgsSynthUtil-struct
208 * @source_stride: the source buffer stride
209 *
210 * Set @source stride of @synth_util.
211 *
212 * Since: 3.9.3
213 */
214 void
ags_synth_util_set_source_stride(AgsSynthUtil * synth_util,guint source_stride)215 ags_synth_util_set_source_stride(AgsSynthUtil *synth_util,
216 guint source_stride)
217 {
218 if(synth_util == NULL){
219 return;
220 }
221
222 synth_util->source_stride = source_stride;
223 }
224
225 /**
226 * ags_synth_util_get_buffer_length:
227 * @synth_util: the #AgsSynthUtil-struct
228 *
229 * Get buffer length of @synth_util.
230 *
231 * Returns: the buffer length
232 *
233 * Since: 3.9.3
234 */
235 guint
ags_synth_util_get_buffer_length(AgsSynthUtil * synth_util)236 ags_synth_util_get_buffer_length(AgsSynthUtil *synth_util)
237 {
238 if(synth_util == NULL){
239 return(0);
240 }
241
242 return(synth_util->buffer_length);
243 }
244
245 /**
246 * ags_synth_util_set_buffer_length:
247 * @synth_util: the #AgsSynthUtil-struct
248 * @buffer_length: the buffer length
249 *
250 * Set @buffer_length of @synth_util.
251 *
252 * Since: 3.9.3
253 */
254 void
ags_synth_util_set_buffer_length(AgsSynthUtil * synth_util,guint buffer_length)255 ags_synth_util_set_buffer_length(AgsSynthUtil *synth_util,
256 guint buffer_length)
257 {
258 if(synth_util == NULL){
259 return;
260 }
261
262 synth_util->buffer_length = buffer_length;
263 }
264
265 /**
266 * ags_synth_util_get_audio_buffer_util_format:
267 * @synth_util: the #AgsSynthUtil-struct
268 *
269 * Get audio buffer util format of @synth_util.
270 *
271 * Returns: the audio buffer util format
272 *
273 * Since: 3.9.3
274 */
275 guint
ags_synth_util_get_audio_buffer_util_format(AgsSynthUtil * synth_util)276 ags_synth_util_get_audio_buffer_util_format(AgsSynthUtil *synth_util)
277 {
278 if(synth_util == NULL){
279 return(0);
280 }
281
282 return(synth_util->audio_buffer_util_format);
283 }
284
285 /**
286 * ags_synth_util_set_audio_buffer_util_format:
287 * @synth_util: the #AgsSynthUtil-struct
288 * @audio_buffer_util_format: the audio buffer util format
289 *
290 * Set @audio_buffer_util_format of @synth_util.
291 *
292 * Since: 3.9.3
293 */
294 void
ags_synth_util_set_audio_buffer_util_format(AgsSynthUtil * synth_util,guint audio_buffer_util_format)295 ags_synth_util_set_audio_buffer_util_format(AgsSynthUtil *synth_util,
296 guint audio_buffer_util_format)
297 {
298 if(synth_util == NULL){
299 return;
300 }
301
302 synth_util->audio_buffer_util_format = audio_buffer_util_format;
303 }
304
305 /**
306 * ags_synth_util_get_samplerate:
307 * @synth_util: the #AgsSynthUtil-struct
308 *
309 * Get samplerate of @synth_util.
310 *
311 * Returns: the samplerate
312 *
313 * Since: 3.9.3
314 */
315 guint
ags_synth_util_get_samplerate(AgsSynthUtil * synth_util)316 ags_synth_util_get_samplerate(AgsSynthUtil *synth_util)
317 {
318 if(synth_util == NULL){
319 return(0);
320 }
321
322 return(synth_util->samplerate);
323 }
324
325 /**
326 * ags_synth_util_set_samplerate:
327 * @synth_util: the #AgsSynthUtil-struct
328 * @samplerate: the samplerate
329 *
330 * Set @samplerate of @synth_util.
331 *
332 * Since: 3.9.3
333 */
334 void
ags_synth_util_set_samplerate(AgsSynthUtil * synth_util,guint samplerate)335 ags_synth_util_set_samplerate(AgsSynthUtil *synth_util,
336 guint samplerate)
337 {
338 if(synth_util == NULL){
339 return;
340 }
341
342 synth_util->samplerate = samplerate;
343 }
344
345 /**
346 * ags_synth_util_get_synth_oscillator_mode:
347 * @synth_util: the #AgsSynthUtil-struct
348 *
349 * Get synth oscillator mode of @synth_util.
350 *
351 * Returns: the synth oscillator mode
352 *
353 * Since: 3.9.3
354 */
355 guint
ags_synth_util_get_synth_oscillator_mode(AgsSynthUtil * synth_util)356 ags_synth_util_get_synth_oscillator_mode(AgsSynthUtil *synth_util)
357 {
358 if(synth_util == NULL){
359 return(0);
360 }
361
362 return(synth_util->synth_oscillator_mode);
363 }
364
365 /**
366 * ags_synth_util_set_synth_oscillator_mode:
367 * @synth_util: the #AgsSynthUtil-struct
368 * @synth_oscillator_mode: the synth oscillator mode
369 *
370 * Set @synth_oscillator_mode of @synth_util.
371 *
372 * Since: 3.9.3
373 */
374 void
ags_synth_util_set_synth_oscillator_mode(AgsSynthUtil * synth_util,guint synth_oscillator_mode)375 ags_synth_util_set_synth_oscillator_mode(AgsSynthUtil *synth_util,
376 guint synth_oscillator_mode)
377 {
378 if(synth_util == NULL){
379 return;
380 }
381
382 synth_util->synth_oscillator_mode = synth_oscillator_mode;
383 }
384
385 /**
386 * ags_synth_util_get_frequency:
387 * @synth_util: the #AgsSynthUtil-struct
388 *
389 * Get frequency of @synth_util.
390 *
391 * Returns: the frequency
392 *
393 * Since: 3.9.3
394 */
395 gdouble
ags_synth_util_get_frequency(AgsSynthUtil * synth_util)396 ags_synth_util_get_frequency(AgsSynthUtil *synth_util)
397 {
398 if(synth_util == NULL){
399 return(1.0);
400 }
401
402 return(synth_util->frequency);
403 }
404
405 /**
406 * ags_synth_util_set_frequency:
407 * @synth_util: the #AgsSynthUtil-struct
408 * @frequency: the frequency
409 *
410 * Set @frequency of @synth_util.
411 *
412 * Since: 3.9.3
413 */
414 void
ags_synth_util_set_frequency(AgsSynthUtil * synth_util,gdouble frequency)415 ags_synth_util_set_frequency(AgsSynthUtil *synth_util,
416 gdouble frequency)
417 {
418 if(synth_util == NULL){
419 return;
420 }
421
422 synth_util->frequency = frequency;
423 }
424
425 /**
426 * ags_synth_util_get_phase:
427 * @synth_util: the #AgsSynthUtil-struct
428 *
429 * Get phase of @synth_util.
430 *
431 * Returns: the phase
432 *
433 * Since: 3.9.3
434 */
435 gdouble
ags_synth_util_get_phase(AgsSynthUtil * synth_util)436 ags_synth_util_get_phase(AgsSynthUtil *synth_util)
437 {
438 if(synth_util == NULL){
439 return(1.0);
440 }
441
442 return(synth_util->phase);
443 }
444
445 /**
446 * ags_synth_util_set_phase:
447 * @synth_util: the #AgsSynthUtil-struct
448 * @phase: the phase
449 *
450 * Set @phase of @synth_util.
451 *
452 * Since: 3.9.3
453 */
454 void
ags_synth_util_set_phase(AgsSynthUtil * synth_util,gdouble phase)455 ags_synth_util_set_phase(AgsSynthUtil *synth_util,
456 gdouble phase)
457 {
458 if(synth_util == NULL){
459 return;
460 }
461
462 synth_util->phase = phase;
463 }
464
465 /**
466 * ags_synth_util_get_volume:
467 * @synth_util: the #AgsSynthUtil-struct
468 *
469 * Get volume of @synth_util.
470 *
471 * Returns: the volume
472 *
473 * Since: 3.9.3
474 */
475 gdouble
ags_synth_util_get_volume(AgsSynthUtil * synth_util)476 ags_synth_util_get_volume(AgsSynthUtil *synth_util)
477 {
478 if(synth_util == NULL){
479 return(1.0);
480 }
481
482 return(synth_util->volume);
483 }
484
485 /**
486 * ags_synth_util_set_volume:
487 * @synth_util: the #AgsSynthUtil-struct
488 * @volume: the volume
489 *
490 * Set @volume of @synth_util.
491 *
492 * Since: 3.9.3
493 */
494 void
ags_synth_util_set_volume(AgsSynthUtil * synth_util,gdouble volume)495 ags_synth_util_set_volume(AgsSynthUtil *synth_util,
496 gdouble volume)
497 {
498 if(synth_util == NULL){
499 return;
500 }
501
502 synth_util->volume = volume;
503 }
504
505 /**
506 * ags_synth_util_get_frame_count:
507 * @synth_util: the #AgsSynthUtil-struct
508 *
509 * Get frame count of @synth_util.
510 *
511 * Returns: the frame count
512 *
513 * Since: 3.9.3
514 */
515 guint
ags_synth_util_get_frame_count(AgsSynthUtil * synth_util)516 ags_synth_util_get_frame_count(AgsSynthUtil *synth_util)
517 {
518 if(synth_util == NULL){
519 return(0);
520 }
521
522 return(synth_util->frame_count);
523 }
524
525 /**
526 * ags_synth_util_set_frame_count:
527 * @synth_util: the #AgsSynthUtil-struct
528 * @frame_count: the frame count
529 *
530 * Set @frame_count of @synth_util.
531 *
532 * Since: 3.9.3
533 */
534 void
ags_synth_util_set_frame_count(AgsSynthUtil * synth_util,guint frame_count)535 ags_synth_util_set_frame_count(AgsSynthUtil *synth_util,
536 guint frame_count)
537 {
538 if(synth_util == NULL){
539 return;
540 }
541
542 synth_util->frame_count = frame_count;
543 }
544
545 /**
546 * ags_synth_util_get_offset:
547 * @synth_util: the #AgsSynthUtil-struct
548 *
549 * Get offset of @synth_util.
550 *
551 * Returns: the offset
552 *
553 * Since: 3.9.3
554 */
555 guint
ags_synth_util_get_offset(AgsSynthUtil * synth_util)556 ags_synth_util_get_offset(AgsSynthUtil *synth_util)
557 {
558 if(synth_util == NULL){
559 return(0);
560 }
561
562 return(synth_util->offset);
563 }
564
565 /**
566 * ags_synth_util_set_offset:
567 * @synth_util: the #AgsSynthUtil-struct
568 * @offset: the offset
569 *
570 * Set @offset of @synth_util.
571 *
572 * Since: 3.9.3
573 */
574 void
ags_synth_util_set_offset(AgsSynthUtil * synth_util,guint offset)575 ags_synth_util_set_offset(AgsSynthUtil *synth_util,
576 guint offset)
577 {
578 if(synth_util == NULL){
579 return;
580 }
581
582 synth_util->offset = offset;
583 }
584
585 /**
586 * ags_synth_util_get_xcross_count_s8:
587 * @buffer: the buffer containing audio data
588 * @buffer_size: the buffer size
589 *
590 * Get zero-cross count.
591 *
592 * Returns: the count of zero-crossings
593 *
594 * Since: 3.0.0
595 */
596 guint
ags_synth_util_get_xcross_count_s8(gint8 * buffer,guint buffer_size)597 ags_synth_util_get_xcross_count_s8(gint8 *buffer,
598 guint buffer_size)
599 {
600 guint count;
601 guint i;
602 gboolean negative;
603
604 if(buffer == NULL){
605 return(0);
606 }
607
608 count = 0;
609
610 if(buffer[0] > 0){
611 negative = FALSE;
612 }else{
613 negative = TRUE;
614 }
615
616 for(i = 1; i < buffer_size; i++){
617 if(negative &&
618 buffer[i] > 0){
619 count++;
620
621 negative = FALSE;
622 }else if(!negative &&
623 buffer[i] < 0){
624 count++;
625
626 negative = TRUE;
627 }
628 }
629
630 return(count);
631 }
632
633 /**
634 * ags_synth_util_get_xcross_count_s16:
635 * @buffer: the buffer containing audio data
636 * @buffer_size: the buffer size
637 *
638 * Get zero-cross count.
639 *
640 * Returns: the count of zero-crossings
641 *
642 * Since: 3.0.0
643 */
644 guint
ags_synth_util_get_xcross_count_s16(gint16 * buffer,guint buffer_size)645 ags_synth_util_get_xcross_count_s16(gint16 *buffer,
646 guint buffer_size)
647 {
648 guint count;
649 guint i;
650 gboolean negative;
651
652 if(buffer == NULL){
653 return(0);
654 }
655
656 count = 0;
657
658 if(buffer[0] > 0){
659 negative = FALSE;
660 }else{
661 negative = TRUE;
662 }
663
664 for(i = 1; i < buffer_size; i++){
665 if(negative &&
666 buffer[i] > 0){
667 count++;
668
669 negative = FALSE;
670 }else if(!negative &&
671 buffer[i] < 0){
672 count++;
673
674 negative = TRUE;
675 }
676 }
677
678 return(count);
679 }
680
681 /**
682 * ags_synth_util_get_xcross_count_s24:
683 * @buffer: the buffer containing audio data
684 * @buffer_size: the buffer size
685 *
686 * Get zero-cross count.
687 *
688 * Returns: the count of zero-crossings
689 *
690 * Since: 3.0.0
691 */
692 guint
ags_synth_util_get_xcross_count_s24(gint32 * buffer,guint buffer_size)693 ags_synth_util_get_xcross_count_s24(gint32 *buffer,
694 guint buffer_size)
695 {
696 guint count;
697 guint i;
698 gboolean negative;
699
700 if(buffer == NULL){
701 return(0);
702 }
703
704 count = 0;
705
706 if(buffer[0] > 0){
707 negative = FALSE;
708 }else{
709 negative = TRUE;
710 }
711
712 for(i = 1; i < buffer_size; i++){
713 if(negative &&
714 buffer[i] > 0){
715 count++;
716
717 negative = FALSE;
718 }else if(!negative &&
719 buffer[i] < 0){
720 count++;
721
722 negative = TRUE;
723 }
724 }
725
726 return(count);
727 }
728
729 /**
730 * ags_synth_util_get_xcross_count_s32:
731 * @buffer: the buffer containing audio data
732 * @buffer_size: the buffer size
733 *
734 * Get zero-cross count.
735 *
736 * Returns: the count of zero-crossings
737 *
738 * Since: 3.0.0
739 */
740 guint
ags_synth_util_get_xcross_count_s32(gint32 * buffer,guint buffer_size)741 ags_synth_util_get_xcross_count_s32(gint32 *buffer,
742 guint buffer_size)
743 {
744 guint count;
745 guint i;
746 gboolean negative;
747
748 if(buffer == NULL){
749 return(0);
750 }
751
752 count = 0;
753
754 if(buffer[0] > 0){
755 negative = FALSE;
756 }else{
757 negative = TRUE;
758 }
759
760 for(i = 1; i < buffer_size; i++){
761 if(negative &&
762 buffer[i] > 0){
763 count++;
764
765 negative = FALSE;
766 }else if(!negative &&
767 buffer[i] < 0){
768 count++;
769
770 negative = TRUE;
771 }
772 }
773
774 return(count);
775 }
776
777 /**
778 * ags_synth_util_get_xcross_count_s64:
779 * @buffer: the buffer containing audio data
780 * @buffer_size: the buffer size
781 *
782 * Get zero-cross count.
783 *
784 * Returns: the count of zero-crossings
785 *
786 * Since: 3.0.0
787 */
788 guint
ags_synth_util_get_xcross_count_s64(gint64 * buffer,guint buffer_size)789 ags_synth_util_get_xcross_count_s64(gint64 *buffer,
790 guint buffer_size)
791 {
792 guint count;
793 guint i;
794 gboolean negative;
795
796 if(buffer == NULL){
797 return(0);
798 }
799
800 count = 0;
801
802 if(buffer[0] > 0){
803 negative = FALSE;
804 }else{
805 negative = TRUE;
806 }
807
808 for(i = 1; i < buffer_size; i++){
809 if(negative &&
810 buffer[i] > 0){
811 count++;
812
813 negative = FALSE;
814 }else if(!negative &&
815 buffer[i] < 0){
816 count++;
817
818 negative = TRUE;
819 }
820 }
821
822 return(count);
823 }
824
825 /**
826 * ags_synth_util_get_xcross_count_float:
827 * @buffer: the buffer containing audio data
828 * @buffer_size: the buffer size
829 *
830 * Get zero-cross count.
831 *
832 * Returns: the count of zero-crossings
833 *
834 * Since: 3.0.0
835 */
836 guint
ags_synth_util_get_xcross_count_float(gfloat * buffer,guint buffer_size)837 ags_synth_util_get_xcross_count_float(gfloat *buffer,
838 guint buffer_size)
839 {
840 guint count;
841 guint i;
842 gboolean negative;
843
844 if(buffer == NULL){
845 return(0);
846 }
847
848 count = 0;
849
850 if(buffer[0] > 0.0){
851 negative = FALSE;
852 }else{
853 negative = TRUE;
854 }
855
856 for(i = 1; i < buffer_size; i++){
857 if(negative &&
858 buffer[i] > 0.0){
859 count++;
860
861 negative = FALSE;
862 }else if(!negative &&
863 buffer[i] < 0.0){
864 count++;
865
866 negative = TRUE;
867 }
868 }
869
870 return(count);
871 }
872
873 /**
874 * ags_synth_util_get_xcross_count_double:
875 * @buffer: the buffer containing audio data
876 * @buffer_size: the buffer size
877 *
878 * Get zero-cross count.
879 *
880 * Returns: the count of zero-crossings
881 *
882 * Since: 3.0.0
883 */
884 guint
ags_synth_util_get_xcross_count_double(gdouble * buffer,guint buffer_size)885 ags_synth_util_get_xcross_count_double(gdouble *buffer,
886 guint buffer_size)
887 {
888 guint count;
889
890 guint i;
891 gboolean negative;
892
893 if(buffer == NULL){
894 return(0);
895 }
896
897 count = 0;
898
899 if(buffer[0] > 0){
900 negative = FALSE;
901 }else{
902 negative = TRUE;
903 }
904
905 for(i = 1; i < buffer_size; i++){
906 if(negative &&
907 buffer[i] > 0.0){
908 count++;
909
910 negative = FALSE;
911 }else if(!negative &&
912 buffer[i] < 0.0){
913 count++;
914
915 negative = TRUE;
916 }
917 }
918
919 return(count);
920 }
921
922 /**
923 * ags_synth_util_get_xcross_count_complex:
924 * @buffer: the buffer containing audio data
925 * @buffer_size: the buffer size
926 *
927 * Get zero-cross count.
928 *
929 * Returns: the count of zero-crossings
930 *
931 * Since: 3.0.0
932 */
933 guint
ags_synth_util_get_xcross_count_complex(AgsComplex * buffer,guint buffer_size)934 ags_synth_util_get_xcross_count_complex(AgsComplex *buffer,
935 guint buffer_size)
936 {
937 gfloat **ptr_ptr;
938 gfloat *ptr;
939
940 gfloat value;
941 guint count;
942 complex z;
943
944 guint i;
945 gboolean negative;
946
947 if(buffer == NULL){
948 return(0);
949 }
950
951 count = 0;
952
953 ptr = &value;
954 ptr_ptr = &ptr;
955
956 AGS_FOURIER_TRANSFORM_UTIL_INVERSE_STFT_FLOAT_FRAME(buffer, 1,
957 0, buffer_size,
958 ptr_ptr);
959
960 if(value > 0.0){
961 negative = FALSE;
962 }else{
963 negative = TRUE;
964 }
965
966 for(i = 1; i < buffer_size; i++){
967 AGS_FOURIER_TRANSFORM_UTIL_INVERSE_STFT_FLOAT_FRAME(buffer + i, 1,
968 i, buffer_size,
969 ptr_ptr);
970
971 if(negative &&
972 value > 0.0){
973 count++;
974
975 negative = FALSE;
976 }else if(!negative &&
977 value < 0.0){
978 count++;
979
980 negative = TRUE;
981 }
982 }
983
984 return(count);
985 }
986
987 /**
988 * ags_synth_util_get_xcross_count:
989 * @buffer: the buffer containing audio data
990 * @audio_buffer_util_format: the audio buffer util format
991 * @buffer_size: the buffer size
992 *
993 * Get zero-cross count.
994 *
995 * Returns: the count of zero-crossings
996 *
997 * Since: 3.0.0
998 */
999 guint
ags_synth_util_get_xcross_count(void * buffer,guint audio_buffer_util_format,guint buffer_size)1000 ags_synth_util_get_xcross_count(void *buffer,
1001 guint audio_buffer_util_format,
1002 guint buffer_size)
1003 {
1004 guint count;
1005
1006 if(buffer == NULL){
1007 return(0);
1008 }
1009
1010 count = 0;
1011
1012 switch(audio_buffer_util_format){
1013 case AGS_AUDIO_BUFFER_UTIL_S8:
1014 {
1015 count = ags_synth_util_get_xcross_count_s8((gint8 *) buffer,
1016 buffer_size);
1017 }
1018 break;
1019 case AGS_AUDIO_BUFFER_UTIL_S16:
1020 {
1021 count = ags_synth_util_get_xcross_count_s16((gint16 *) buffer,
1022 buffer_size);
1023 }
1024 break;
1025 case AGS_AUDIO_BUFFER_UTIL_S24:
1026 {
1027 count = ags_synth_util_get_xcross_count_s24((gint32 *) buffer,
1028 buffer_size);
1029 }
1030 break;
1031 case AGS_AUDIO_BUFFER_UTIL_S32:
1032 {
1033 count = ags_synth_util_get_xcross_count_s32((gint32 *) buffer,
1034 buffer_size);
1035 }
1036 break;
1037 case AGS_AUDIO_BUFFER_UTIL_S64:
1038 {
1039 count = ags_synth_util_get_xcross_count_s64((gint64 *) buffer,
1040 buffer_size);
1041 }
1042 break;
1043 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
1044 {
1045 count = ags_synth_util_get_xcross_count_float((gfloat *) buffer,
1046 buffer_size);
1047 }
1048 break;
1049 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
1050 {
1051 count = ags_synth_util_get_xcross_count_double((gdouble *) buffer,
1052 buffer_size);
1053 }
1054 break;
1055 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
1056 {
1057 count = ags_synth_util_get_xcross_count_complex((AgsComplex *) buffer,
1058 buffer_size);
1059 }
1060 break;
1061 default:
1062 {
1063 g_warning("ags_synth_util_get_xcross_count() - unsupported format");
1064 }
1065 }
1066
1067 return(count);
1068 }
1069
1070 /**
1071 * ags_synth_util_compute_sin_s8:
1072 * @synth_util: the #AgsSynthUtil-struct
1073 *
1074 * Compute sine synth of signed 8 bit data.
1075 *
1076 * Since: 3.9.3
1077 */
1078 void
ags_synth_util_compute_sin_s8(AgsSynthUtil * synth_util)1079 ags_synth_util_compute_sin_s8(AgsSynthUtil *synth_util)
1080 {
1081 gint8 *source, *tmp_source;
1082
1083 gdouble volume;
1084 guint i, i_stop;
1085
1086 static const gdouble scale = 127.0;
1087
1088 if(synth_util == NULL ||
1089 synth_util->source == NULL){
1090 return;
1091 }
1092
1093 source = synth_util->source;
1094
1095 volume = scale * synth_util->volume;
1096
1097 i = 0;
1098
1099 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
1100 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1101
1102 if(synth_util->offset + i_stop > synth_util->frame_count){
1103 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1104 }
1105
1106 for(; i < i_stop;){
1107 ags_v8double v_buffer, v_sine;
1108
1109 tmp_source = source;
1110
1111 v_buffer = (ags_v8double) {
1112 (gdouble) *(tmp_source),
1113 (gdouble) *(tmp_source += synth_util->source_stride),
1114 (gdouble) *(tmp_source += synth_util->source_stride),
1115 (gdouble) *(tmp_source += synth_util->source_stride),
1116 (gdouble) *(tmp_source += synth_util->source_stride),
1117 (gdouble) *(tmp_source += synth_util->source_stride),
1118 (gdouble) *(tmp_source += synth_util->source_stride),
1119 (gdouble) *(tmp_source += synth_util->source_stride)
1120 };
1121
1122 v_sine = (ags_v8double) {
1123 sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1124 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1125 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1126 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1127 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1128 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1129 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1130 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate)
1131 };
1132
1133 i++;
1134
1135 v_sine *= volume;
1136
1137 v_buffer += v_sine;
1138
1139 *(source) = (gint8) v_buffer[0];
1140 *(source += synth_util->source_stride) = (gint8) v_buffer[1];
1141 *(source += synth_util->source_stride) = (gint8) v_buffer[2];
1142 *(source += synth_util->source_stride) = (gint8) v_buffer[3];
1143 *(source += synth_util->source_stride) = (gint8) v_buffer[4];
1144 *(source += synth_util->source_stride) = (gint8) v_buffer[5];
1145 *(source += synth_util->source_stride) = (gint8) v_buffer[6];
1146 *(source += synth_util->source_stride) = (gint8) v_buffer[7];
1147
1148 source += synth_util->source_stride;
1149 }
1150 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
1151 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1152
1153 if(synth_util->offset + i_stop > synth_util->frame_count){
1154 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1155 }
1156
1157 for(; i < i_stop;){
1158 double ret_v_buffer[8], tmp_ret_v_buffer[8];
1159
1160 tmp_source = source;
1161
1162 double v_buffer[] = {
1163 (double) *(tmp_source),
1164 (double) *(tmp_source += synth_util->source_stride),
1165 (double) *(tmp_source += synth_util->source_stride),
1166 (double) *(tmp_source += synth_util->source_stride),
1167 (double) *(tmp_source += synth_util->source_stride),
1168 (double) *(tmp_source += synth_util->source_stride),
1169 (double) *(tmp_source += synth_util->source_stride),
1170 (double) *(tmp_source += synth_util->source_stride)};
1171 double v_sine[] = {
1172 sin((double) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1173 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1174 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1175 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1176 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1177 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1178 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1179 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate)};
1180
1181 double v_volume[] = {(double) volume};
1182
1183 i++;
1184
1185 vDSP_vmulD(v_sine, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
1186 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
1187
1188 *(source) = (gint8) ret_v_buffer[0];
1189 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[1];
1190 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[2];
1191 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[3];
1192 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[4];
1193 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[5];
1194 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[6];
1195 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[7];
1196
1197 source += synth_util->source_stride;
1198 }
1199 #else
1200 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1201
1202 if(synth_util->offset + i_stop > synth_util->frame_count){
1203 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1204 }
1205
1206 for(; i < i_stop;){
1207 tmp_source = source;
1208
1209 (*source) = (gint8) ((gint16) (tmp_source)[0] + (gint16) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1210 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1211 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1212 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1213 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1214 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1215 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1216 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1217
1218 source += synth_util->source_stride;
1219 i++;
1220 }
1221 #endif
1222
1223 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
1224 source[0] = (gint8) ((gint16) source[0] + (gint16) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1225
1226 source += synth_util->source_stride;
1227 i++;
1228 }
1229 }
1230
1231 /**
1232 * ags_synth_util_compute_sin_s16:
1233 * @synth_util: the #AgsSynthUtil-struct
1234 *
1235 * Compute sine synth of signed 16 bit data.
1236 *
1237 * Since: 3.9.3
1238 */
1239 void
ags_synth_util_compute_sin_s16(AgsSynthUtil * synth_util)1240 ags_synth_util_compute_sin_s16(AgsSynthUtil *synth_util)
1241 {
1242 gint16 *source, *tmp_source;
1243
1244 gdouble volume;
1245 guint i, i_stop;
1246
1247 static const gdouble scale = 32767.0;
1248
1249 if(synth_util == NULL ||
1250 synth_util->source == NULL){
1251 return;
1252 }
1253
1254 source = synth_util->source;
1255
1256 volume = scale * synth_util->volume;
1257
1258 i = 0;
1259
1260 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
1261 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1262
1263 if(synth_util->offset + i_stop > synth_util->frame_count){
1264 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1265 }
1266
1267 for(; i < i_stop;){
1268 ags_v8double v_buffer, v_sine;
1269
1270 tmp_source = source;
1271
1272 v_buffer = (ags_v8double) {
1273 (gdouble) *(tmp_source),
1274 (gdouble) *(tmp_source += synth_util->source_stride),
1275 (gdouble) *(tmp_source += synth_util->source_stride),
1276 (gdouble) *(tmp_source += synth_util->source_stride),
1277 (gdouble) *(tmp_source += synth_util->source_stride),
1278 (gdouble) *(tmp_source += synth_util->source_stride),
1279 (gdouble) *(tmp_source += synth_util->source_stride),
1280 (gdouble) *(tmp_source += synth_util->source_stride)
1281 };
1282
1283 v_sine = (ags_v8double) {
1284 sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1285 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1286 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1287 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1288 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1289 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1290 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1291 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate)
1292 };
1293
1294 i++;
1295
1296 v_sine *= volume;
1297
1298 v_buffer += v_sine;
1299
1300 *(source) = (gint16) v_buffer[0];
1301 *(source += synth_util->source_stride) = (gint16) v_buffer[1];
1302 *(source += synth_util->source_stride) = (gint16) v_buffer[2];
1303 *(source += synth_util->source_stride) = (gint16) v_buffer[3];
1304 *(source += synth_util->source_stride) = (gint16) v_buffer[4];
1305 *(source += synth_util->source_stride) = (gint16) v_buffer[5];
1306 *(source += synth_util->source_stride) = (gint16) v_buffer[6];
1307 *(source += synth_util->source_stride) = (gint16) v_buffer[7];
1308
1309 source += synth_util->source_stride;
1310 }
1311 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
1312 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1313
1314 if(synth_util->offset + i_stop > synth_util->frame_count){
1315 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1316 }
1317
1318 for(; i < i_stop;){
1319 double ret_v_buffer[8], tmp_ret_v_buffer[8];
1320
1321 tmp_source = source;
1322
1323 double v_buffer[] = {
1324 (double) *(tmp_source),
1325 (double) *(tmp_source += synth_util->source_stride),
1326 (double) *(tmp_source += synth_util->source_stride),
1327 (double) *(tmp_source += synth_util->source_stride),
1328 (double) *(tmp_source += synth_util->source_stride),
1329 (double) *(tmp_source += synth_util->source_stride),
1330 (double) *(tmp_source += synth_util->source_stride),
1331 (double) *(tmp_source += synth_util->source_stride)};
1332 double v_sine[] = {
1333 sin((double) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1334 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1335 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1336 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1337 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1338 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1339 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1340 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate)};
1341
1342 double v_volume[] = {(double) volume};
1343
1344 i++;
1345
1346 vDSP_vmulD(v_sine, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
1347 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
1348
1349 *(source) = (gint16) ret_v_buffer[0];
1350 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[1];
1351 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[2];
1352 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[3];
1353 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[4];
1354 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[5];
1355 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[6];
1356 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[7];
1357
1358 source += synth_util->source_stride;
1359 }
1360 #else
1361 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1362
1363 if(synth_util->offset + i_stop > synth_util->frame_count){
1364 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1365 }
1366
1367 for(; i < i_stop;){
1368 tmp_source = source;
1369
1370 (*source) = (gint16) ((gint32) (tmp_source)[0] + (gint32) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1371 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1372 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1373 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1374 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1375 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1376 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1377 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1378
1379 source += synth_util->source_stride;
1380 i++;
1381 }
1382 #endif
1383
1384 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
1385 source[0] = (gint16) ((gint32) source[0] + (gint32) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1386
1387 source += synth_util->source_stride;
1388 i++;
1389 }
1390 }
1391
1392 /**
1393 * ags_synth_util_compute_sin_s24:
1394 * @synth_util: the #AgsSynthUtil-struct
1395 *
1396 * Compute sine synth of signed 24 bit data.
1397 *
1398 * Since: 3.9.3
1399 */
1400 void
ags_synth_util_compute_sin_s24(AgsSynthUtil * synth_util)1401 ags_synth_util_compute_sin_s24(AgsSynthUtil *synth_util)
1402 {
1403 gint32 *source, *tmp_source;
1404
1405 gdouble volume;
1406 guint i, i_stop;
1407
1408 static const gdouble scale = 8388607.0;
1409
1410 if(synth_util == NULL ||
1411 synth_util->source == NULL){
1412 return;
1413 }
1414
1415 source = synth_util->source;
1416
1417 volume = scale * synth_util->volume;
1418
1419 i = 0;
1420
1421 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
1422 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1423
1424 if(synth_util->offset + i_stop > synth_util->frame_count){
1425 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1426 }
1427
1428 for(; i < i_stop;){
1429 ags_v8double v_buffer, v_sine;
1430
1431 tmp_source = source;
1432
1433 v_buffer = (ags_v8double) {
1434 (gdouble) *(tmp_source),
1435 (gdouble) *(tmp_source += synth_util->source_stride),
1436 (gdouble) *(tmp_source += synth_util->source_stride),
1437 (gdouble) *(tmp_source += synth_util->source_stride),
1438 (gdouble) *(tmp_source += synth_util->source_stride),
1439 (gdouble) *(tmp_source += synth_util->source_stride),
1440 (gdouble) *(tmp_source += synth_util->source_stride),
1441 (gdouble) *(tmp_source += synth_util->source_stride)
1442 };
1443
1444 v_sine = (ags_v8double) {
1445 sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1446 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1447 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1448 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1449 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1450 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1451 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1452 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate)
1453 };
1454
1455 i++;
1456
1457 v_sine *= volume;
1458
1459 v_buffer += v_sine;
1460
1461 *(source) = (gint32) v_buffer[0];
1462 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
1463 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
1464 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
1465 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
1466 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
1467 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
1468 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
1469
1470 source += synth_util->source_stride;
1471 }
1472 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
1473 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1474
1475 if(synth_util->offset + i_stop > synth_util->frame_count){
1476 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1477 }
1478
1479 for(; i < i_stop;){
1480 double ret_v_buffer[8], tmp_ret_v_buffer[8];
1481
1482 tmp_source = source;
1483
1484 double v_buffer[] = {
1485 (double) *(tmp_source),
1486 (double) *(tmp_source += synth_util->source_stride),
1487 (double) *(tmp_source += synth_util->source_stride),
1488 (double) *(tmp_source += synth_util->source_stride),
1489 (double) *(tmp_source += synth_util->source_stride),
1490 (double) *(tmp_source += synth_util->source_stride),
1491 (double) *(tmp_source += synth_util->source_stride),
1492 (double) *(tmp_source += synth_util->source_stride)};
1493 double v_sine[] = {
1494 sin((double) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1495 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1496 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1497 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1498 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1499 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1500 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1501 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate)};
1502
1503 double v_volume[] = {(double) volume};
1504
1505 i++;
1506
1507 vDSP_vmulD(v_sine, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
1508 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
1509
1510 *(source) = (gint32) ret_v_buffer[0];
1511 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
1512 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
1513 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
1514 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
1515 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
1516 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
1517 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
1518
1519 source += synth_util->source_stride;
1520 }
1521 #else
1522 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1523
1524 if(synth_util->offset + i_stop > synth_util->frame_count){
1525 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1526 }
1527
1528 for(; i < i_stop;){
1529 tmp_source = source;
1530
1531 (*source) = (gint32) ((gint32) (tmp_source)[0] + (gint32) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1532 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1533 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1534 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1535 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1536 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1537 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1538 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1539
1540 source += synth_util->source_stride;
1541 i++;
1542 }
1543 #endif
1544
1545 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
1546 source[0] = (gint32) ((gint32) source[0] + (gint32) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1547
1548 source += synth_util->source_stride;
1549 i++;
1550 }
1551 }
1552
1553 /**
1554 * ags_synth_util_compute_sin_s32:
1555 * @synth_util: the #AgsSynthUtil-struct
1556 *
1557 * Compute sine synth of signed 32 bit data.
1558 *
1559 * Since: 3.9.3
1560 */
1561 void
ags_synth_util_compute_sin_s32(AgsSynthUtil * synth_util)1562 ags_synth_util_compute_sin_s32(AgsSynthUtil *synth_util)
1563 {
1564 gint32 *source, *tmp_source;
1565
1566 gdouble volume;
1567 guint i, i_stop;
1568
1569 static const gdouble scale = 214748363.0;
1570
1571 if(synth_util == NULL ||
1572 synth_util->source == NULL){
1573 return;
1574 }
1575
1576 source = synth_util->source;
1577
1578 volume = scale * synth_util->volume;
1579
1580 i = 0;
1581
1582 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
1583 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1584
1585 if(synth_util->offset + i_stop > synth_util->frame_count){
1586 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1587 }
1588
1589 for(; i < i_stop;){
1590 ags_v8double v_buffer, v_sine;
1591
1592 tmp_source = source;
1593
1594 v_buffer = (ags_v8double) {
1595 (gdouble) *(tmp_source),
1596 (gdouble) *(tmp_source += synth_util->source_stride),
1597 (gdouble) *(tmp_source += synth_util->source_stride),
1598 (gdouble) *(tmp_source += synth_util->source_stride),
1599 (gdouble) *(tmp_source += synth_util->source_stride),
1600 (gdouble) *(tmp_source += synth_util->source_stride),
1601 (gdouble) *(tmp_source += synth_util->source_stride),
1602 (gdouble) *(tmp_source += synth_util->source_stride)
1603 };
1604
1605 v_sine = (ags_v8double) {
1606 sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1607 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1608 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1609 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1610 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1611 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1612 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1613 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate)
1614 };
1615
1616 i++;
1617
1618 v_sine *= volume;
1619
1620 v_buffer += v_sine;
1621
1622 *(source) = (gint32) v_buffer[0];
1623 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
1624 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
1625 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
1626 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
1627 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
1628 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
1629 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
1630
1631 source += synth_util->source_stride;
1632 }
1633 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
1634 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1635
1636 if(synth_util->offset + i_stop > synth_util->frame_count){
1637 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1638 }
1639
1640 for(; i < i_stop;){
1641 double ret_v_buffer[8], tmp_ret_v_buffer[8];
1642
1643 tmp_source = source;
1644
1645 double v_buffer[] = {
1646 (double) *(tmp_source),
1647 (double) *(tmp_source += synth_util->source_stride),
1648 (double) *(tmp_source += synth_util->source_stride),
1649 (double) *(tmp_source += synth_util->source_stride),
1650 (double) *(tmp_source += synth_util->source_stride),
1651 (double) *(tmp_source += synth_util->source_stride),
1652 (double) *(tmp_source += synth_util->source_stride),
1653 (double) *(tmp_source += synth_util->source_stride)};
1654 double v_sine[] = {
1655 sin((double) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1656 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1657 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1658 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1659 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1660 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1661 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1662 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate)};
1663
1664 double v_volume[] = {(double) volume};
1665
1666 i++;
1667
1668 vDSP_vmulD(v_sine, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
1669 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
1670
1671 *(source) = (gint32) ret_v_buffer[0];
1672 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
1673 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
1674 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
1675 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
1676 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
1677 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
1678 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
1679
1680 source += synth_util->source_stride;
1681 }
1682 #else
1683 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1684
1685 if(synth_util->offset + i_stop > synth_util->frame_count){
1686 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1687 }
1688
1689 for(; i < i_stop;){
1690 tmp_source = source;
1691
1692 (*source) = (gint32) ((gint64) (tmp_source)[0] + (gint64) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1693 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1694 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1695 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1696 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1697 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1698 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1699 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1700
1701 source += synth_util->source_stride;
1702 i++;
1703 }
1704 #endif
1705
1706 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
1707 source[0] = (gint32) ((gint64) source[0] + (gint64) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1708
1709 source += synth_util->source_stride;
1710 i++;
1711 }
1712 }
1713
1714 /**
1715 * ags_synth_util_compute_sin_s64:
1716 * @synth_util: the #AgsSynthUtil-struct
1717 *
1718 * Compute sine synth of signed 64 bit data.
1719 *
1720 * Since: 3.9.3
1721 */
1722 void
ags_synth_util_compute_sin_s64(AgsSynthUtil * synth_util)1723 ags_synth_util_compute_sin_s64(AgsSynthUtil *synth_util)
1724 {
1725 gint64 *source, *tmp_source;
1726
1727 gdouble volume;
1728 guint i, i_stop;
1729
1730 static const gdouble scale = 9223372036854775807.0;
1731
1732 if(synth_util == NULL ||
1733 synth_util->source == NULL){
1734 return;
1735 }
1736
1737 source = synth_util->source;
1738
1739 volume = scale * synth_util->volume;
1740
1741 i = 0;
1742
1743 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
1744 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1745
1746 if(synth_util->offset + i_stop > synth_util->frame_count){
1747 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1748 }
1749
1750 for(; i < i_stop;){
1751 ags_v8double v_buffer, v_sine;
1752
1753 tmp_source = source;
1754
1755 v_buffer = (ags_v8double) {
1756 (gdouble) *(tmp_source),
1757 (gdouble) *(tmp_source += synth_util->source_stride),
1758 (gdouble) *(tmp_source += synth_util->source_stride),
1759 (gdouble) *(tmp_source += synth_util->source_stride),
1760 (gdouble) *(tmp_source += synth_util->source_stride),
1761 (gdouble) *(tmp_source += synth_util->source_stride),
1762 (gdouble) *(tmp_source += synth_util->source_stride),
1763 (gdouble) *(tmp_source += synth_util->source_stride)
1764 };
1765
1766 v_sine = (ags_v8double) {
1767 sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1768 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1769 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1770 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1771 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1772 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1773 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1774 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate)
1775 };
1776
1777 i++;
1778
1779 v_sine *= volume;
1780
1781 v_buffer += v_sine;
1782
1783 *(source) = (gint64) v_buffer[0];
1784 *(source += synth_util->source_stride) = (gint64) v_buffer[1];
1785 *(source += synth_util->source_stride) = (gint64) v_buffer[2];
1786 *(source += synth_util->source_stride) = (gint64) v_buffer[3];
1787 *(source += synth_util->source_stride) = (gint64) v_buffer[4];
1788 *(source += synth_util->source_stride) = (gint64) v_buffer[5];
1789 *(source += synth_util->source_stride) = (gint64) v_buffer[6];
1790 *(source += synth_util->source_stride) = (gint64) v_buffer[7];
1791
1792 source += synth_util->source_stride;
1793 }
1794 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
1795 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1796
1797 if(synth_util->offset + i_stop > synth_util->frame_count){
1798 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1799 }
1800
1801 for(; i < i_stop;){
1802 double ret_v_buffer[8], tmp_ret_v_buffer[8];
1803
1804 tmp_source = source;
1805
1806 double v_buffer[] = {
1807 (double) *(tmp_source),
1808 (double) *(tmp_source += synth_util->source_stride),
1809 (double) *(tmp_source += synth_util->source_stride),
1810 (double) *(tmp_source += synth_util->source_stride),
1811 (double) *(tmp_source += synth_util->source_stride),
1812 (double) *(tmp_source += synth_util->source_stride),
1813 (double) *(tmp_source += synth_util->source_stride),
1814 (double) *(tmp_source += synth_util->source_stride)};
1815 double v_sine[] = {
1816 sin((double) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1817 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1818 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1819 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1820 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1821 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1822 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1823 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate)};
1824
1825 double v_volume[] = {(double) volume};
1826
1827 i++;
1828
1829 vDSP_vmulD(v_sine, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
1830 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
1831
1832 *(source) = (gint64) ret_v_buffer[0];
1833 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[1];
1834 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[2];
1835 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[3];
1836 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[4];
1837 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[5];
1838 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[6];
1839 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[7];
1840
1841 source += synth_util->source_stride;
1842 }
1843 #else
1844 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1845
1846 if(synth_util->offset + i_stop > synth_util->frame_count){
1847 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1848 }
1849
1850 for(; i < i_stop;){
1851 tmp_source = source;
1852
1853 (*source) = (gint64) ((gint64) (tmp_source)[0] + (gint64) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1854 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1855 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1856 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1857 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1858 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1859 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1860 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1861
1862 source += synth_util->source_stride;
1863 i++;
1864 }
1865 #endif
1866
1867 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
1868 source[0] = (gint64) ((gint64) source[0] + (gint64) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
1869
1870 source += synth_util->source_stride;
1871 i++;
1872 }
1873 }
1874
1875 /**
1876 * ags_synth_util_compute_sin_float:
1877 * @synth_util: the #AgsSynthUtil-struct
1878 *
1879 * Compute sine synth of floating point data.
1880 *
1881 * Since: 3.9.3
1882 */
1883 void
ags_synth_util_compute_sin_float(AgsSynthUtil * synth_util)1884 ags_synth_util_compute_sin_float(AgsSynthUtil *synth_util)
1885 {
1886 gfloat *source, *tmp_source;
1887
1888 gdouble volume;
1889 guint i, i_stop;
1890
1891 if(synth_util == NULL ||
1892 synth_util->source == NULL){
1893 return;
1894 }
1895
1896 source = synth_util->source;
1897
1898 volume = synth_util->volume;
1899
1900 i = 0;
1901
1902 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
1903 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1904
1905 if(synth_util->offset + i_stop > synth_util->frame_count){
1906 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1907 }
1908
1909 for(; i < i_stop;){
1910 ags_v8double v_buffer, v_sine;
1911
1912 tmp_source = source;
1913
1914 v_buffer = (ags_v8double) {
1915 (gdouble) *(tmp_source),
1916 (gdouble) *(tmp_source += synth_util->source_stride),
1917 (gdouble) *(tmp_source += synth_util->source_stride),
1918 (gdouble) *(tmp_source += synth_util->source_stride),
1919 (gdouble) *(tmp_source += synth_util->source_stride),
1920 (gdouble) *(tmp_source += synth_util->source_stride),
1921 (gdouble) *(tmp_source += synth_util->source_stride),
1922 (gdouble) *(tmp_source += synth_util->source_stride)
1923 };
1924
1925 v_sine = (ags_v8double) {
1926 sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1927 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1928 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1929 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1930 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1931 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1932 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
1933 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate)
1934 };
1935
1936 i++;
1937
1938 v_sine *= volume;
1939
1940 v_buffer += v_sine;
1941
1942 *(source) = (gfloat) v_buffer[0];
1943 *(source += synth_util->source_stride) = (gfloat) v_buffer[1];
1944 *(source += synth_util->source_stride) = (gfloat) v_buffer[2];
1945 *(source += synth_util->source_stride) = (gfloat) v_buffer[3];
1946 *(source += synth_util->source_stride) = (gfloat) v_buffer[4];
1947 *(source += synth_util->source_stride) = (gfloat) v_buffer[5];
1948 *(source += synth_util->source_stride) = (gfloat) v_buffer[6];
1949 *(source += synth_util->source_stride) = (gfloat) v_buffer[7];
1950
1951 source += synth_util->source_stride;
1952 }
1953 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
1954 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
1955
1956 if(synth_util->offset + i_stop > synth_util->frame_count){
1957 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
1958 }
1959
1960 for(; i < i_stop;){
1961 double ret_v_buffer[8], tmp_ret_v_buffer[8];
1962
1963 tmp_source = source;
1964
1965 double v_buffer[] = {
1966 (double) *(tmp_source),
1967 (double) *(tmp_source += synth_util->source_stride),
1968 (double) *(tmp_source += synth_util->source_stride),
1969 (double) *(tmp_source += synth_util->source_stride),
1970 (double) *(tmp_source += synth_util->source_stride),
1971 (double) *(tmp_source += synth_util->source_stride),
1972 (double) *(tmp_source += synth_util->source_stride),
1973 (double) *(tmp_source += synth_util->source_stride)};
1974 double v_sine[] = {
1975 sin((double) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1976 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1977 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1978 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1979 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1980 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1981 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
1982 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate)};
1983
1984 double v_volume[] = {(double) volume};
1985
1986 i++;
1987
1988 vDSP_vmulD(v_sine, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
1989 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
1990
1991 *(source) = (gfloat) ret_v_buffer[0];
1992 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[1];
1993 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[2];
1994 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[3];
1995 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[4];
1996 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[5];
1997 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[6];
1998 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[7];
1999
2000 source += synth_util->source_stride;
2001 }
2002 #else
2003 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2004
2005 if(synth_util->offset + i_stop > synth_util->frame_count){
2006 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2007 }
2008
2009 for(; i < i_stop;){
2010 tmp_source = source;
2011
2012 (*source) = (gfloat) ((tmp_source)[0] + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2013 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2014 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2015 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2016 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2017 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2018 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2019 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2020
2021 source += synth_util->source_stride;
2022 i++;
2023 }
2024 #endif
2025
2026 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
2027 source[0] = (gfloat) (source[0] + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2028
2029 source += synth_util->source_stride;
2030 i++;
2031 }
2032 }
2033
2034 /**
2035 * ags_synth_util_compute_sin_double:
2036 * @synth_util: the #AgsSynthUtil-struct
2037 *
2038 * Compute sine synth of double precision floating point data.
2039 *
2040 * Since: 3.9.3
2041 */
2042 void
ags_synth_util_compute_sin_double(AgsSynthUtil * synth_util)2043 ags_synth_util_compute_sin_double(AgsSynthUtil *synth_util)
2044 {
2045 gdouble *source, *tmp_source;
2046
2047 gdouble volume;
2048 guint i, i_stop;
2049
2050 if(synth_util == NULL ||
2051 synth_util->source == NULL){
2052 return;
2053 }
2054
2055 source = synth_util->source;
2056
2057 volume = synth_util->volume;
2058
2059 i = 0;
2060
2061 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
2062 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2063
2064 if(synth_util->offset + i_stop > synth_util->frame_count){
2065 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2066 }
2067
2068 for(; i < i_stop;){
2069 ags_v8double v_buffer, v_sine;
2070
2071 tmp_source = source;
2072
2073 v_buffer = (ags_v8double) {
2074 (gdouble) *(tmp_source),
2075 (gdouble) *(tmp_source += synth_util->source_stride),
2076 (gdouble) *(tmp_source += synth_util->source_stride),
2077 (gdouble) *(tmp_source += synth_util->source_stride),
2078 (gdouble) *(tmp_source += synth_util->source_stride),
2079 (gdouble) *(tmp_source += synth_util->source_stride),
2080 (gdouble) *(tmp_source += synth_util->source_stride),
2081 (gdouble) *(tmp_source += synth_util->source_stride)
2082 };
2083
2084 v_sine = (ags_v8double) {
2085 sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
2086 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
2087 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
2088 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
2089 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
2090 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
2091 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate),
2092 sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate)
2093 };
2094
2095 i++;
2096
2097 v_sine *= volume;
2098
2099 v_buffer += v_sine;
2100
2101 *(source) = (gdouble) v_buffer[0];
2102 *(source += synth_util->source_stride) = (gdouble) v_buffer[1];
2103 *(source += synth_util->source_stride) = (gdouble) v_buffer[2];
2104 *(source += synth_util->source_stride) = (gdouble) v_buffer[3];
2105 *(source += synth_util->source_stride) = (gdouble) v_buffer[4];
2106 *(source += synth_util->source_stride) = (gdouble) v_buffer[5];
2107 *(source += synth_util->source_stride) = (gdouble) v_buffer[6];
2108 *(source += synth_util->source_stride) = (gdouble) v_buffer[7];
2109
2110 source += synth_util->source_stride;
2111 }
2112 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
2113 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2114
2115 if(synth_util->offset + i_stop > synth_util->frame_count){
2116 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2117 }
2118
2119 for(; i < i_stop;){
2120 double ret_v_buffer[8], tmp_ret_v_buffer[8];
2121
2122 tmp_source = source;
2123
2124 double v_buffer[] = {
2125 (double) *(tmp_source),
2126 (double) *(tmp_source += synth_util->source_stride),
2127 (double) *(tmp_source += synth_util->source_stride),
2128 (double) *(tmp_source += synth_util->source_stride),
2129 (double) *(tmp_source += synth_util->source_stride),
2130 (double) *(tmp_source += synth_util->source_stride),
2131 (double) *(tmp_source += synth_util->source_stride),
2132 (double) *(tmp_source += synth_util->source_stride)};
2133 double v_sine[] = {
2134 sin((double) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
2135 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
2136 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
2137 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
2138 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
2139 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
2140 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate),
2141 sin((double) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (double) synth_util->samplerate)};
2142
2143 double v_volume[] = {(double) volume};
2144
2145 i++;
2146
2147 vDSP_vmulD(v_sine, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
2148 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
2149
2150 *(source) = (gdouble) ret_v_buffer[0];
2151 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[1];
2152 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[2];
2153 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[3];
2154 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[4];
2155 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[5];
2156 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[6];
2157 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[7];
2158
2159 source += synth_util->source_stride;
2160 }
2161 #else
2162 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2163
2164 if(synth_util->offset + i_stop > synth_util->frame_count){
2165 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2166 }
2167
2168 for(; i < i_stop;){
2169 tmp_source = source;
2170
2171 (*source) = (gdouble) ((tmp_source)[0] + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2172 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2173 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2174 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2175 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2176 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2177 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2178 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2179
2180 source += synth_util->source_stride;
2181 i++;
2182 }
2183 #endif
2184
2185 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
2186 source[0] = (gdouble) (source[0] + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume));
2187
2188 source += synth_util->source_stride;
2189 i++;
2190 }
2191 }
2192
2193 /**
2194 * ags_synth_util_compute_sin_complex:
2195 * @synth_util: the #AgsSynthUtil-struct
2196 *
2197 * Compute sine synth of complex data.
2198 *
2199 * Since: 3.9.3
2200 */
2201 void
ags_synth_util_compute_sin_complex(AgsSynthUtil * synth_util)2202 ags_synth_util_compute_sin_complex(AgsSynthUtil *synth_util)
2203 {
2204 AgsComplex *source;
2205
2206 gdouble volume;
2207 guint i;
2208
2209 if(synth_util == NULL ||
2210 synth_util->source == NULL){
2211 return;
2212 }
2213
2214 source = synth_util->source;
2215
2216 volume = synth_util->volume;
2217
2218 i = 0;
2219
2220 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
2221 ags_complex_set(source,
2222 (ags_complex_get(source) + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) * volume)));
2223
2224 source += synth_util->source_stride;
2225 i++;
2226 }
2227 }
2228
2229 /**
2230 * ags_synth_util_compute_sin:
2231 * @synth_util: the #AgsSynthUtil-struct
2232 *
2233 * Compute sine synth.
2234 *
2235 * Since: 3.9.3
2236 */
2237 void
ags_synth_util_compute_sin(AgsSynthUtil * synth_util)2238 ags_synth_util_compute_sin(AgsSynthUtil *synth_util)
2239 {
2240 if(synth_util == NULL ||
2241 synth_util->source == NULL){
2242 return;
2243 }
2244
2245 switch(synth_util->audio_buffer_util_format){
2246 case AGS_AUDIO_BUFFER_UTIL_S8:
2247 {
2248 ags_synth_util_compute_sin_s8(synth_util);
2249 }
2250 break;
2251 case AGS_AUDIO_BUFFER_UTIL_S16:
2252 {
2253 ags_synth_util_compute_sin_s16(synth_util);
2254 }
2255 break;
2256 case AGS_AUDIO_BUFFER_UTIL_S24:
2257 {
2258 ags_synth_util_compute_sin_s24(synth_util);
2259 }
2260 break;
2261 case AGS_AUDIO_BUFFER_UTIL_S32:
2262 {
2263 ags_synth_util_compute_sin_s32(synth_util);
2264 }
2265 break;
2266 case AGS_AUDIO_BUFFER_UTIL_S64:
2267 {
2268 ags_synth_util_compute_sin_s64(synth_util);
2269 }
2270 break;
2271 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
2272 {
2273 ags_synth_util_compute_sin_float(synth_util);
2274 }
2275 break;
2276 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
2277 {
2278 ags_synth_util_compute_sin_double(synth_util);
2279 }
2280 break;
2281 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
2282 {
2283 ags_synth_util_compute_sin_complex(synth_util);
2284 }
2285 break;
2286 }
2287 }
2288
2289 /**
2290 * ags_synth_util_sin_s8:
2291 * @buffer: the audio buffer
2292 * @freq: the frequency of the sin wave
2293 * @phase: the phase of the sin wave
2294 * @volume: the volume of the sin wave
2295 * @samplerate: the samplerate
2296 * @offset: start frame
2297 * @n_frames: generate n frames
2298 *
2299 * Generate sinus wave.
2300 *
2301 * Since: 3.0.0
2302 */
2303 void
ags_synth_util_sin_s8(gint8 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)2304 ags_synth_util_sin_s8(gint8 *buffer,
2305 gdouble freq, gdouble phase, gdouble volume,
2306 guint samplerate,
2307 guint offset, guint n_frames)
2308 {
2309 static const gdouble scale = 127.0;
2310 guint i;
2311
2312 if(buffer == NULL){
2313 return;
2314 }
2315
2316 for(i = offset; i < offset + n_frames; i++){
2317 buffer[i] = (gint8) (0xff & ((gint16) buffer[i] + (gint16) (sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) * scale * volume)));
2318 }
2319 }
2320
2321 /**
2322 * ags_synth_util_sin_s16:
2323 * @buffer: the audio buffer
2324 * @freq: the frequency of the sin wave
2325 * @phase: the phase of the sin wave
2326 * @volume: the volume of the sin wave
2327 * @samplerate: the samplerate
2328 * @offset: start frame
2329 * @n_frames: generate n frames
2330 *
2331 * Generate sinus wave.
2332 *
2333 * Since: 3.0.0
2334 */
2335 void
ags_synth_util_sin_s16(gint16 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)2336 ags_synth_util_sin_s16(gint16 *buffer,
2337 gdouble freq, gdouble phase, gdouble volume,
2338 guint samplerate,
2339 guint offset, guint n_frames)
2340 {
2341 static const gdouble scale = 32767.0;
2342 guint i;
2343
2344 if(buffer == NULL){
2345 return;
2346 }
2347
2348 for(i = offset; i < offset + n_frames; i++){
2349 buffer[i] = (gint16) (0xffff & ((gint32) buffer[i] + (gint32) (sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) * scale * volume)));
2350 }
2351 }
2352
2353 /**
2354 * ags_synth_util_sin_s24:
2355 * @buffer: the audio buffer
2356 * @freq: the frequency of the sin wave
2357 * @phase: the phase of the sin wave
2358 * @volume: the volume of the sin wave
2359 * @samplerate: the samplerate
2360 * @offset: start frame
2361 * @n_frames: generate n frames
2362 *
2363 * Generate sinus wave.
2364 *
2365 * Since: 3.0.0
2366 */
2367 void
ags_synth_util_sin_s24(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)2368 ags_synth_util_sin_s24(gint32 *buffer,
2369 gdouble freq, gdouble phase, gdouble volume,
2370 guint samplerate,
2371 guint offset, guint n_frames)
2372 {
2373 static const gdouble scale = 8388607.0;
2374 guint i;
2375
2376 if(buffer == NULL){
2377 return;
2378 }
2379
2380 for(i = offset; i < offset + n_frames; i++){
2381 buffer[i] = (gint32) (0xffffffff & ((gint32) buffer[i] + (gint32) (sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) * scale * volume)));
2382 }
2383 }
2384
2385 /**
2386 * ags_synth_util_sin_s32:
2387 * @buffer: the audio buffer
2388 * @freq: the frequency of the sin wave
2389 * @phase: the phase of the sin wave
2390 * @volume: the volume of the sin wave
2391 * @samplerate: the samplerate
2392 * @offset: start frame
2393 * @n_frames: generate n frames
2394 *
2395 * Generate sinus wave.
2396 *
2397 * Since: 3.0.0
2398 */
2399 void
ags_synth_util_sin_s32(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)2400 ags_synth_util_sin_s32(gint32 *buffer,
2401 gdouble freq, gdouble phase, gdouble volume,
2402 guint samplerate,
2403 guint offset, guint n_frames)
2404 {
2405 static const gdouble scale = 214748363.0;
2406 guint i;
2407
2408 if(buffer == NULL){
2409 return;
2410 }
2411
2412 for(i = offset; i < offset + n_frames; i++){
2413 buffer[i] = (gint32) (0xffffffff & ((gint64) buffer[i] + (gint64) (sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) * scale * volume)));
2414 }
2415 }
2416
2417 /**
2418 * ags_synth_util_sin_s64:
2419 * @buffer: the audio buffer
2420 * @freq: the frequency of the sin wave
2421 * @phase: the phase of the sin wave
2422 * @volume: the volume of the sin wave
2423 * @samplerate: the samplerate
2424 * @offset: start frame
2425 * @n_frames: generate n frames
2426 *
2427 * Generate sinus wave.
2428 *
2429 * Since: 3.0.0
2430 */
2431 void
ags_synth_util_sin_s64(gint64 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)2432 ags_synth_util_sin_s64(gint64 *buffer,
2433 gdouble freq, gdouble phase, gdouble volume,
2434 guint samplerate,
2435 guint offset, guint n_frames)
2436 {
2437 static const gdouble scale = 9223372036854775807.0;
2438 guint i;
2439
2440 if(buffer == NULL){
2441 return;
2442 }
2443
2444 for(i = offset; i < offset + n_frames; i++){
2445 buffer[i] = (gint64) (0xffffffffffff & ((gint64) buffer[i] + (gint64) (sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) * scale * volume)));
2446 }
2447 }
2448
2449 /**
2450 * ags_synth_util_sin_float:
2451 * @buffer: the audio buffer
2452 * @freq: the frequency of the sin wave
2453 * @phase: the phase of the sin wave
2454 * @volume: the volume of the sin wave
2455 * @samplerate: the samplerate
2456 * @offset: start frame
2457 * @n_frames: generate n frames
2458 *
2459 * Generate sinus wave.
2460 *
2461 * Since: 3.0.0
2462 */
2463 void
ags_synth_util_sin_float(gfloat * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)2464 ags_synth_util_sin_float(gfloat *buffer,
2465 gdouble freq, gdouble phase, gdouble volume,
2466 guint samplerate,
2467 guint offset, guint n_frames)
2468 {
2469 guint i;
2470
2471 if(buffer == NULL){
2472 return;
2473 }
2474
2475 for(i = offset; i < offset + n_frames; i++){
2476 buffer[i] = (float) ((gdouble) buffer[i] + (gdouble) (sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) * volume));
2477 }
2478 }
2479
2480 /**
2481 * ags_synth_util_sin_double:
2482 * @buffer: the audio buffer
2483 * @freq: the frequency of the sin wave
2484 * @phase: the phase of the sin wave
2485 * @volume: the volume of the sin wave
2486 * @samplerate: the samplerate
2487 * @offset: start frame
2488 * @n_frames: generate n frames
2489 *
2490 * Generate sinus wave.
2491 *
2492 * Since: 3.0.0
2493 */
2494 void
ags_synth_util_sin_double(gdouble * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)2495 ags_synth_util_sin_double(gdouble *buffer,
2496 gdouble freq, gdouble phase, gdouble volume,
2497 guint samplerate,
2498 guint offset, guint n_frames)
2499 {
2500 guint i;
2501
2502 if(buffer == NULL){
2503 return;
2504 }
2505
2506 for(i = offset; i < offset + n_frames; i++){
2507 buffer[i] = (double) ((gdouble) buffer[i] + (gdouble) (sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) * volume));
2508 }
2509 }
2510
2511 /**
2512 * ags_synth_util_sin_complex:
2513 * @buffer: the audio buffer
2514 * @freq: the frequency of the sin wave
2515 * @phase: the phase of the sin wave
2516 * @volume: the volume of the sin wave
2517 * @samplerate: the samplerate
2518 * @offset: start frame
2519 * @n_frames: generate n frames
2520 *
2521 * Generate sinus wave.
2522 *
2523 * Since: 3.0.0
2524 */
2525 void
ags_synth_util_sin_complex(AgsComplex * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)2526 ags_synth_util_sin_complex(AgsComplex *buffer,
2527 gdouble freq, gdouble phase, gdouble volume,
2528 guint samplerate,
2529 guint offset, guint n_frames)
2530 {
2531 AgsComplex *c_ptr;
2532 AgsComplex **c_ptr_ptr;
2533
2534 double y;
2535
2536 guint i_stop;
2537 guint i;
2538
2539 if(buffer == NULL){
2540 return;
2541 }
2542
2543 c_ptr = buffer;
2544 c_ptr_ptr = &c_ptr;
2545
2546 i_stop = offset + n_frames;
2547
2548 for(i = offset; i < i_stop; i++, c_ptr++){
2549 y = (gdouble) (sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) * volume);
2550
2551 AGS_AUDIO_BUFFER_UTIL_DOUBLE_TO_COMPLEX(y, c_ptr_ptr);
2552 }
2553 }
2554
2555 /**
2556 * ags_synth_util_sin:
2557 * @buffer: the audio buffer
2558 * @freq: the frequency of the sin wave
2559 * @phase: the phase of the sin wave
2560 * @volume: the volume of the sin wave
2561 * @samplerate: the samplerate
2562 * @audio_buffer_util_format: the audio data format
2563 * @offset: start frame
2564 * @n_frames: generate n frames
2565 *
2566 * Generate sin wave.
2567 *
2568 * Since: 3.0.0
2569 */
2570 void
ags_synth_util_sin(void * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint audio_buffer_util_format,guint offset,guint n_frames)2571 ags_synth_util_sin(void *buffer,
2572 gdouble freq, gdouble phase, gdouble volume,
2573 guint samplerate, guint audio_buffer_util_format,
2574 guint offset, guint n_frames)
2575 {
2576 if(buffer == NULL){
2577 return;
2578 }
2579
2580 switch(audio_buffer_util_format){
2581 case AGS_AUDIO_BUFFER_UTIL_S8:
2582 {
2583 ags_synth_util_sin_s8((gint8 *) buffer,
2584 freq, phase, volume,
2585 samplerate,
2586 offset, n_frames);
2587 }
2588 break;
2589 case AGS_AUDIO_BUFFER_UTIL_S16:
2590 {
2591 ags_synth_util_sin_s16((gint16 *) buffer,
2592 freq, phase, volume,
2593 samplerate,
2594 offset, n_frames);
2595 }
2596 break;
2597 case AGS_AUDIO_BUFFER_UTIL_S24:
2598 {
2599 ags_synth_util_sin_s24((gint32 *) buffer,
2600 freq, phase, volume,
2601 samplerate,
2602 offset, n_frames);
2603 }
2604 break;
2605 case AGS_AUDIO_BUFFER_UTIL_S32:
2606 {
2607 ags_synth_util_sin_s32((gint32 *) buffer,
2608 freq, phase, volume,
2609 samplerate,
2610 offset, n_frames);
2611 }
2612 break;
2613 case AGS_AUDIO_BUFFER_UTIL_S64:
2614 {
2615 ags_synth_util_sin_s64((gint64 *) buffer,
2616 freq, phase, volume,
2617 samplerate,
2618 offset, n_frames);
2619 }
2620 break;
2621 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
2622 {
2623 ags_synth_util_sin_float((gfloat *) buffer,
2624 freq, phase, volume,
2625 samplerate,
2626 offset, n_frames);
2627 }
2628 break;
2629 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
2630 {
2631 ags_synth_util_sin_double((gdouble *) buffer,
2632 freq, phase, volume,
2633 samplerate,
2634 offset, n_frames);
2635 }
2636 break;
2637 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
2638 {
2639 ags_synth_util_sin_complex((AgsComplex *) buffer,
2640 freq, phase, volume,
2641 samplerate,
2642 offset, n_frames);
2643 }
2644 break;
2645 default:
2646 {
2647 g_warning("ags_synth_util_sin() - unsupported format");
2648 }
2649 }
2650 }
2651
2652 /**
2653 * ags_synth_util_compute_sawtooth_s8:
2654 * @synth_util: the #AgsSynthUtil-struct
2655 *
2656 * Compute sawtooth synth of signed 8 bit data.
2657 *
2658 * Since: 3.9.3
2659 */
2660 void
ags_synth_util_compute_sawtooth_s8(AgsSynthUtil * synth_util)2661 ags_synth_util_compute_sawtooth_s8(AgsSynthUtil *synth_util)
2662 {
2663 gint8 *source, *tmp_source;
2664
2665 gdouble ratio;
2666 gdouble volume;
2667 guint i, i_stop;
2668
2669 static const gdouble scale = 127.0;
2670
2671 if(synth_util == NULL ||
2672 synth_util->source == NULL){
2673 return;
2674 }
2675
2676 source = synth_util->source;
2677
2678 ratio = synth_util->frequency / synth_util->samplerate;
2679
2680 volume = scale * synth_util->volume;
2681
2682 i = 0;
2683
2684 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
2685 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2686
2687 if(synth_util->offset + i_stop > synth_util->frame_count){
2688 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2689 }
2690
2691 for(; i < i_stop;){
2692 ags_v8double v_buffer, v_sawtooth;
2693
2694 tmp_source = source;
2695
2696 v_buffer = (ags_v8double) {
2697 (gdouble) *(tmp_source),
2698 (gdouble) *(tmp_source += synth_util->source_stride),
2699 (gdouble) *(tmp_source += synth_util->source_stride),
2700 (gdouble) *(tmp_source += synth_util->source_stride),
2701 (gdouble) *(tmp_source += synth_util->source_stride),
2702 (gdouble) *(tmp_source += synth_util->source_stride),
2703 (gdouble) *(tmp_source += synth_util->source_stride),
2704 (gdouble) *(tmp_source += synth_util->source_stride)
2705 };
2706
2707 v_sawtooth = (ags_v8double) {
2708 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2709 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2710 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2711 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2712 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2713 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2714 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2715 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2716 };
2717
2718 i++;
2719
2720 v_sawtooth *= volume;
2721
2722 v_buffer += v_sawtooth;
2723
2724 *(source) = (gint8) v_buffer[0];
2725 *(source += synth_util->source_stride) = (gint8) v_buffer[1];
2726 *(source += synth_util->source_stride) = (gint8) v_buffer[2];
2727 *(source += synth_util->source_stride) = (gint8) v_buffer[3];
2728 *(source += synth_util->source_stride) = (gint8) v_buffer[4];
2729 *(source += synth_util->source_stride) = (gint8) v_buffer[5];
2730 *(source += synth_util->source_stride) = (gint8) v_buffer[6];
2731 *(source += synth_util->source_stride) = (gint8) v_buffer[7];
2732
2733 source += synth_util->source_stride;
2734 }
2735 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
2736 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2737
2738 if(synth_util->offset + i_stop > synth_util->frame_count){
2739 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2740 }
2741
2742 for(; i < i_stop;){
2743 double ret_v_buffer[8], tmp_ret_v_buffer[8];
2744
2745 tmp_source = source;
2746
2747 double v_buffer[] = {
2748 (double) *(tmp_source),
2749 (double) *(tmp_source += synth_util->source_stride),
2750 (double) *(tmp_source += synth_util->source_stride),
2751 (double) *(tmp_source += synth_util->source_stride),
2752 (double) *(tmp_source += synth_util->source_stride),
2753 (double) *(tmp_source += synth_util->source_stride),
2754 (double) *(tmp_source += synth_util->source_stride),
2755 (double) *(tmp_source += synth_util->source_stride)};
2756 double v_sawtooth[] = {
2757 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2758 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2759 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2760 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2761 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2762 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2763 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2764 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0)};
2765
2766 double v_volume[] = {(double) volume};
2767
2768 i++;
2769
2770 vDSP_vmulD(v_sawtooth, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
2771 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
2772
2773 *(source) = (gint8) ret_v_buffer[0];
2774 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[1];
2775 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[2];
2776 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[3];
2777 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[4];
2778 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[5];
2779 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[6];
2780 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[7];
2781
2782 source += synth_util->source_stride;
2783 }
2784 #else
2785 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2786
2787 if(synth_util->offset + i_stop > synth_util->frame_count){
2788 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2789 }
2790
2791 for(; i < i_stop;){
2792 tmp_source = source;
2793
2794 (*source) = (gint8) ((gint16) (tmp_source)[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2795 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2796 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2797 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2798 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2799 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2800 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2801 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2802
2803 source += synth_util->source_stride;
2804 i++;
2805 }
2806 #endif
2807
2808 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
2809 source[0] = (gint8) ((gint16) source[0] + (gint16) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2810
2811 source += synth_util->source_stride;
2812 i++;
2813 }
2814 }
2815
2816 /**
2817 * ags_synth_util_compute_sawtooth_s16:
2818 * @synth_util: the #AgsSynthUtil-struct
2819 *
2820 * Compute sawtooth synth of signed 16 bit data.
2821 *
2822 * Since: 3.9.3
2823 */
2824 void
ags_synth_util_compute_sawtooth_s16(AgsSynthUtil * synth_util)2825 ags_synth_util_compute_sawtooth_s16(AgsSynthUtil *synth_util)
2826 {
2827 gint16 *source, *tmp_source;
2828
2829 gdouble ratio;
2830 gdouble volume;
2831 guint i, i_stop;
2832
2833 static const gdouble scale = 32767.0;
2834
2835 if(synth_util == NULL ||
2836 synth_util->source == NULL){
2837 return;
2838 }
2839
2840 source = synth_util->source;
2841
2842 ratio = synth_util->frequency / synth_util->samplerate;
2843
2844 volume = scale * synth_util->volume;
2845
2846 i = 0;
2847
2848 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
2849 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2850
2851 if(synth_util->offset + i_stop > synth_util->frame_count){
2852 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2853 }
2854
2855 for(; i < i_stop;){
2856 ags_v8double v_buffer, v_sawtooth;
2857
2858 tmp_source = source;
2859
2860 v_buffer = (ags_v8double) {
2861 (gdouble) *(tmp_source),
2862 (gdouble) *(tmp_source += synth_util->source_stride),
2863 (gdouble) *(tmp_source += synth_util->source_stride),
2864 (gdouble) *(tmp_source += synth_util->source_stride),
2865 (gdouble) *(tmp_source += synth_util->source_stride),
2866 (gdouble) *(tmp_source += synth_util->source_stride),
2867 (gdouble) *(tmp_source += synth_util->source_stride),
2868 (gdouble) *(tmp_source += synth_util->source_stride)
2869 };
2870
2871 v_sawtooth = (ags_v8double) {
2872 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2873 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2874 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2875 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2876 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2877 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2878 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2879 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2880 };
2881
2882 i++;
2883
2884 v_sawtooth *= volume;
2885
2886 v_buffer += v_sawtooth;
2887
2888 *(source) = (gint16) v_buffer[0];
2889 *(source += synth_util->source_stride) = (gint16) v_buffer[1];
2890 *(source += synth_util->source_stride) = (gint16) v_buffer[2];
2891 *(source += synth_util->source_stride) = (gint16) v_buffer[3];
2892 *(source += synth_util->source_stride) = (gint16) v_buffer[4];
2893 *(source += synth_util->source_stride) = (gint16) v_buffer[5];
2894 *(source += synth_util->source_stride) = (gint16) v_buffer[6];
2895 *(source += synth_util->source_stride) = (gint16) v_buffer[7];
2896
2897 source += synth_util->source_stride;
2898 }
2899 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
2900 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2901
2902 if(synth_util->offset + i_stop > synth_util->frame_count){
2903 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2904 }
2905
2906 for(; i < i_stop;){
2907 double ret_v_buffer[8], tmp_ret_v_buffer[8];
2908
2909 tmp_source = source;
2910
2911 double v_buffer[] = {
2912 (double) *(tmp_source),
2913 (double) *(tmp_source += synth_util->source_stride),
2914 (double) *(tmp_source += synth_util->source_stride),
2915 (double) *(tmp_source += synth_util->source_stride),
2916 (double) *(tmp_source += synth_util->source_stride),
2917 (double) *(tmp_source += synth_util->source_stride),
2918 (double) *(tmp_source += synth_util->source_stride),
2919 (double) *(tmp_source += synth_util->source_stride)};
2920 double v_sawtooth[] = {
2921 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2922 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2923 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2924 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2925 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2926 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2927 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
2928 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0)};
2929
2930 double v_volume[] = {(double) volume};
2931
2932 i++;
2933
2934 vDSP_vmulD(v_sawtooth, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
2935 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
2936
2937 *(source) = (gint16) ret_v_buffer[0];
2938 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[1];
2939 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[2];
2940 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[3];
2941 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[4];
2942 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[5];
2943 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[6];
2944 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[7];
2945
2946 source += synth_util->source_stride;
2947 }
2948 #else
2949 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
2950
2951 if(synth_util->offset + i_stop > synth_util->frame_count){
2952 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
2953 }
2954
2955 for(; i < i_stop;){
2956 tmp_source = source;
2957
2958 (*source) = (gint16) ((gint32) (tmp_source)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2959 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2960 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2961 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2962 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2963 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2964 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2965 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2966
2967 source += synth_util->source_stride;
2968 i++;
2969 }
2970 #endif
2971
2972 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
2973 source[0] = (gint16) ((gint32) source[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
2974
2975 source += synth_util->source_stride;
2976 i++;
2977 }
2978 }
2979
2980 /**
2981 * ags_synth_util_compute_sawtooth_s24:
2982 * @synth_util: the #AgsSynthUtil-struct
2983 *
2984 * Compute sawtooth synth of signed 24 bit data.
2985 *
2986 * Since: 3.9.3
2987 */
2988 void
ags_synth_util_compute_sawtooth_s24(AgsSynthUtil * synth_util)2989 ags_synth_util_compute_sawtooth_s24(AgsSynthUtil *synth_util)
2990 {
2991 gint32 *source, *tmp_source;
2992
2993 gdouble ratio;
2994 gdouble volume;
2995 guint i, i_stop;
2996
2997 static const gdouble scale = 8388607.0;
2998
2999 if(synth_util == NULL ||
3000 synth_util->source == NULL){
3001 return;
3002 }
3003
3004 source = synth_util->source;
3005
3006 ratio = synth_util->frequency / synth_util->samplerate;
3007
3008 volume = scale * synth_util->volume;
3009
3010 i = 0;
3011
3012 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
3013 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3014
3015 if(synth_util->offset + i_stop > synth_util->frame_count){
3016 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3017 }
3018
3019 for(; i < i_stop;){
3020 ags_v8double v_buffer, v_sawtooth;
3021
3022 tmp_source = source;
3023
3024 v_buffer = (ags_v8double) {
3025 (gdouble) *(tmp_source),
3026 (gdouble) *(tmp_source += synth_util->source_stride),
3027 (gdouble) *(tmp_source += synth_util->source_stride),
3028 (gdouble) *(tmp_source += synth_util->source_stride),
3029 (gdouble) *(tmp_source += synth_util->source_stride),
3030 (gdouble) *(tmp_source += synth_util->source_stride),
3031 (gdouble) *(tmp_source += synth_util->source_stride),
3032 (gdouble) *(tmp_source += synth_util->source_stride)
3033 };
3034
3035 v_sawtooth = (ags_v8double) {
3036 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3037 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3038 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3039 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3040 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3041 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3042 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3043 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3044 };
3045
3046 i++;
3047
3048 v_sawtooth *= volume;
3049
3050 v_buffer += v_sawtooth;
3051
3052 *(source) = (gint32) v_buffer[0];
3053 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
3054 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
3055 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
3056 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
3057 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
3058 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
3059 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
3060
3061 source += synth_util->source_stride;
3062 }
3063 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
3064 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3065
3066 if(synth_util->offset + i_stop > synth_util->frame_count){
3067 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3068 }
3069
3070 for(; i < i_stop;){
3071 double ret_v_buffer[8], tmp_ret_v_buffer[8];
3072
3073 tmp_source = source;
3074
3075 double v_buffer[] = {
3076 (double) *(tmp_source),
3077 (double) *(tmp_source += synth_util->source_stride),
3078 (double) *(tmp_source += synth_util->source_stride),
3079 (double) *(tmp_source += synth_util->source_stride),
3080 (double) *(tmp_source += synth_util->source_stride),
3081 (double) *(tmp_source += synth_util->source_stride),
3082 (double) *(tmp_source += synth_util->source_stride),
3083 (double) *(tmp_source += synth_util->source_stride)};
3084 double v_sawtooth[] = {
3085 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3086 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3087 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3088 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3089 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3090 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3091 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3092 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0)};
3093
3094 double v_volume[] = {(double) volume};
3095
3096 i++;
3097
3098 vDSP_vmulD(v_sawtooth, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
3099 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
3100
3101 *(source) = (gint32) ret_v_buffer[0];
3102 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
3103 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
3104 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
3105 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
3106 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
3107 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
3108 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
3109
3110 source += synth_util->source_stride;
3111 }
3112 #else
3113 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3114
3115 if(synth_util->offset + i_stop > synth_util->frame_count){
3116 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3117 }
3118
3119 for(; i < i_stop;){
3120 tmp_source = source;
3121
3122 (*source) = (gint32) ((gint32) (tmp_source)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3123 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3124 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3125 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3126 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3127 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3128 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3129 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3130
3131 source += synth_util->source_stride;
3132 i++;
3133 }
3134 #endif
3135
3136 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
3137 source[0] = (gint32) ((gint32) source[0] + (gint32) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3138
3139 source += synth_util->source_stride;
3140 i++;
3141 }
3142 }
3143
3144 /**
3145 * ags_synth_util_compute_sawtooth_s32:
3146 * @synth_util: the #AgsSynthUtil-struct
3147 *
3148 * Compute sawtooth synth of signed 32 bit data.
3149 *
3150 * Since: 3.9.3
3151 */
3152 void
ags_synth_util_compute_sawtooth_s32(AgsSynthUtil * synth_util)3153 ags_synth_util_compute_sawtooth_s32(AgsSynthUtil *synth_util)
3154 {
3155 gint32 *source, *tmp_source;
3156
3157 gdouble ratio;
3158 gdouble volume;
3159 guint i, i_stop;
3160
3161 static const gdouble scale = 214748363.0;
3162
3163 if(synth_util == NULL ||
3164 synth_util->source == NULL){
3165 return;
3166 }
3167
3168 source = synth_util->source;
3169
3170 ratio = synth_util->frequency / synth_util->samplerate;
3171
3172 volume = scale * synth_util->volume;
3173
3174 i = 0;
3175
3176 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
3177 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3178
3179 if(synth_util->offset + i_stop > synth_util->frame_count){
3180 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3181 }
3182
3183 for(; i < i_stop;){
3184 ags_v8double v_buffer, v_sawtooth;
3185
3186 tmp_source = source;
3187
3188 v_buffer = (ags_v8double) {
3189 (gdouble) *(tmp_source),
3190 (gdouble) *(tmp_source += synth_util->source_stride),
3191 (gdouble) *(tmp_source += synth_util->source_stride),
3192 (gdouble) *(tmp_source += synth_util->source_stride),
3193 (gdouble) *(tmp_source += synth_util->source_stride),
3194 (gdouble) *(tmp_source += synth_util->source_stride),
3195 (gdouble) *(tmp_source += synth_util->source_stride),
3196 (gdouble) *(tmp_source += synth_util->source_stride)
3197 };
3198
3199 v_sawtooth = (ags_v8double) {
3200 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3201 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3202 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3203 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3204 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3205 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3206 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3207 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3208 };
3209
3210 i++;
3211
3212 v_sawtooth *= volume;
3213
3214 v_buffer += v_sawtooth;
3215
3216 *(source) = (gint32) v_buffer[0];
3217 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
3218 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
3219 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
3220 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
3221 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
3222 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
3223 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
3224
3225 source += synth_util->source_stride;
3226 }
3227 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
3228 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3229
3230 if(synth_util->offset + i_stop > synth_util->frame_count){
3231 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3232 }
3233
3234 for(; i < i_stop;){
3235 double ret_v_buffer[8], tmp_ret_v_buffer[8];
3236
3237 tmp_source = source;
3238
3239 double v_buffer[] = {
3240 (double) *(tmp_source),
3241 (double) *(tmp_source += synth_util->source_stride),
3242 (double) *(tmp_source += synth_util->source_stride),
3243 (double) *(tmp_source += synth_util->source_stride),
3244 (double) *(tmp_source += synth_util->source_stride),
3245 (double) *(tmp_source += synth_util->source_stride),
3246 (double) *(tmp_source += synth_util->source_stride),
3247 (double) *(tmp_source += synth_util->source_stride)};
3248 double v_sawtooth[] = {
3249 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3250 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3251 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3252 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3253 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3254 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3255 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3256 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0)};
3257
3258 double v_volume[] = {(double) volume};
3259
3260 i++;
3261
3262 vDSP_vmulD(v_sawtooth, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
3263 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
3264
3265 *(source) = (gint32) ret_v_buffer[0];
3266 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
3267 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
3268 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
3269 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
3270 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
3271 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
3272 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
3273
3274 source += synth_util->source_stride;
3275 }
3276 #else
3277 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3278
3279 if(synth_util->offset + i_stop > synth_util->frame_count){
3280 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3281 }
3282
3283 for(; i < i_stop;){
3284 tmp_source = source;
3285
3286 (*source) = (gint32) ((gint64) (tmp_source)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3287 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3288 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3289 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3290 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3291 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3292 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3293 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3294
3295 source += synth_util->source_stride;
3296 i++;
3297 }
3298 #endif
3299
3300 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
3301 source[0] = (gint32) ((gint64) source[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3302
3303 source += synth_util->source_stride;
3304 i++;
3305 }
3306 }
3307
3308 /**
3309 * ags_synth_util_compute_sawtooth_s64:
3310 * @synth_util: the #AgsSynthUtil-struct
3311 *
3312 * Compute sawtooth synth of signed 64 bit data.
3313 *
3314 * Since: 3.9.3
3315 */
3316 void
ags_synth_util_compute_sawtooth_s64(AgsSynthUtil * synth_util)3317 ags_synth_util_compute_sawtooth_s64(AgsSynthUtil *synth_util)
3318 {
3319 gint64 *source, *tmp_source;
3320
3321 gdouble ratio;
3322 gdouble volume;
3323 guint i, i_stop;
3324
3325 static const gdouble scale = 9223372036854775807.0;
3326
3327 if(synth_util == NULL ||
3328 synth_util->source == NULL){
3329 return;
3330 }
3331
3332 source = synth_util->source;
3333
3334 ratio = synth_util->frequency / synth_util->samplerate;
3335
3336 volume = scale * synth_util->volume;
3337
3338 i = 0;
3339
3340 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
3341 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3342
3343 if(synth_util->offset + i_stop > synth_util->frame_count){
3344 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3345 }
3346
3347 for(; i < i_stop;){
3348 ags_v8double v_buffer, v_sawtooth;
3349
3350 tmp_source = source;
3351
3352 v_buffer = (ags_v8double) {
3353 (gdouble) *(tmp_source),
3354 (gdouble) *(tmp_source += synth_util->source_stride),
3355 (gdouble) *(tmp_source += synth_util->source_stride),
3356 (gdouble) *(tmp_source += synth_util->source_stride),
3357 (gdouble) *(tmp_source += synth_util->source_stride),
3358 (gdouble) *(tmp_source += synth_util->source_stride),
3359 (gdouble) *(tmp_source += synth_util->source_stride),
3360 (gdouble) *(tmp_source += synth_util->source_stride)
3361 };
3362
3363 v_sawtooth = (ags_v8double) {
3364 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3365 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3366 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3367 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3368 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3369 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3370 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3371 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3372 };
3373
3374 i++;
3375
3376 v_sawtooth *= volume;
3377
3378 v_buffer += v_sawtooth;
3379
3380 *(source) = (gint64) v_buffer[0];
3381 *(source += synth_util->source_stride) = (gint64) v_buffer[1];
3382 *(source += synth_util->source_stride) = (gint64) v_buffer[2];
3383 *(source += synth_util->source_stride) = (gint64) v_buffer[3];
3384 *(source += synth_util->source_stride) = (gint64) v_buffer[4];
3385 *(source += synth_util->source_stride) = (gint64) v_buffer[5];
3386 *(source += synth_util->source_stride) = (gint64) v_buffer[6];
3387 *(source += synth_util->source_stride) = (gint64) v_buffer[7];
3388
3389 source += synth_util->source_stride;
3390 }
3391 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
3392 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3393
3394 if(synth_util->offset + i_stop > synth_util->frame_count){
3395 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3396 }
3397
3398 for(; i < i_stop;){
3399 double ret_v_buffer[8], tmp_ret_v_buffer[8];
3400
3401 tmp_source = source;
3402
3403 double v_buffer[] = {
3404 (double) *(tmp_source),
3405 (double) *(tmp_source += synth_util->source_stride),
3406 (double) *(tmp_source += synth_util->source_stride),
3407 (double) *(tmp_source += synth_util->source_stride),
3408 (double) *(tmp_source += synth_util->source_stride),
3409 (double) *(tmp_source += synth_util->source_stride),
3410 (double) *(tmp_source += synth_util->source_stride),
3411 (double) *(tmp_source += synth_util->source_stride)};
3412 double v_sawtooth[] = {
3413 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3414 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3415 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3416 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3417 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3418 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3419 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3420 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0)};
3421
3422 double v_volume[] = {(double) volume};
3423
3424 i++;
3425
3426 vDSP_vmulD(v_sawtooth, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
3427 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
3428
3429 *(source) = (gint64) ret_v_buffer[0];
3430 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[1];
3431 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[2];
3432 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[3];
3433 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[4];
3434 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[5];
3435 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[6];
3436 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[7];
3437
3438 source += synth_util->source_stride;
3439 }
3440 #else
3441 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3442
3443 if(synth_util->offset + i_stop > synth_util->frame_count){
3444 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3445 }
3446
3447 for(; i < i_stop;){
3448 tmp_source = source;
3449
3450 (*source) = (gint64) ((gint64) (tmp_source)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3451 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3452 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3453 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3454 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3455 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3456 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3457 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3458
3459 source += synth_util->source_stride;
3460 i++;
3461 }
3462 #endif
3463
3464 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
3465 source[0] = (gint64) ((gint64) source[0] + (gint64) ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3466
3467 source += synth_util->source_stride;
3468 i++;
3469 }
3470 }
3471
3472 /**
3473 * ags_synth_util_compute_sawtooth_float:
3474 * @synth_util: the #AgsSynthUtil-struct
3475 *
3476 * Compute sawtooth synth of floating point data.
3477 *
3478 * Since: 3.9.3
3479 */
3480 void
ags_synth_util_compute_sawtooth_float(AgsSynthUtil * synth_util)3481 ags_synth_util_compute_sawtooth_float(AgsSynthUtil *synth_util)
3482 {
3483 gfloat *source, *tmp_source;
3484
3485 gdouble ratio;
3486 gdouble volume;
3487 guint i, i_stop;
3488
3489 if(synth_util == NULL ||
3490 synth_util->source == NULL){
3491 return;
3492 }
3493
3494 source = synth_util->source;
3495
3496 ratio = synth_util->frequency / synth_util->samplerate;
3497
3498 volume = synth_util->volume;
3499
3500 i = 0;
3501
3502 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
3503 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3504
3505 if(synth_util->offset + i_stop > synth_util->frame_count){
3506 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3507 }
3508
3509 for(; i < i_stop;){
3510 ags_v8double v_buffer, v_sawtooth;
3511
3512 tmp_source = source;
3513
3514 v_buffer = (ags_v8double) {
3515 (gdouble) *(tmp_source),
3516 (gdouble) *(tmp_source += synth_util->source_stride),
3517 (gdouble) *(tmp_source += synth_util->source_stride),
3518 (gdouble) *(tmp_source += synth_util->source_stride),
3519 (gdouble) *(tmp_source += synth_util->source_stride),
3520 (gdouble) *(tmp_source += synth_util->source_stride),
3521 (gdouble) *(tmp_source += synth_util->source_stride),
3522 (gdouble) *(tmp_source += synth_util->source_stride)
3523 };
3524
3525 v_sawtooth = (ags_v8double) {
3526 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3527 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3528 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3529 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3530 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3531 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3532 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3533 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3534 };
3535
3536 i++;
3537
3538 v_sawtooth *= volume;
3539
3540 v_buffer += v_sawtooth;
3541
3542 *(source) = (gfloat) v_buffer[0];
3543 *(source += synth_util->source_stride) = (gfloat) v_buffer[1];
3544 *(source += synth_util->source_stride) = (gfloat) v_buffer[2];
3545 *(source += synth_util->source_stride) = (gfloat) v_buffer[3];
3546 *(source += synth_util->source_stride) = (gfloat) v_buffer[4];
3547 *(source += synth_util->source_stride) = (gfloat) v_buffer[5];
3548 *(source += synth_util->source_stride) = (gfloat) v_buffer[6];
3549 *(source += synth_util->source_stride) = (gfloat) v_buffer[7];
3550
3551 source += synth_util->source_stride;
3552 }
3553 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
3554 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3555
3556 if(synth_util->offset + i_stop > synth_util->frame_count){
3557 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3558 }
3559
3560 for(; i < i_stop;){
3561 double ret_v_buffer[8], tmp_ret_v_buffer[8];
3562
3563 tmp_source = source;
3564
3565 double v_buffer[] = {
3566 (double) *(tmp_source),
3567 (double) *(tmp_source += synth_util->source_stride),
3568 (double) *(tmp_source += synth_util->source_stride),
3569 (double) *(tmp_source += synth_util->source_stride),
3570 (double) *(tmp_source += synth_util->source_stride),
3571 (double) *(tmp_source += synth_util->source_stride),
3572 (double) *(tmp_source += synth_util->source_stride),
3573 (double) *(tmp_source += synth_util->source_stride)};
3574 double v_sawtooth[] = {
3575 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3576 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3577 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3578 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3579 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3580 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3581 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3582 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0)};
3583
3584 double v_volume[] = {(double) volume};
3585
3586 i++;
3587
3588 vDSP_vmulD(v_sawtooth, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
3589 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
3590
3591 *(source) = (gfloat) ret_v_buffer[0];
3592 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[1];
3593 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[2];
3594 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[3];
3595 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[4];
3596 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[5];
3597 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[6];
3598 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[7];
3599
3600 source += synth_util->source_stride;
3601 }
3602 #else
3603 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3604
3605 if(synth_util->offset + i_stop > synth_util->frame_count){
3606 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3607 }
3608
3609 for(; i < i_stop;){
3610 tmp_source = source;
3611
3612 (*source) = (gfloat) ((tmp_source)[0] + ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3613 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3614 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3615 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3616 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3617 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3618 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3619 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3620
3621 source += synth_util->source_stride;
3622 i++;
3623 }
3624 #endif
3625
3626 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
3627 source[0] = (gfloat) (source[0] + ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3628
3629 source += synth_util->source_stride;
3630 i++;
3631 }
3632 }
3633
3634 /**
3635 * ags_synth_util_compute_sawtooth_double:
3636 * @synth_util: the #AgsSynthUtil-struct
3637 *
3638 * Compute sawtooth synth of double precision floating point data.
3639 *
3640 * Since: 3.9.3
3641 */
3642 void
ags_synth_util_compute_sawtooth_double(AgsSynthUtil * synth_util)3643 ags_synth_util_compute_sawtooth_double(AgsSynthUtil *synth_util)
3644 {
3645 gdouble *source, *tmp_source;
3646
3647 gdouble ratio;
3648 gdouble volume;
3649 guint i, i_stop;
3650
3651 if(synth_util == NULL ||
3652 synth_util->source == NULL){
3653 return;
3654 }
3655
3656 source = synth_util->source;
3657
3658 ratio = synth_util->frequency / synth_util->samplerate;
3659
3660 volume = synth_util->volume;
3661
3662 i = 0;
3663
3664 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
3665 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3666
3667 if(synth_util->offset + i_stop > synth_util->frame_count){
3668 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3669 }
3670
3671 for(; i < i_stop;){
3672 ags_v8double v_buffer, v_sawtooth;
3673
3674 tmp_source = source;
3675
3676 v_buffer = (ags_v8double) {
3677 (gdouble) *(tmp_source),
3678 (gdouble) *(tmp_source += synth_util->source_stride),
3679 (gdouble) *(tmp_source += synth_util->source_stride),
3680 (gdouble) *(tmp_source += synth_util->source_stride),
3681 (gdouble) *(tmp_source += synth_util->source_stride),
3682 (gdouble) *(tmp_source += synth_util->source_stride),
3683 (gdouble) *(tmp_source += synth_util->source_stride),
3684 (gdouble) *(tmp_source += synth_util->source_stride)
3685 };
3686
3687 v_sawtooth = (ags_v8double) {
3688 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3689 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3690 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3691 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3692 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3693 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3694 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3695 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3696 };
3697
3698 i++;
3699
3700 v_sawtooth *= volume;
3701
3702 v_buffer += v_sawtooth;
3703
3704 *(source) = (gdouble) v_buffer[0];
3705 *(source += synth_util->source_stride) = (gdouble) v_buffer[1];
3706 *(source += synth_util->source_stride) = (gdouble) v_buffer[2];
3707 *(source += synth_util->source_stride) = (gdouble) v_buffer[3];
3708 *(source += synth_util->source_stride) = (gdouble) v_buffer[4];
3709 *(source += synth_util->source_stride) = (gdouble) v_buffer[5];
3710 *(source += synth_util->source_stride) = (gdouble) v_buffer[6];
3711 *(source += synth_util->source_stride) = (gdouble) v_buffer[7];
3712
3713 source += synth_util->source_stride;
3714 }
3715 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
3716 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3717
3718 if(synth_util->offset + i_stop > synth_util->frame_count){
3719 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3720 }
3721
3722 for(; i < i_stop;){
3723 double ret_v_buffer[8], tmp_ret_v_buffer[8];
3724
3725 tmp_source = source;
3726
3727 double v_buffer[] = {
3728 (double) *(tmp_source),
3729 (double) *(tmp_source += synth_util->source_stride),
3730 (double) *(tmp_source += synth_util->source_stride),
3731 (double) *(tmp_source += synth_util->source_stride),
3732 (double) *(tmp_source += synth_util->source_stride),
3733 (double) *(tmp_source += synth_util->source_stride),
3734 (double) *(tmp_source += synth_util->source_stride),
3735 (double) *(tmp_source += synth_util->source_stride)};
3736 double v_sawtooth[] = {
3737 ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3738 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3739 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3740 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3741 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3742 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3743 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0),
3744 ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0)};
3745
3746 double v_volume[] = {(double) volume};
3747
3748 i++;
3749
3750 vDSP_vmulD(v_sawtooth, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
3751 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
3752
3753 *(source) = (gdouble) ret_v_buffer[0];
3754 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[1];
3755 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[2];
3756 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[3];
3757 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[4];
3758 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[5];
3759 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[6];
3760 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[7];
3761
3762 source += synth_util->source_stride;
3763 }
3764 #else
3765 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
3766
3767 if(synth_util->offset + i_stop > synth_util->frame_count){
3768 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
3769 }
3770
3771 for(; i < i_stop;){
3772 tmp_source = source;
3773
3774 (*source) = (gdouble) ((tmp_source)[0] + ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3775 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3776 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3777 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3778 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3779 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3780 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3781 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((fmod(((gdouble) (synth_util->offset + (i++)) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3782
3783 source += synth_util->source_stride;
3784 i++;
3785 }
3786 #endif
3787
3788 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
3789 source[0] = (gdouble) (source[0] + ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume);
3790
3791 source += synth_util->source_stride;
3792 i++;
3793 }
3794 }
3795
3796 /**
3797 * ags_synth_util_compute_sawtooth_complex:
3798 * @synth_util: the #AgsSynthUtil-struct
3799 *
3800 * Compute sawtooth synth of complex data.
3801 *
3802 * Since: 3.9.3
3803 */
3804 void
ags_synth_util_compute_sawtooth_complex(AgsSynthUtil * synth_util)3805 ags_synth_util_compute_sawtooth_complex(AgsSynthUtil *synth_util)
3806 {
3807 AgsComplex *source;
3808
3809 gdouble ratio;
3810 gdouble volume;
3811 guint i;
3812
3813 if(synth_util == NULL ||
3814 synth_util->source == NULL){
3815 return;
3816 }
3817
3818 source = synth_util->source;
3819
3820 ratio = synth_util->frequency / synth_util->samplerate;
3821
3822 volume = synth_util->volume;
3823
3824 i = 0;
3825
3826 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
3827 ags_complex_set(source,
3828 (ags_complex_get(source) + ((fmod(((gdouble) (synth_util->offset + i) + synth_util->phase), ratio) * 2.0 * ratio) - 1.0) * volume));
3829
3830 source += synth_util->source_stride;
3831 i++;
3832 }
3833 }
3834
3835 /**
3836 * ags_synth_util_compute_sawtooth:
3837 * @synth_util: the #AgsSynthUtil-struct
3838 *
3839 * Compute sawtooth synth.
3840 *
3841 * Since: 3.9.3
3842 */
3843 void
ags_synth_util_compute_sawtooth(AgsSynthUtil * synth_util)3844 ags_synth_util_compute_sawtooth(AgsSynthUtil *synth_util)
3845 {
3846 if(synth_util == NULL ||
3847 synth_util->source == NULL){
3848 return;
3849 }
3850
3851 switch(synth_util->audio_buffer_util_format){
3852 case AGS_AUDIO_BUFFER_UTIL_S8:
3853 {
3854 ags_synth_util_compute_sawtooth_s8(synth_util);
3855 }
3856 break;
3857 case AGS_AUDIO_BUFFER_UTIL_S16:
3858 {
3859 ags_synth_util_compute_sawtooth_s16(synth_util);
3860 }
3861 break;
3862 case AGS_AUDIO_BUFFER_UTIL_S24:
3863 {
3864 ags_synth_util_compute_sawtooth_s24(synth_util);
3865 }
3866 break;
3867 case AGS_AUDIO_BUFFER_UTIL_S32:
3868 {
3869 ags_synth_util_compute_sawtooth_s32(synth_util);
3870 }
3871 break;
3872 case AGS_AUDIO_BUFFER_UTIL_S64:
3873 {
3874 ags_synth_util_compute_sawtooth_s64(synth_util);
3875 }
3876 break;
3877 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
3878 {
3879 ags_synth_util_compute_sawtooth_float(synth_util);
3880 }
3881 break;
3882 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
3883 {
3884 ags_synth_util_compute_sawtooth_double(synth_util);
3885 }
3886 break;
3887 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
3888 {
3889 ags_synth_util_compute_sawtooth_complex(synth_util);
3890 }
3891 break;
3892 }
3893 }
3894
3895 /**
3896 * ags_synth_util_sawtooth_s8:
3897 * @buffer: the audio buffer
3898 * @freq: the frequency of the sawtooth wave
3899 * @phase: the phase of the sawtooth wave
3900 * @volume: the volume of the sawtooth wave
3901 * @samplerate: the samplerate
3902 * @offset: start frame
3903 * @n_frames: generate n frames
3904 *
3905 * Generate sawtooth wave.
3906 *
3907 * Since: 3.0.0
3908 */
3909 void
ags_synth_util_sawtooth_s8(gint8 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)3910 ags_synth_util_sawtooth_s8(gint8 *buffer,
3911 gdouble freq, gdouble phase, gdouble volume,
3912 guint samplerate,
3913 guint offset, guint n_frames)
3914 {
3915 static const gdouble scale = 127.0;
3916 guint i;
3917
3918 if(buffer == NULL){
3919 return;
3920 }
3921
3922 phase = (int) ceil(phase) % (int) ceil(freq);
3923 phase = ceil(phase / freq) * ceil(samplerate / freq);
3924
3925 for(i = offset; i < offset + n_frames; i++){
3926 buffer[i] = (gint8) (0xff & ((gint16) buffer[i] + (gint16) (((((int) ceil(i + phase) % (int) ceil(samplerate / freq)) * 2.0 * freq / samplerate) - 1.0) * scale * volume)));
3927 }
3928 }
3929
3930 /**
3931 * ags_synth_util_sawtooth_s16:
3932 * @buffer: the audio buffer
3933 * @freq: the frequency of the sawtooth wave
3934 * @phase: the phase of the sawtooth wave
3935 * @volume: the volume of the sawtooth wave
3936 * @samplerate: the samplerate
3937 * @offset: start frame
3938 * @n_frames: generate n frames
3939 *
3940 * Generate sawtooth wave.
3941 *
3942 * Since: 3.0.0
3943 */
3944 void
ags_synth_util_sawtooth_s16(gint16 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)3945 ags_synth_util_sawtooth_s16(gint16 *buffer,
3946 gdouble freq, gdouble phase, gdouble volume,
3947 guint samplerate,
3948 guint offset, guint n_frames)
3949 {
3950 static const gdouble scale = 32767.0;
3951 guint i;
3952
3953 if(buffer == NULL){
3954 return;
3955 }
3956
3957 phase = (int) ceil(phase) % (int) ceil(freq);
3958 phase = ceil(phase / freq) * ceil(samplerate / freq);
3959
3960 for(i = offset; i < offset + n_frames; i++){
3961 buffer[i] = (gint16) (0xffff & ((gint32) buffer[i] + (gint32) (((((int) ceil(i + phase) % (int) ceil(samplerate / freq)) * 2.0 * freq / samplerate) - 1.0) * scale * volume)));
3962 }
3963 }
3964
3965 /**
3966 * ags_synth_util_sawtooth_s24:
3967 * @buffer: the audio buffer
3968 * @freq: the frequency of the sawtooth wave
3969 * @phase: the phase of the sawtooth wave
3970 * @volume: the volume of the sawtooth wave
3971 * @samplerate: the samplerate
3972 * @offset: start frame
3973 * @n_frames: generate n frames
3974 *
3975 * Generate sawtooth wave.
3976 *
3977 * Since: 3.0.0
3978 */
3979 void
ags_synth_util_sawtooth_s24(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)3980 ags_synth_util_sawtooth_s24(gint32 *buffer,
3981 gdouble freq, gdouble phase, gdouble volume,
3982 guint samplerate,
3983 guint offset, guint n_frames)
3984 {
3985 static const gdouble scale = 8388607.0;
3986 guint i;
3987
3988 if(buffer == NULL){
3989 return;
3990 }
3991
3992 phase = (int) ceil(phase) % (int) ceil(freq);
3993 phase = ceil(phase / freq) * ceil(samplerate / freq);
3994
3995 for(i = offset; i < offset + n_frames; i++){
3996 buffer[i] = (gint32) (0xffffffff & ((gint32) buffer[i] + (gint32) (((((int) ceil(i + phase) % (int) ceil(samplerate / freq)) * 2.0 * freq / samplerate) - 1.0) * scale * volume)));
3997 }
3998 }
3999
4000 /**
4001 * ags_synth_util_sawtooth_s32:
4002 * @buffer: the audio buffer
4003 * @freq: the frequency of the sawtooth wave
4004 * @phase: the phase of the sawtooth wave
4005 * @volume: the volume of the sawtooth wave
4006 * @samplerate: the samplerate
4007 * @offset: start frame
4008 * @n_frames: generate n frames
4009 *
4010 * Generate sawtooth wave.
4011 *
4012 * Since: 3.0.0
4013 */
4014 void
ags_synth_util_sawtooth_s32(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)4015 ags_synth_util_sawtooth_s32(gint32 *buffer,
4016 gdouble freq, gdouble phase, gdouble volume,
4017 guint samplerate,
4018 guint offset, guint n_frames)
4019 {
4020 static const gdouble scale = 214748363.0;
4021 guint i;
4022
4023 if(buffer == NULL){
4024 return;
4025 }
4026
4027 phase = (int) ceil(phase) % (int) ceil(freq);
4028 phase = ceil(phase / freq) * ceil(samplerate / freq);
4029
4030 for(i = offset; i < offset + n_frames; i++){
4031 buffer[i] = (gint32) (0xffffffff & ((gint64) buffer[i] + (gint64) (((((int) ceil(i + phase) % (int) ceil(samplerate / freq)) * 2.0 * freq / samplerate) - 1.0) * scale * volume)));
4032 }
4033 }
4034
4035 /**
4036 * ags_synth_util_sawtooth_s64:
4037 * @buffer: the audio buffer
4038 * @freq: the frequency of the sawtooth wave
4039 * @phase: the phase of the sawtooth wave
4040 * @volume: the volume of the sawtooth wave
4041 * @samplerate: the samplerate
4042 * @offset: start frame
4043 * @n_frames: generate n frames
4044 *
4045 * Generate sawtooth wave.
4046 *
4047 * Since: 3.0.0
4048 */
4049 void
ags_synth_util_sawtooth_s64(gint64 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)4050 ags_synth_util_sawtooth_s64(gint64 *buffer,
4051 gdouble freq, gdouble phase, gdouble volume,
4052 guint samplerate,
4053 guint offset, guint n_frames)
4054 {
4055 static const gdouble scale = 9223372036854775807.0;
4056 guint i;
4057
4058 if(buffer == NULL){
4059 return;
4060 }
4061
4062 phase = (int) ceil(phase) % (int) ceil(freq);
4063 phase = ceil(phase / freq) * ceil(samplerate / freq);
4064
4065 for(i = offset; i < offset + n_frames; i++){
4066 buffer[i] = (gint64) (0xffffffffffffffff & ((gint64) buffer[i] + (gint64) (((((int) ceil(i + phase) % (int) ceil(samplerate / freq)) * 2.0 * freq / samplerate) - 1.0) * scale * volume)));
4067 }
4068 }
4069
4070 /**
4071 * ags_synth_util_sawtooth_float:
4072 * @buffer: the audio buffer
4073 * @freq: the frequency of the sawtooth wave
4074 * @phase: the phase of the sawtooth wave
4075 * @volume: the volume of the sawtooth wave
4076 * @samplerate: the samplerate
4077 * @offset: start frame
4078 * @n_frames: generate n frames
4079 *
4080 * Generate sawtooth wave.
4081 *
4082 * Since: 3.0.0
4083 */
4084 void
ags_synth_util_sawtooth_float(gfloat * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)4085 ags_synth_util_sawtooth_float(gfloat *buffer,
4086 gdouble freq, gdouble phase, gdouble volume,
4087 guint samplerate,
4088 guint offset, guint n_frames)
4089 {
4090 guint i;
4091
4092 if(buffer == NULL){
4093 return;
4094 }
4095
4096 phase = (int) ceil(phase) % (int) ceil(freq);
4097 phase = ceil(phase / freq) * ceil(samplerate / freq);
4098
4099 for(i = offset; i < offset + n_frames; i++){
4100 buffer[i] = ((double) buffer[i] + (double) (((((int) ceil(i + phase) % (int) ceil(samplerate / freq)) * 2.0 * freq / samplerate) - 1.0) * volume));
4101 }
4102 }
4103
4104 /**
4105 * ags_synth_util_sawtooth_double:
4106 * @buffer: the audio buffer
4107 * @freq: the frequency of the sawtooth wave
4108 * @phase: the phase of the sawtooth wave
4109 * @volume: the volume of the sawtooth wave
4110 * @samplerate: the samplerate
4111 * @offset: start frame
4112 * @n_frames: generate n frames
4113 *
4114 * Generate sawtooth wave.
4115 *
4116 * Since: 3.0.0
4117 */
4118 void
ags_synth_util_sawtooth_double(gdouble * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)4119 ags_synth_util_sawtooth_double(gdouble *buffer,
4120 gdouble freq, gdouble phase, gdouble volume,
4121 guint samplerate,
4122 guint offset, guint n_frames)
4123 {
4124 guint i;
4125
4126 if(buffer == NULL){
4127 return;
4128 }
4129
4130 phase = (int) ceil(phase) % (int) ceil(freq);
4131 phase = ceil(phase / freq) * ceil(samplerate / freq);
4132
4133 for(i = offset; i < offset + n_frames; i++){
4134 buffer[i] = ((double) buffer[i] + (double) (((((int) ceil(i + phase) % (int) ceil(samplerate / freq)) * 2.0 * freq / samplerate) - 1.0) * volume));
4135 }
4136 }
4137
4138 /**
4139 * ags_synth_util_sawtooth_complex:
4140 * @buffer: the audio buffer
4141 * @freq: the frequency of the sawtooth wave
4142 * @phase: the phase of the sawtooth wave
4143 * @volume: the volume of the sawtooth wave
4144 * @samplerate: the samplerate
4145 * @offset: start frame
4146 * @n_frames: generate n frames
4147 *
4148 * Generate sawtooth wave.
4149 *
4150 * Since: 3.0.0
4151 */
4152 void
ags_synth_util_sawtooth_complex(AgsComplex * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)4153 ags_synth_util_sawtooth_complex(AgsComplex *buffer,
4154 gdouble freq, gdouble phase, gdouble volume,
4155 guint samplerate,
4156 guint offset, guint n_frames)
4157 {
4158 AgsComplex *c_ptr;
4159 AgsComplex **c_ptr_ptr;
4160
4161 double y;
4162
4163 guint i_stop;
4164 guint i;
4165
4166 if(buffer == NULL){
4167 return;
4168 }
4169
4170 phase = (int) ceil(phase) % (int) ceil(freq);
4171 phase = ceil(phase / freq) * ceil(samplerate / freq);
4172
4173 c_ptr = buffer;
4174 c_ptr_ptr = &c_ptr;
4175
4176 i_stop = offset + n_frames;
4177
4178 for(i = offset; i < i_stop; i++, c_ptr++){
4179 y = (double) (((((int) ceil(i + phase) % (int) ceil(samplerate / freq)) * 2.0 * freq / samplerate) - 1.0) * volume);
4180
4181 AGS_AUDIO_BUFFER_UTIL_DOUBLE_TO_COMPLEX(y, c_ptr_ptr);
4182 }
4183 }
4184
4185 /**
4186 * ags_synth_util_sawtooth:
4187 * @buffer: the audio buffer
4188 * @freq: the frequency of the sawtooth wave
4189 * @phase: the phase of the sawtooth wave
4190 * @volume: the volume of the sawtooth wave
4191 * @samplerate: the samplerate
4192 * @audio_buffer_util_format: the audio data format
4193 * @offset: start frame
4194 * @n_frames: generate n frames
4195 *
4196 * Generate sawtooth wave.
4197 *
4198 * Since: 3.0.0
4199 */
4200 void
ags_synth_util_sawtooth(void * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint audio_buffer_util_format,guint offset,guint n_frames)4201 ags_synth_util_sawtooth(void *buffer,
4202 gdouble freq, gdouble phase, gdouble volume,
4203 guint samplerate, guint audio_buffer_util_format,
4204 guint offset, guint n_frames)
4205 {
4206 if(buffer == NULL){
4207 return;
4208 }
4209
4210 switch(audio_buffer_util_format){
4211 case AGS_AUDIO_BUFFER_UTIL_S8:
4212 {
4213 ags_synth_util_sawtooth_s8((gint8 *) buffer,
4214 freq, phase, volume,
4215 samplerate,
4216 offset, n_frames);
4217 }
4218 break;
4219 case AGS_AUDIO_BUFFER_UTIL_S16:
4220 {
4221 ags_synth_util_sawtooth_s16((gint16 *) buffer,
4222 freq, phase, volume,
4223 samplerate,
4224 offset, n_frames);
4225 }
4226 break;
4227 case AGS_AUDIO_BUFFER_UTIL_S24:
4228 {
4229 ags_synth_util_sawtooth_s24((gint32 *) buffer,
4230 freq, phase, volume,
4231 samplerate,
4232 offset, n_frames);
4233 }
4234 break;
4235 case AGS_AUDIO_BUFFER_UTIL_S32:
4236 {
4237 ags_synth_util_sawtooth_s32((gint32 *) buffer,
4238 freq, phase, volume,
4239 samplerate,
4240 offset, n_frames);
4241 }
4242 break;
4243 case AGS_AUDIO_BUFFER_UTIL_S64:
4244 {
4245 ags_synth_util_sawtooth_s64((gint64 *) buffer,
4246 freq, phase, volume,
4247 samplerate,
4248 offset, n_frames);
4249 }
4250 break;
4251 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
4252 {
4253 ags_synth_util_sawtooth_float((gfloat *) buffer,
4254 freq, phase, volume,
4255 samplerate,
4256 offset, n_frames);
4257 }
4258 break;
4259 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
4260 {
4261 ags_synth_util_sawtooth_double((gdouble *) buffer,
4262 freq, phase, volume,
4263 samplerate,
4264 offset, n_frames);
4265 }
4266 break;
4267 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
4268 {
4269 ags_synth_util_sawtooth_complex((AgsComplex *) buffer,
4270 freq, phase, volume,
4271 samplerate,
4272 offset, n_frames);
4273 }
4274 break;
4275 default:
4276 {
4277 g_warning("ags_synth_util_sawtooth() - unsupported format");
4278 }
4279 }
4280 }
4281
4282 /**
4283 * ags_synth_util_compute_triangle_s8:
4284 * @synth_util: the #AgsSynthUtil-struct
4285 *
4286 * Compute triangle synth of signed 8 bit data.
4287 *
4288 * Since: 3.9.3
4289 */
4290 void
ags_synth_util_compute_triangle_s8(AgsSynthUtil * synth_util)4291 ags_synth_util_compute_triangle_s8(AgsSynthUtil *synth_util)
4292 {
4293 gint8 *source, *tmp_source;
4294
4295 gdouble ratio;
4296 gdouble volume;
4297 guint i, i_stop;
4298
4299 static const gdouble scale = 127.0;
4300
4301 if(synth_util == NULL ||
4302 synth_util->source == NULL){
4303 return;
4304 }
4305
4306 source = synth_util->source;
4307
4308 ratio = synth_util->frequency / synth_util->samplerate;
4309 volume = scale * synth_util->volume;
4310
4311 i = 0;
4312
4313 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
4314 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4315
4316 if(synth_util->offset + i_stop > synth_util->frame_count){
4317 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4318 }
4319
4320 for(; i < i_stop;){
4321 ags_v8double v_buffer, v_triangle;
4322
4323 tmp_source = source;
4324
4325 v_buffer = (ags_v8double) {
4326 (gdouble) *(tmp_source),
4327 (gdouble) *(tmp_source += synth_util->source_stride),
4328 (gdouble) *(tmp_source += synth_util->source_stride),
4329 (gdouble) *(tmp_source += synth_util->source_stride),
4330 (gdouble) *(tmp_source += synth_util->source_stride),
4331 (gdouble) *(tmp_source += synth_util->source_stride),
4332 (gdouble) *(tmp_source += synth_util->source_stride),
4333 (gdouble) *(tmp_source += synth_util->source_stride)
4334 };
4335
4336 v_triangle = (ags_v8double) {
4337 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4338 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4339 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4340 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4341 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4342 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4343 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4344 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4345 };
4346
4347 v_triangle *= volume;
4348
4349 v_buffer += v_triangle;
4350
4351 *(source) = (gint8) v_buffer[0];
4352 *(source += synth_util->source_stride) = (gint8) v_buffer[1];
4353 *(source += synth_util->source_stride) = (gint8) v_buffer[2];
4354 *(source += synth_util->source_stride) = (gint8) v_buffer[3];
4355 *(source += synth_util->source_stride) = (gint8) v_buffer[4];
4356 *(source += synth_util->source_stride) = (gint8) v_buffer[5];
4357 *(source += synth_util->source_stride) = (gint8) v_buffer[6];
4358 *(source += synth_util->source_stride) = (gint8) v_buffer[7];
4359
4360 source += synth_util->source_stride;
4361
4362 i += 8;
4363 }
4364 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
4365 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4366
4367 if(synth_util->offset + i_stop > synth_util->frame_count){
4368 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4369 }
4370
4371 for(; i < i_stop;){
4372 double ret_v_buffer[8], tmp_ret_v_buffer[8];
4373
4374 tmp_source = source;
4375
4376 double v_buffer[] = {
4377 (double) *(tmp_source),
4378 (double) *(tmp_source += synth_util->source_stride),
4379 (double) *(tmp_source += synth_util->source_stride),
4380 (double) *(tmp_source += synth_util->source_stride),
4381 (double) *(tmp_source += synth_util->source_stride),
4382 (double) *(tmp_source += synth_util->source_stride),
4383 (double) *(tmp_source += synth_util->source_stride),
4384 (double) *(tmp_source += synth_util->source_stride)};
4385 double v_triangle[] = {
4386 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4387 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4388 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4389 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4390 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4391 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4392 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4393 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume)};
4394 double v_volume[] = {(double) volume};
4395
4396 vDSP_vmulD(v_triangle, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
4397 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
4398
4399 *(source) = (gint8) ret_v_buffer[0];
4400 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[1];
4401 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[2];
4402 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[3];
4403 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[4];
4404 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[5];
4405 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[6];
4406 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[7];
4407
4408 source += synth_util->source_stride;
4409
4410 i += 8;
4411 }
4412 #else
4413 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4414
4415 if(synth_util->offset + i_stop > synth_util->frame_count){
4416 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4417 }
4418
4419 for(; i < i_stop;){
4420 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4421 source += synth_util->source_stride;
4422 i++;
4423 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4424 source += synth_util->source_stride;
4425 i++;
4426 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4427 source += synth_util->source_stride;
4428 i++;
4429 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4430 source += synth_util->source_stride;
4431 i++;
4432 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4433 source += synth_util->source_stride;
4434 i++;
4435 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4436 source += synth_util->source_stride;
4437 i++;
4438 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4439 source += synth_util->source_stride;
4440 i++;
4441 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4442 source += synth_util->source_stride;
4443 i++;
4444 }
4445 #endif
4446
4447 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
4448 source[0] = (gint8) ((gint16) source[0] + (gint16) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4449
4450 source += synth_util->source_stride;
4451 i++;
4452 }
4453 }
4454
4455 /**
4456 * ags_synth_util_compute_triangle_s16:
4457 * @synth_util: the #AgsSynthUtil-struct
4458 *
4459 * Compute triangle synth of signed 16 bit data.
4460 *
4461 * Since: 3.9.3
4462 */
4463 void
ags_synth_util_compute_triangle_s16(AgsSynthUtil * synth_util)4464 ags_synth_util_compute_triangle_s16(AgsSynthUtil *synth_util)
4465 {
4466 gint16 *source, *tmp_source;
4467
4468 gdouble ratio;
4469 gdouble volume;
4470 guint i, i_stop;
4471
4472 static const gdouble scale = 32767.0;
4473
4474 if(synth_util == NULL ||
4475 synth_util->source == NULL){
4476 return;
4477 }
4478
4479 source = synth_util->source;
4480
4481 ratio = synth_util->frequency / synth_util->samplerate;
4482 volume = scale * synth_util->volume;
4483
4484 i = 0;
4485
4486 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
4487 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4488
4489 if(synth_util->offset + i_stop > synth_util->frame_count){
4490 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4491 }
4492
4493 for(; i < i_stop;){
4494 ags_v8double v_buffer, v_triangle;
4495
4496 tmp_source = source;
4497
4498 v_buffer = (ags_v8double) {
4499 (gdouble) *(tmp_source),
4500 (gdouble) *(tmp_source += synth_util->source_stride),
4501 (gdouble) *(tmp_source += synth_util->source_stride),
4502 (gdouble) *(tmp_source += synth_util->source_stride),
4503 (gdouble) *(tmp_source += synth_util->source_stride),
4504 (gdouble) *(tmp_source += synth_util->source_stride),
4505 (gdouble) *(tmp_source += synth_util->source_stride),
4506 (gdouble) *(tmp_source += synth_util->source_stride)
4507 };
4508
4509 v_triangle = (ags_v8double) {
4510 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4511 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4512 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4513 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4514 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4515 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4516 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4517 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4518 };
4519
4520 i++;
4521
4522 v_triangle *= volume;
4523
4524 v_buffer += v_triangle;
4525
4526 *(source) = (gint16) v_buffer[0];
4527 *(source += synth_util->source_stride) = (gint16) v_buffer[1];
4528 *(source += synth_util->source_stride) = (gint16) v_buffer[2];
4529 *(source += synth_util->source_stride) = (gint16) v_buffer[3];
4530 *(source += synth_util->source_stride) = (gint16) v_buffer[4];
4531 *(source += synth_util->source_stride) = (gint16) v_buffer[5];
4532 *(source += synth_util->source_stride) = (gint16) v_buffer[6];
4533 *(source += synth_util->source_stride) = (gint16) v_buffer[7];
4534
4535 source += synth_util->source_stride;
4536 }
4537 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
4538 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4539
4540 if(synth_util->offset + i_stop > synth_util->frame_count){
4541 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4542 }
4543
4544 for(; i < i_stop;){
4545 double ret_v_buffer[8], tmp_ret_v_buffer[8];
4546
4547 tmp_source = source;
4548
4549 double v_buffer[] = {
4550 (double) *(tmp_source),
4551 (double) *(tmp_source += synth_util->source_stride),
4552 (double) *(tmp_source += synth_util->source_stride),
4553 (double) *(tmp_source += synth_util->source_stride),
4554 (double) *(tmp_source += synth_util->source_stride),
4555 (double) *(tmp_source += synth_util->source_stride),
4556 (double) *(tmp_source += synth_util->source_stride),
4557 (double) *(tmp_source += synth_util->source_stride)};
4558 double v_triangle[] = {
4559 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4560 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4561 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4562 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4563 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4564 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4565 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4566 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume)};
4567 double v_volume[] = {(double) volume};
4568
4569 vDSP_vmulD(v_triangle, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
4570 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
4571
4572 *(source) = (gint16) ret_v_buffer[0];
4573 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[1];
4574 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[2];
4575 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[3];
4576 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[4];
4577 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[5];
4578 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[6];
4579 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[7];
4580
4581 source += synth_util->source_stride;
4582
4583 i += 8;
4584 }
4585 #else
4586 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4587
4588 if(synth_util->offset + i_stop > synth_util->frame_count){
4589 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4590 }
4591
4592 for(; i < i_stop;){
4593 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4594 source += synth_util->source_stride;
4595 i++;
4596 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4597 source += synth_util->source_stride;
4598 i++;
4599 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4600 source += synth_util->source_stride;
4601 i++;
4602 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4603 source += synth_util->source_stride;
4604 i++;
4605 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4606 source += synth_util->source_stride;
4607 i++;
4608 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4609 source += synth_util->source_stride;
4610 i++;
4611 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4612 source += synth_util->source_stride;
4613 i++;
4614 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4615 source += synth_util->source_stride;
4616 i++;
4617 }
4618 #endif
4619
4620 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
4621 source[0] = (gint16) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4622
4623 source += synth_util->source_stride;
4624 i++;
4625 }
4626 }
4627
4628 /**
4629 * ags_synth_util_compute_triangle_s24:
4630 * @synth_util: the #AgsSynthUtil-struct
4631 *
4632 * Compute triangle synth of signed 24 bit data.
4633 *
4634 * Since: 3.9.3
4635 */
4636 void
ags_synth_util_compute_triangle_s24(AgsSynthUtil * synth_util)4637 ags_synth_util_compute_triangle_s24(AgsSynthUtil *synth_util)
4638 {
4639 gint32 *source, *tmp_source;
4640
4641 gdouble ratio;
4642 gdouble volume;
4643 guint i, i_stop;
4644
4645 static const gdouble scale = 8388607.0;
4646
4647 if(synth_util == NULL ||
4648 synth_util->source == NULL){
4649 return;
4650 }
4651
4652 source = synth_util->source;
4653
4654 ratio = synth_util->frequency / synth_util->samplerate;
4655 volume = scale * synth_util->volume;
4656
4657 i = 0;
4658
4659 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
4660 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4661
4662 if(synth_util->offset + i_stop > synth_util->frame_count){
4663 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4664 }
4665
4666 for(; i < i_stop;){
4667 ags_v8double v_buffer, v_triangle;
4668
4669 tmp_source = source;
4670
4671 v_buffer = (ags_v8double) {
4672 (gdouble) *(tmp_source),
4673 (gdouble) *(tmp_source += synth_util->source_stride),
4674 (gdouble) *(tmp_source += synth_util->source_stride),
4675 (gdouble) *(tmp_source += synth_util->source_stride),
4676 (gdouble) *(tmp_source += synth_util->source_stride),
4677 (gdouble) *(tmp_source += synth_util->source_stride),
4678 (gdouble) *(tmp_source += synth_util->source_stride),
4679 (gdouble) *(tmp_source += synth_util->source_stride)
4680 };
4681
4682 v_triangle = (ags_v8double) {
4683 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4684 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4685 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4686 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4687 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4688 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4689 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4690 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4691 };
4692
4693 v_triangle *= volume;
4694
4695 v_buffer += v_triangle;
4696
4697 *(source) = (gint32) v_buffer[0];
4698 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
4699 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
4700 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
4701 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
4702 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
4703 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
4704 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
4705
4706 source += synth_util->source_stride;
4707
4708 i += 8;
4709 }
4710 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
4711 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4712
4713 if(synth_util->offset + i_stop > synth_util->frame_count){
4714 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4715 }
4716
4717 for(; i < i_stop;){
4718 double ret_v_buffer[8], tmp_ret_v_buffer[8];
4719
4720 tmp_source = source;
4721
4722 double v_buffer[] = {
4723 (double) *(tmp_source),
4724 (double) *(tmp_source += synth_util->source_stride),
4725 (double) *(tmp_source += synth_util->source_stride),
4726 (double) *(tmp_source += synth_util->source_stride),
4727 (double) *(tmp_source += synth_util->source_stride),
4728 (double) *(tmp_source += synth_util->source_stride),
4729 (double) *(tmp_source += synth_util->source_stride),
4730 (double) *(tmp_source += synth_util->source_stride)};
4731 double v_triangle[] = {
4732 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4733 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4734 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4735 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4736 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4737 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4738 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4739 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume)};
4740 double v_volume[] = {(double) volume};
4741
4742 vDSP_vmulD(v_triangle, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
4743 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
4744
4745 *(source) = (gint32) ret_v_buffer[0];
4746 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
4747 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
4748 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
4749 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
4750 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
4751 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
4752 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
4753
4754 source += synth_util->source_stride;
4755
4756 i += 8;
4757 }
4758 #else
4759 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4760
4761 if(synth_util->offset + i_stop > synth_util->frame_count){
4762 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4763 }
4764
4765 for(; i < i_stop;){
4766 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4767 source += synth_util->source_stride;
4768 i++;
4769 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4770 source += synth_util->source_stride;
4771 i++;
4772 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4773 source += synth_util->source_stride;
4774 i++;
4775 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4776 source += synth_util->source_stride;
4777 i++;
4778 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4779 source += synth_util->source_stride;
4780 i++;
4781 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4782 source += synth_util->source_stride;
4783 i++;
4784 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4785 source += synth_util->source_stride;
4786 i++;
4787 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4788 source += synth_util->source_stride;
4789 i++;
4790 }
4791 #endif
4792
4793 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
4794 source[0] = (gint32) ((gint32) source[0] + (gint32) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4795
4796 source += synth_util->source_stride;
4797 i++;
4798 }
4799 }
4800
4801 /**
4802 * ags_synth_util_compute_triangle_s32:
4803 * @synth_util: the #AgsSynthUtil-struct
4804 *
4805 * Compute triangle synth of signed 32 bit data.
4806 *
4807 * Since: 3.9.3
4808 */
4809 void
ags_synth_util_compute_triangle_s32(AgsSynthUtil * synth_util)4810 ags_synth_util_compute_triangle_s32(AgsSynthUtil *synth_util)
4811 {
4812 gint32 *source, *tmp_source;
4813
4814 gdouble ratio;
4815 gdouble volume;
4816 guint i, i_stop;
4817
4818 static const gdouble scale = 214748363.0;
4819
4820 if(synth_util == NULL ||
4821 synth_util->source == NULL){
4822 return;
4823 }
4824
4825 source = synth_util->source;
4826
4827 ratio = synth_util->frequency / synth_util->samplerate;
4828 volume = scale * synth_util->volume;
4829
4830 i = 0;
4831
4832 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
4833 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4834
4835 if(synth_util->offset + i_stop > synth_util->frame_count){
4836 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4837 }
4838
4839 for(; i < i_stop;){
4840 ags_v8double v_buffer, v_triangle;
4841
4842 tmp_source = source;
4843
4844 v_buffer = (ags_v8double) {
4845 (gdouble) *(tmp_source),
4846 (gdouble) *(tmp_source += synth_util->source_stride),
4847 (gdouble) *(tmp_source += synth_util->source_stride),
4848 (gdouble) *(tmp_source += synth_util->source_stride),
4849 (gdouble) *(tmp_source += synth_util->source_stride),
4850 (gdouble) *(tmp_source += synth_util->source_stride),
4851 (gdouble) *(tmp_source += synth_util->source_stride),
4852 (gdouble) *(tmp_source += synth_util->source_stride)
4853 };
4854
4855 v_triangle = (ags_v8double) {
4856 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4857 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4858 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4859 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4860 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4861 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4862 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4863 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4864 };
4865
4866 v_triangle *= volume;
4867
4868 v_buffer += v_triangle;
4869
4870 *(source) = (gint32) v_buffer[0];
4871 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
4872 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
4873 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
4874 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
4875 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
4876 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
4877 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
4878
4879 source += synth_util->source_stride;
4880
4881 i += 8;
4882 }
4883 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
4884 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4885
4886 if(synth_util->offset + i_stop > synth_util->frame_count){
4887 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4888 }
4889
4890 for(; i < i_stop;){
4891 double ret_v_buffer[8], tmp_ret_v_buffer[8];
4892
4893 tmp_source = source;
4894
4895 double v_buffer[] = {
4896 (double) *(tmp_source),
4897 (double) *(tmp_source += synth_util->source_stride),
4898 (double) *(tmp_source += synth_util->source_stride),
4899 (double) *(tmp_source += synth_util->source_stride),
4900 (double) *(tmp_source += synth_util->source_stride),
4901 (double) *(tmp_source += synth_util->source_stride),
4902 (double) *(tmp_source += synth_util->source_stride),
4903 (double) *(tmp_source += synth_util->source_stride)};
4904 double v_triangle[] = {
4905 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4906 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4907 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4908 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4909 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4910 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4911 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
4912 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume)};
4913 double v_volume[] = {(double) volume};
4914
4915 vDSP_vmulD(v_triangle, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
4916 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
4917
4918 *(source) = (gint32) ret_v_buffer[0];
4919 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
4920 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
4921 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
4922 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
4923 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
4924 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
4925 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
4926
4927 source += synth_util->source_stride;
4928
4929 i += 8;
4930 }
4931 #else
4932 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
4933
4934 if(synth_util->offset + i_stop > synth_util->frame_count){
4935 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
4936 }
4937
4938 for(; i < i_stop;){
4939 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4940 source += synth_util->source_stride;
4941 i++;
4942 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4943 source += synth_util->source_stride;
4944 i++;
4945 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4946 source += synth_util->source_stride;
4947 i++;
4948 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4949 source += synth_util->source_stride;
4950 i++;
4951 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4952 source += synth_util->source_stride;
4953 i++;
4954 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4955 source += synth_util->source_stride;
4956 i++;
4957 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4958 source += synth_util->source_stride;
4959 i++;
4960 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4961 source += synth_util->source_stride;
4962 i++;
4963 }
4964 #endif
4965
4966 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
4967 source[0] = (gint32) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
4968
4969 source += synth_util->source_stride;
4970 i++;
4971 }
4972 }
4973
4974 /**
4975 * ags_synth_util_compute_triangle_s64:
4976 * @synth_util: the #AgsSynthUtil-struct
4977 *
4978 * Compute triangle synth of signed 64 bit data.
4979 *
4980 * Since: 3.9.3
4981 */
4982 void
ags_synth_util_compute_triangle_s64(AgsSynthUtil * synth_util)4983 ags_synth_util_compute_triangle_s64(AgsSynthUtil *synth_util)
4984 {
4985 gint64 *source, *tmp_source;
4986
4987 gdouble ratio;
4988 gdouble volume;
4989 guint i, i_stop;
4990
4991 static const gdouble scale = 9223372036854775807.0;
4992
4993 if(synth_util == NULL ||
4994 synth_util->source == NULL){
4995 return;
4996 }
4997
4998 source = synth_util->source;
4999
5000 ratio = synth_util->frequency / synth_util->samplerate;
5001 volume = scale * synth_util->volume;
5002
5003 i = 0;
5004
5005 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
5006 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5007
5008 if(synth_util->offset + i_stop > synth_util->frame_count){
5009 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5010 }
5011
5012 for(; i < i_stop;){
5013 ags_v8double v_buffer, v_triangle;
5014
5015 tmp_source = source;
5016
5017 v_buffer = (ags_v8double) {
5018 (gdouble) *(tmp_source),
5019 (gdouble) *(tmp_source += synth_util->source_stride),
5020 (gdouble) *(tmp_source += synth_util->source_stride),
5021 (gdouble) *(tmp_source += synth_util->source_stride),
5022 (gdouble) *(tmp_source += synth_util->source_stride),
5023 (gdouble) *(tmp_source += synth_util->source_stride),
5024 (gdouble) *(tmp_source += synth_util->source_stride),
5025 (gdouble) *(tmp_source += synth_util->source_stride)
5026 };
5027
5028 v_triangle = (ags_v8double) {
5029 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5030 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5031 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5032 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5033 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5034 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5035 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5036 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5037 };
5038
5039 v_triangle *= volume;
5040
5041 v_buffer += v_triangle;
5042
5043 *(source) = (gint64) v_buffer[0];
5044 *(source += synth_util->source_stride) = (gint64) v_buffer[1];
5045 *(source += synth_util->source_stride) = (gint64) v_buffer[2];
5046 *(source += synth_util->source_stride) = (gint64) v_buffer[3];
5047 *(source += synth_util->source_stride) = (gint64) v_buffer[4];
5048 *(source += synth_util->source_stride) = (gint64) v_buffer[5];
5049 *(source += synth_util->source_stride) = (gint64) v_buffer[6];
5050 *(source += synth_util->source_stride) = (gint64) v_buffer[7];
5051
5052 source += synth_util->source_stride;
5053
5054 i += 8;
5055 }
5056 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
5057 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5058
5059 if(synth_util->offset + i_stop > synth_util->frame_count){
5060 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5061 }
5062
5063 for(; i < i_stop;){
5064 double ret_v_buffer[8], tmp_ret_v_buffer[8];
5065
5066 tmp_source = source;
5067
5068 double v_buffer[] = {
5069 (double) *(tmp_source),
5070 (double) *(tmp_source += synth_util->source_stride),
5071 (double) *(tmp_source += synth_util->source_stride),
5072 (double) *(tmp_source += synth_util->source_stride),
5073 (double) *(tmp_source += synth_util->source_stride),
5074 (double) *(tmp_source += synth_util->source_stride),
5075 (double) *(tmp_source += synth_util->source_stride),
5076 (double) *(tmp_source += synth_util->source_stride)};
5077 double v_triangle[] = {
5078 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5079 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5080 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5081 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5082 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5083 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5084 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5085 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume)};
5086 double v_volume[] = {(double) volume};
5087
5088 vDSP_vmulD(v_triangle, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
5089 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
5090
5091 *(source) = (gint64) ret_v_buffer[0];
5092 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[1];
5093 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[2];
5094 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[3];
5095 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[4];
5096 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[5];
5097 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[6];
5098 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[7];
5099
5100 source += synth_util->source_stride;
5101
5102 i += 8;
5103 }
5104 #else
5105 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5106
5107 if(synth_util->offset + i_stop > synth_util->frame_count){
5108 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5109 }
5110
5111 for(; i < i_stop;){
5112 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5113 source += synth_util->source_stride;
5114 i++;
5115 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5116 source += synth_util->source_stride;
5117 i++;
5118 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5119 source += synth_util->source_stride;
5120 i++;
5121 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5122 source += synth_util->source_stride;
5123 i++;
5124 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5125 source += synth_util->source_stride;
5126 i++;
5127 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5128 source += synth_util->source_stride;
5129 i++;
5130 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5131 source += synth_util->source_stride;
5132 i++;
5133 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5134 source += synth_util->source_stride;
5135 i++;
5136 }
5137 #endif
5138
5139 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
5140 source[0] = (gint64) ((gint64) source[0] + (gint64) (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5141
5142 source += synth_util->source_stride;
5143 i++;
5144 }
5145 }
5146
5147 /**
5148 * ags_synth_util_compute_triangle_float:
5149 * @synth_util: the #AgsSynthUtil-struct
5150 *
5151 * Compute triangle synth of floating point data.
5152 *
5153 * Since: 3.9.3
5154 */
5155 void
ags_synth_util_compute_triangle_float(AgsSynthUtil * synth_util)5156 ags_synth_util_compute_triangle_float(AgsSynthUtil *synth_util)
5157 {
5158 gfloat *source, *tmp_source;
5159
5160 gdouble ratio;
5161 gdouble volume;
5162 guint i, i_stop;
5163
5164 static const gdouble scale = 9223372036854775807.0;
5165
5166 if(synth_util == NULL ||
5167 synth_util->source == NULL){
5168 return;
5169 }
5170
5171 source = synth_util->source;
5172
5173 ratio = synth_util->frequency / synth_util->samplerate;
5174 volume = scale * synth_util->volume;
5175
5176 i = 0;
5177
5178 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
5179 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5180
5181 if(synth_util->offset + i_stop > synth_util->frame_count){
5182 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5183 }
5184
5185 for(; i < i_stop;){
5186 ags_v8double v_buffer, v_triangle;
5187
5188 tmp_source = source;
5189
5190 v_buffer = (ags_v8double) {
5191 (gdouble) *(tmp_source),
5192 (gdouble) *(tmp_source += synth_util->source_stride),
5193 (gdouble) *(tmp_source += synth_util->source_stride),
5194 (gdouble) *(tmp_source += synth_util->source_stride),
5195 (gdouble) *(tmp_source += synth_util->source_stride),
5196 (gdouble) *(tmp_source += synth_util->source_stride),
5197 (gdouble) *(tmp_source += synth_util->source_stride),
5198 (gdouble) *(tmp_source += synth_util->source_stride)
5199 };
5200
5201 v_triangle = (ags_v8double) {
5202 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5203 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5204 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5205 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5206 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5207 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5208 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5209 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5210 };
5211
5212 v_triangle *= volume;
5213
5214 v_buffer += v_triangle;
5215
5216 *(source) = (gfloat) v_buffer[0];
5217 *(source += synth_util->source_stride) = (gfloat) v_buffer[1];
5218 *(source += synth_util->source_stride) = (gfloat) v_buffer[2];
5219 *(source += synth_util->source_stride) = (gfloat) v_buffer[3];
5220 *(source += synth_util->source_stride) = (gfloat) v_buffer[4];
5221 *(source += synth_util->source_stride) = (gfloat) v_buffer[5];
5222 *(source += synth_util->source_stride) = (gfloat) v_buffer[6];
5223 *(source += synth_util->source_stride) = (gfloat) v_buffer[7];
5224
5225 source += synth_util->source_stride;
5226
5227 i += 8;
5228 }
5229 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
5230 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5231
5232 if(synth_util->offset + i_stop > synth_util->frame_count){
5233 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5234 }
5235
5236 for(; i < i_stop;){
5237 double ret_v_buffer[8], tmp_ret_v_buffer[8];
5238
5239 tmp_source = source;
5240
5241 double v_buffer[] = {
5242 (double) *(tmp_source),
5243 (double) *(tmp_source += synth_util->source_stride),
5244 (double) *(tmp_source += synth_util->source_stride),
5245 (double) *(tmp_source += synth_util->source_stride),
5246 (double) *(tmp_source += synth_util->source_stride),
5247 (double) *(tmp_source += synth_util->source_stride),
5248 (double) *(tmp_source += synth_util->source_stride),
5249 (double) *(tmp_source += synth_util->source_stride)};
5250 double v_triangle[] = {
5251 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5252 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5253 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5254 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5255 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5256 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5257 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5258 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume)};
5259 double v_volume[] = {(double) volume};
5260
5261 vDSP_vmulD(v_triangle, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
5262 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
5263
5264 *(source) = (gfloat) ret_v_buffer[0];
5265 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[1];
5266 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[2];
5267 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[3];
5268 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[4];
5269 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[5];
5270 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[6];
5271 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[7];
5272
5273 source += synth_util->source_stride;
5274
5275 i += 8;
5276 }
5277 #else
5278 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5279
5280 if(synth_util->offset + i_stop > synth_util->frame_count){
5281 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5282 }
5283
5284 for(; i < i_stop;){
5285 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5286 source += synth_util->source_stride;
5287 i++;
5288 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5289 source += synth_util->source_stride;
5290 i++;
5291 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5292 source += synth_util->source_stride;
5293 i++;
5294 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5295 source += synth_util->source_stride;
5296 i++;
5297 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5298 source += synth_util->source_stride;
5299 i++;
5300 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5301 source += synth_util->source_stride;
5302 i++;
5303 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5304 source += synth_util->source_stride;
5305 i++;
5306 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5307 source += synth_util->source_stride;
5308 i++;
5309 }
5310 #endif
5311
5312 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
5313 source[0] = (gfloat) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5314
5315 source += synth_util->source_stride;
5316 i++;
5317 }
5318 }
5319
5320 /**
5321 * ags_synth_util_compute_triangle_double:
5322 * @synth_util: the #AgsSynthUtil-struct
5323 *
5324 * Compute triangle synth of double precision floating point data.
5325 *
5326 * Since: 3.9.3
5327 */
5328 void
ags_synth_util_compute_triangle_double(AgsSynthUtil * synth_util)5329 ags_synth_util_compute_triangle_double(AgsSynthUtil *synth_util)
5330 {
5331 gdouble *source, *tmp_source;
5332
5333 gdouble ratio;
5334 gdouble volume;
5335 guint i, i_stop;
5336
5337 static const gdouble scale = 9223372036854775807.0;
5338
5339 if(synth_util == NULL ||
5340 synth_util->source == NULL){
5341 return;
5342 }
5343
5344 source = synth_util->source;
5345
5346 ratio = synth_util->frequency / synth_util->samplerate;
5347 volume = scale * synth_util->volume;
5348
5349 i = 0;
5350
5351 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
5352 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5353
5354 if(synth_util->offset + i_stop > synth_util->frame_count){
5355 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5356 }
5357
5358 for(; i < i_stop;){
5359 ags_v8double v_buffer, v_triangle;
5360
5361 tmp_source = source;
5362
5363 v_buffer = (ags_v8double) {
5364 (gdouble) *(tmp_source),
5365 (gdouble) *(tmp_source += synth_util->source_stride),
5366 (gdouble) *(tmp_source += synth_util->source_stride),
5367 (gdouble) *(tmp_source += synth_util->source_stride),
5368 (gdouble) *(tmp_source += synth_util->source_stride),
5369 (gdouble) *(tmp_source += synth_util->source_stride),
5370 (gdouble) *(tmp_source += synth_util->source_stride),
5371 (gdouble) *(tmp_source += synth_util->source_stride)
5372 };
5373
5374 v_triangle = (ags_v8double) {
5375 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5376 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5377 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5378 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5379 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5380 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5381 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5382 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5383 };
5384
5385 v_triangle *= volume;
5386
5387 v_buffer += v_triangle;
5388
5389 *(source) = (gdouble) v_buffer[0];
5390 *(source += synth_util->source_stride) = (gdouble) v_buffer[1];
5391 *(source += synth_util->source_stride) = (gdouble) v_buffer[2];
5392 *(source += synth_util->source_stride) = (gdouble) v_buffer[3];
5393 *(source += synth_util->source_stride) = (gdouble) v_buffer[4];
5394 *(source += synth_util->source_stride) = (gdouble) v_buffer[5];
5395 *(source += synth_util->source_stride) = (gdouble) v_buffer[6];
5396 *(source += synth_util->source_stride) = (gdouble) v_buffer[7];
5397
5398 source += synth_util->source_stride;
5399
5400 i += 8;
5401 }
5402 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
5403 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5404
5405 if(synth_util->offset + i_stop > synth_util->frame_count){
5406 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5407 }
5408
5409 for(; i < i_stop;){
5410 double ret_v_buffer[8], tmp_ret_v_buffer[8];
5411
5412 tmp_source = source;
5413
5414 double v_buffer[] = {
5415 (double) *(tmp_source),
5416 (double) *(tmp_source += synth_util->source_stride),
5417 (double) *(tmp_source += synth_util->source_stride),
5418 (double) *(tmp_source += synth_util->source_stride),
5419 (double) *(tmp_source += synth_util->source_stride),
5420 (double) *(tmp_source += synth_util->source_stride),
5421 (double) *(tmp_source += synth_util->source_stride),
5422 (double) *(tmp_source += synth_util->source_stride)};
5423 double v_triangle[] = {
5424 (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5425 (((((synth_util->offset + i + 1) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 1) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5426 (((((synth_util->offset + i + 2) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 2) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5427 (((((synth_util->offset + i + 3) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 3) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5428 (((((synth_util->offset + i + 4) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 4) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5429 (((((synth_util->offset + i + 5) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 5) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5430 (((((synth_util->offset + i + 6) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 6) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume),
5431 (((((synth_util->offset + i + 7) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i + 7) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume)};
5432 double v_volume[] = {(double) volume};
5433
5434 vDSP_vmulD(v_triangle, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
5435 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
5436
5437 *(source) = (gdouble) ret_v_buffer[0];
5438 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[1];
5439 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[2];
5440 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[3];
5441 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[4];
5442 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[5];
5443 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[6];
5444 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[7];
5445
5446 source += synth_util->source_stride;
5447
5448 i += 8;
5449 }
5450 #else
5451 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
5452
5453 if(synth_util->offset + i_stop > synth_util->frame_count){
5454 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
5455 }
5456
5457 for(; i < i_stop;){
5458 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5459 source += synth_util->source_stride;
5460 i++;
5461 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5462 source += synth_util->source_stride;
5463 i++;
5464 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5465 source += synth_util->source_stride;
5466 i++;
5467 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5468 source += synth_util->source_stride;
5469 i++;
5470 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5471 source += synth_util->source_stride;
5472 i++;
5473 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5474 source += synth_util->source_stride;
5475 i++;
5476 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5477 source += synth_util->source_stride;
5478 i++;
5479 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5480 source += synth_util->source_stride;
5481 i++;
5482 }
5483 #endif
5484
5485 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
5486 source[0] = (gdouble) (source[0] + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume));
5487
5488 source += synth_util->source_stride;
5489 i++;
5490 }
5491 }
5492
5493 /**
5494 * ags_synth_util_compute_triangle_complex:
5495 * @synth_util: the #AgsSynthUtil-struct
5496 *
5497 * Compute triangle synth of complex data.
5498 *
5499 * Since: 3.9.3
5500 */
5501 void
ags_synth_util_compute_triangle_complex(AgsSynthUtil * synth_util)5502 ags_synth_util_compute_triangle_complex(AgsSynthUtil *synth_util)
5503 {
5504 AgsComplex *source;
5505
5506 gdouble ratio;
5507 gdouble volume;
5508 guint i;
5509
5510 if(synth_util == NULL ||
5511 synth_util->source == NULL){
5512 return;
5513 }
5514
5515 source = synth_util->source;
5516
5517 ratio = synth_util->frequency / synth_util->samplerate;
5518
5519 volume = synth_util->volume;
5520
5521 i = 0;
5522
5523 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
5524 ags_complex_set(source,
5525 (ags_complex_get(source) + (((((synth_util->offset + i) + synth_util->phase) * ratio * 2.0) - (((double) ((((synth_util->offset + i) + synth_util->phase) * ratio)) / 2.0) * 2.0) - 1.0) * volume)));
5526
5527 source += synth_util->source_stride;
5528 i++;
5529 }
5530 }
5531
5532 /**
5533 * ags_synth_util_compute_triangle:
5534 * @synth_util: the #AgsSynthUtil-struct
5535 *
5536 * Compute triangle synth.
5537 *
5538 * Since: 3.9.3
5539 */
5540 void
ags_synth_util_compute_triangle(AgsSynthUtil * synth_util)5541 ags_synth_util_compute_triangle(AgsSynthUtil *synth_util)
5542 {
5543 if(synth_util == NULL ||
5544 synth_util->source == NULL){
5545 return;
5546 }
5547
5548 switch(synth_util->audio_buffer_util_format){
5549 case AGS_AUDIO_BUFFER_UTIL_S8:
5550 {
5551 ags_synth_util_compute_triangle_s8(synth_util);
5552 }
5553 break;
5554 case AGS_AUDIO_BUFFER_UTIL_S16:
5555 {
5556 ags_synth_util_compute_triangle_s16(synth_util);
5557 }
5558 break;
5559 case AGS_AUDIO_BUFFER_UTIL_S24:
5560 {
5561 ags_synth_util_compute_triangle_s24(synth_util);
5562 }
5563 break;
5564 case AGS_AUDIO_BUFFER_UTIL_S32:
5565 {
5566 ags_synth_util_compute_triangle_s32(synth_util);
5567 }
5568 break;
5569 case AGS_AUDIO_BUFFER_UTIL_S64:
5570 {
5571 ags_synth_util_compute_triangle_s64(synth_util);
5572 }
5573 break;
5574 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
5575 {
5576 ags_synth_util_compute_triangle_float(synth_util);
5577 }
5578 break;
5579 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
5580 {
5581 ags_synth_util_compute_triangle_double(synth_util);
5582 }
5583 break;
5584 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
5585 {
5586 ags_synth_util_compute_triangle_complex(synth_util);
5587 }
5588 break;
5589 }
5590 }
5591
5592 /**
5593 * ags_synth_util_triangle_s8:
5594 * @buffer: the audio buffer
5595 * @freq: the frequency of the triangle wave
5596 * @phase: the phase of the triangle wave
5597 * @volume: the volume of the triangle wave
5598 * @samplerate: the samplerate
5599 * @offset: start frame
5600 * @n_frames: generate n frames
5601 *
5602 * Generate triangle wave.
5603 *
5604 * Since: 3.0.0
5605 */
5606 void
ags_synth_util_triangle_s8(gint8 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)5607 ags_synth_util_triangle_s8(gint8 *buffer,
5608 gdouble freq, gdouble phase, gdouble volume,
5609 guint samplerate,
5610 guint offset, guint n_frames)
5611 {
5612 static const gdouble scale = 127.0;
5613 guint i;
5614
5615 if(buffer == NULL){
5616 return;
5617 }
5618
5619 phase = (int) ceil(phase) % (int) ceil(freq);
5620 phase = ceil(phase / freq) * ceil(samplerate / freq);
5621
5622 for(i = offset; i < offset + n_frames; i++){
5623 buffer[i] = (gint8) (0xff & ((gint16) buffer[i] + (gint16) ((((phase + i) * freq / samplerate * 2.0) - ((int) ((double) ((int) ((phase + i) * freq / samplerate)) / 2.0) * 2) - 1.0) * scale * volume)));
5624 }
5625 }
5626
5627 /**
5628 * ags_synth_util_triangle_s16:
5629 * @buffer: the audio buffer
5630 * @freq: the frequency of the triangle wave
5631 * @phase: the phase of the triangle wave
5632 * @volume: the volume of the triangle wave
5633 * @samplerate: the samplerate
5634 * @offset: start frame
5635 * @n_frames: generate n frames
5636 *
5637 * Generate triangle wave.
5638 *
5639 * Since: 3.0.0
5640 */
5641 void
ags_synth_util_triangle_s16(gint16 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)5642 ags_synth_util_triangle_s16(gint16 *buffer,
5643 gdouble freq, gdouble phase, gdouble volume,
5644 guint samplerate,
5645 guint offset, guint n_frames)
5646 {
5647 static const gdouble scale = 32767.0;
5648 guint i;
5649
5650 if(buffer == NULL){
5651 return;
5652 }
5653
5654 phase = (int) ceil(phase) % (int) ceil(freq);
5655 phase = ceil(phase / freq) * ceil(samplerate / freq);
5656
5657 for(i = offset; i < offset + n_frames; i++){
5658 buffer[i] = (gint16) (0xffff & ((gint32) buffer[i] + (gint32) ((((phase + i) * freq / samplerate * 2.0) - ((int) ((double) ((int) ((phase + i) * freq / samplerate)) / 2.0) * 2) - 1.0) * scale * volume)));
5659 }
5660 }
5661
5662 /**
5663 * ags_synth_util_triangle_s24:
5664 * @buffer: the audio buffer
5665 * @freq: the frequency of the triangle wave
5666 * @phase: the phase of the triangle wave
5667 * @volume: the volume of the triangle wave
5668 * @samplerate: the samplerate
5669 * @offset: start frame
5670 * @n_frames: generate n frames
5671 *
5672 * Generate triangle wave.
5673 *
5674 * Since: 3.0.0
5675 */
5676 void
ags_synth_util_triangle_s24(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)5677 ags_synth_util_triangle_s24(gint32 *buffer,
5678 gdouble freq, gdouble phase, gdouble volume,
5679 guint samplerate,
5680 guint offset, guint n_frames)
5681 {
5682 static const gdouble scale = 8388607.0;
5683 guint i;
5684
5685 if(buffer == NULL){
5686 return;
5687 }
5688
5689 phase = (int) ceil(phase) % (int) ceil(freq);
5690 phase = ceil(phase / freq) * ceil(samplerate / freq);
5691
5692 for(i = offset; i < offset + n_frames; i++){
5693 buffer[i] = (gint32) (0xffffffff & ((gint32) buffer[i] + (gint32) ((((phase + i) * freq / samplerate * 2.0) - ((int) ((double) ((int) ((phase + i) * freq / samplerate)) / 2.0) * 2) - 1.0) * scale * volume)));
5694 }
5695 }
5696
5697 /**
5698 * ags_synth_util_triangle_s32:
5699 * @buffer: the audio buffer
5700 * @freq: the frequency of the triangle wave
5701 * @phase: the phase of the triangle wave
5702 * @volume: the volume of the triangle wave
5703 * @samplerate: the samplerate
5704 * @offset: start frame
5705 * @n_frames: generate n frames
5706 *
5707 * Generate triangle wave.
5708 *
5709 * Since: 3.0.0
5710 */
5711 void
ags_synth_util_triangle_s32(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)5712 ags_synth_util_triangle_s32(gint32 *buffer,
5713 gdouble freq, gdouble phase, gdouble volume,
5714 guint samplerate,
5715 guint offset, guint n_frames)
5716 {
5717 static const gdouble scale = 214748363.0;
5718 guint i;
5719
5720 if(buffer == NULL){
5721 return;
5722 }
5723
5724 phase = (int) ceil(phase) % (int) ceil(freq);
5725 phase = ceil(phase / freq) * ceil(samplerate / freq);
5726
5727 for(i = offset; i < offset + n_frames; i++){
5728 buffer[i] = (gint32) (0xffffffff & ((gint64) buffer[i] + (gint64) ((((phase + i) * freq / samplerate * 2.0) - ((int) ((double) ((int) ((phase + i) * freq / samplerate)) / 2.0) * 2) - 1.0) * scale * volume)));
5729 }
5730 }
5731
5732 /**
5733 * ags_synth_util_triangle_s64:
5734 * @buffer: the audio buffer
5735 * @freq: the frequency of the triangle wave
5736 * @phase: the phase of the triangle wave
5737 * @volume: the volume of the triangle wave
5738 * @samplerate: the samplerate
5739 * @offset: start frame
5740 * @n_frames: generate n frames
5741 *
5742 * Generate triangle wave.
5743 *
5744 * Since: 3.0.0
5745 */
5746 void
ags_synth_util_triangle_s64(gint64 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)5747 ags_synth_util_triangle_s64(gint64 *buffer,
5748 gdouble freq, gdouble phase, gdouble volume,
5749 guint samplerate,
5750 guint offset, guint n_frames)
5751 {
5752 static const gdouble scale = 9223372036854775807.0;
5753 guint i;
5754
5755 if(buffer == NULL){
5756 return;
5757 }
5758
5759 phase = (int) ceil(phase) % (int) ceil(freq);
5760 phase = ceil(phase / freq) * ceil(samplerate / freq);
5761
5762 for(i = offset; i < offset + n_frames; i++){
5763 buffer[i] = (gint64) (0xffffffffffffffff & ((gint64) buffer[i] + (gint64) ((((phase + i) * freq / samplerate * 2.0) - ((int) ((double) ((int) ((phase + i) * freq / samplerate)) / 2.0) * 2) - 1.0) * scale * volume)));
5764 }
5765 }
5766
5767 /**
5768 * ags_synth_util_triangle_float:
5769 * @buffer: the audio buffer
5770 * @freq: the frequency of the triangle wave
5771 * @phase: the phase of the triangle wave
5772 * @volume: the volume of the triangle wave
5773 * @samplerate: the samplerate
5774 * @offset: start frame
5775 * @n_frames: generate n frames
5776 *
5777 * Generate triangle wave.
5778 *
5779 * Since: 3.0.0
5780 */
5781 void
ags_synth_util_triangle_float(gfloat * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)5782 ags_synth_util_triangle_float(gfloat *buffer,
5783 gdouble freq, gdouble phase, gdouble volume,
5784 guint samplerate,
5785 guint offset, guint n_frames)
5786 {
5787 guint i;
5788
5789 if(buffer == NULL){
5790 return;
5791 }
5792
5793 phase = (int) ceil(phase) % (int) ceil(freq);
5794 phase = ceil(phase / freq) * ceil(samplerate / freq);
5795
5796 for(i = offset; i < offset + n_frames; i++){
5797 buffer[i] = ((double) buffer[i] + (double) ((((phase + i) * freq / samplerate * 2.0) - ((int) ((double) ((int) ((phase + i) * freq / samplerate)) / 2.0) * 2) - 1.0) * volume));
5798 }
5799 }
5800
5801 /**
5802 * ags_synth_util_triangle_double:
5803 * @buffer: the audio buffer
5804 * @freq: the frequency of the triangle wave
5805 * @phase: the phase of the triangle wave
5806 * @volume: the volume of the triangle wave
5807 * @samplerate: the samplerate
5808 * @offset: start frame
5809 * @n_frames: generate n frames
5810 *
5811 * Generate triangle wave.
5812 *
5813 * Since: 3.0.0
5814 */
5815 void
ags_synth_util_triangle_double(gdouble * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)5816 ags_synth_util_triangle_double(gdouble *buffer,
5817 gdouble freq, gdouble phase, gdouble volume,
5818 guint samplerate,
5819 guint offset, guint n_frames)
5820 {
5821 guint i;
5822
5823 if(buffer == NULL){
5824 return;
5825 }
5826
5827 phase = (int) ceil(phase) % (int) ceil(freq);
5828 phase = ceil(phase / freq) * ceil(samplerate / freq);
5829
5830 for(i = offset; i < offset + n_frames; i++){
5831 buffer[i] = ((double) buffer[i] + (double) ((((phase + i) * freq / samplerate * 2.0) - ((int) ((double) ((int) ((phase + i) * freq / samplerate)) / 2.0) * 2) - 1.0) * volume));
5832 }
5833 }
5834
5835 /**
5836 * ags_synth_util_triangle_complex:
5837 * @buffer: the audio buffer
5838 * @freq: the frequency of the triangle wave
5839 * @phase: the phase of the triangle wave
5840 * @volume: the volume of the triangle wave
5841 * @samplerate: the samplerate
5842 * @offset: start frame
5843 * @n_frames: generate n frames
5844 *
5845 * Generate triangle wave.
5846 *
5847 * Since: 3.0.0
5848 */
5849 void
ags_synth_util_triangle_complex(AgsComplex * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)5850 ags_synth_util_triangle_complex(AgsComplex *buffer,
5851 gdouble freq, gdouble phase, gdouble volume,
5852 guint samplerate,
5853 guint offset, guint n_frames)
5854 {
5855 AgsComplex *c_ptr;
5856 AgsComplex **c_ptr_ptr;
5857
5858 double y;
5859
5860 guint i_stop;
5861 guint i;
5862
5863 if(buffer == NULL){
5864 return;
5865 }
5866
5867 phase = (int) ceil(phase) % (int) ceil(freq);
5868 phase = ceil(phase / freq) * ceil(samplerate / freq);
5869
5870 c_ptr = buffer;
5871 c_ptr_ptr = &c_ptr;
5872
5873 i_stop = offset + n_frames;
5874
5875 for(i = offset; i < i_stop; i++, c_ptr++){
5876 y = (double) ((((phase + i) * freq / samplerate * 2.0) - ((int) ((double) ((int) ((phase + i) * freq / samplerate)) / 2.0) * 2) - 1.0) * volume);
5877
5878 AGS_AUDIO_BUFFER_UTIL_DOUBLE_TO_COMPLEX(y, c_ptr_ptr);
5879 }
5880 }
5881
5882 /**
5883 * ags_synth_util_triangle:
5884 * @buffer: the audio buffer
5885 * @freq: the frequency of the triangle wave
5886 * @phase: the phase of the triangle wave
5887 * @volume: the volume of the triangle wave
5888 * @samplerate: the samplerate
5889 * @audio_buffer_util_format: the audio data format
5890 * @offset: start frame
5891 * @n_frames: generate n frames
5892 *
5893 * Generate triangle wave.
5894 *
5895 * Since: 3.0.0
5896 */
5897 void
ags_synth_util_triangle(void * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint audio_buffer_util_format,guint offset,guint n_frames)5898 ags_synth_util_triangle(void *buffer,
5899 gdouble freq, gdouble phase, gdouble volume,
5900 guint samplerate, guint audio_buffer_util_format,
5901 guint offset, guint n_frames)
5902 {
5903 if(buffer == NULL){
5904 return;
5905 }
5906
5907 switch(audio_buffer_util_format){
5908 case AGS_AUDIO_BUFFER_UTIL_S8:
5909 {
5910 ags_synth_util_triangle_s8((gint8 *) buffer,
5911 freq, phase, volume,
5912 samplerate,
5913 offset, n_frames);
5914 }
5915 break;
5916 case AGS_AUDIO_BUFFER_UTIL_S16:
5917 {
5918 ags_synth_util_triangle_s16((gint16 *) buffer,
5919 freq, phase, volume,
5920 samplerate,
5921 offset, n_frames);
5922 }
5923 break;
5924 case AGS_AUDIO_BUFFER_UTIL_S24:
5925 {
5926 ags_synth_util_triangle_s24((gint32 *) buffer,
5927 freq, phase, volume,
5928 samplerate,
5929 offset, n_frames);
5930 }
5931 break;
5932 case AGS_AUDIO_BUFFER_UTIL_S32:
5933 {
5934 ags_synth_util_triangle_s32((gint32 *) buffer,
5935 freq, phase, volume,
5936 samplerate,
5937 offset, n_frames);
5938 }
5939 break;
5940 case AGS_AUDIO_BUFFER_UTIL_S64:
5941 {
5942 ags_synth_util_triangle_s64((gint64 *) buffer,
5943 freq, phase, volume,
5944 samplerate,
5945 offset, n_frames);
5946 }
5947 break;
5948 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
5949 {
5950 ags_synth_util_triangle_float((gfloat *) buffer,
5951 freq, phase, volume,
5952 samplerate,
5953 offset, n_frames);
5954 }
5955 break;
5956 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
5957 {
5958 ags_synth_util_triangle_double((gdouble *) buffer,
5959 freq, phase, volume,
5960 samplerate,
5961 offset, n_frames);
5962 }
5963 break;
5964 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
5965 {
5966 ags_synth_util_triangle_complex((AgsComplex *) buffer,
5967 freq, phase, volume,
5968 samplerate,
5969 offset, n_frames);
5970 }
5971 break;
5972 default:
5973 {
5974 g_warning("ags_synth_util_triangle() - unsupported format");
5975 }
5976 }
5977 }
5978
5979 /**
5980 * ags_synth_util_compute_square_s8:
5981 * @synth_util: the #AgsSynthUtil-struct
5982 *
5983 * Compute square synth of signed 8 bit data.
5984 *
5985 * Since: 3.9.3
5986 */
5987 void
ags_synth_util_compute_square_s8(AgsSynthUtil * synth_util)5988 ags_synth_util_compute_square_s8(AgsSynthUtil *synth_util)
5989 {
5990 gint8 *source, *tmp_source;
5991
5992 gdouble volume;
5993 guint i, i_stop;
5994
5995 static const gdouble scale = 127.0;
5996
5997 if(synth_util == NULL ||
5998 synth_util->source == NULL){
5999 return;
6000 }
6001
6002 source = synth_util->source;
6003
6004 volume = scale * synth_util->volume;
6005
6006 i = 0;
6007
6008 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
6009 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6010
6011 if(synth_util->offset + i_stop > synth_util->frame_count){
6012 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6013 }
6014
6015 for(; i < i_stop;){
6016 ags_v8double v_buffer, v_square;
6017
6018 tmp_source = source;
6019
6020 v_buffer = (ags_v8double) {
6021 (gdouble) *(tmp_source),
6022 (gdouble) *(tmp_source += synth_util->source_stride),
6023 (gdouble) *(tmp_source += synth_util->source_stride),
6024 (gdouble) *(tmp_source += synth_util->source_stride),
6025 (gdouble) *(tmp_source += synth_util->source_stride),
6026 (gdouble) *(tmp_source += synth_util->source_stride),
6027 (gdouble) *(tmp_source += synth_util->source_stride),
6028 (gdouble) *(tmp_source += synth_util->source_stride)
6029 };
6030
6031 v_square = (ags_v8double) {
6032 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6033 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6034 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6035 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6036 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6037 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6038 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6039 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6040 };
6041
6042 i++;
6043
6044 v_square *= volume;
6045
6046 v_buffer += v_square;
6047
6048 *(source) = (gint8) v_buffer[0];
6049 *(source += synth_util->source_stride) = (gint8) v_buffer[1];
6050 *(source += synth_util->source_stride) = (gint8) v_buffer[2];
6051 *(source += synth_util->source_stride) = (gint8) v_buffer[3];
6052 *(source += synth_util->source_stride) = (gint8) v_buffer[4];
6053 *(source += synth_util->source_stride) = (gint8) v_buffer[5];
6054 *(source += synth_util->source_stride) = (gint8) v_buffer[6];
6055 *(source += synth_util->source_stride) = (gint8) v_buffer[7];
6056
6057 source += synth_util->source_stride;
6058 }
6059 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
6060 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6061
6062 if(synth_util->offset + i_stop > synth_util->frame_count){
6063 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6064 }
6065
6066 for(; i < i_stop;){
6067 double ret_v_buffer[8], tmp_ret_v_buffer[8];
6068
6069 tmp_source = source;
6070
6071 double v_buffer[] = {
6072 (double) *(tmp_source),
6073 (double) *(tmp_source += synth_util->source_stride),
6074 (double) *(tmp_source += synth_util->source_stride),
6075 (double) *(tmp_source += synth_util->source_stride),
6076 (double) *(tmp_source += synth_util->source_stride),
6077 (double) *(tmp_source += synth_util->source_stride),
6078 (double) *(tmp_source += synth_util->source_stride),
6079 (double) *(tmp_source += synth_util->source_stride)};
6080 double v_square[] = {
6081 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6082 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6083 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6084 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6085 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6086 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6087 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6088 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume};
6089
6090 double v_volume[] = {(double) volume};
6091
6092 i++;
6093
6094 vDSP_vmulD(v_square, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
6095 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
6096
6097 *(source) = (gint8) ret_v_buffer[0];
6098 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[1];
6099 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[2];
6100 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[3];
6101 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[4];
6102 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[5];
6103 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[6];
6104 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[7];
6105
6106 source += synth_util->source_stride;
6107 }
6108 #else
6109 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6110
6111 if(synth_util->offset + i_stop > synth_util->frame_count){
6112 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6113 }
6114
6115 for(; i < i_stop;){
6116 tmp_source = source;
6117
6118 (*source) = (gint8) ((gint16) (tmp_source)[0] + (gint16) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6119 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6120 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6121 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6122 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6123 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6124 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6125 *(source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (gint16) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6126
6127 source += synth_util->source_stride;
6128 i++;
6129 }
6130 #endif
6131
6132 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
6133 source[0] = (gint8) ((gint16) source[0] + (gint16) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6134
6135 source += synth_util->source_stride;
6136 i++;
6137 }
6138 }
6139
6140 /**
6141 * ags_synth_util_compute_square_s16:
6142 * @synth_util: the #AgsSynthUtil-struct
6143 *
6144 * Compute square synth of signed 16 bit data.
6145 *
6146 * Since: 3.9.3
6147 */
6148 void
ags_synth_util_compute_square_s16(AgsSynthUtil * synth_util)6149 ags_synth_util_compute_square_s16(AgsSynthUtil *synth_util)
6150 {
6151 gint16 *source, *tmp_source;
6152
6153 gdouble volume;
6154 guint i, i_stop;
6155
6156 static const gdouble scale = 32767.0;
6157
6158 if(synth_util == NULL ||
6159 synth_util->source == NULL){
6160 return;
6161 }
6162
6163 source = synth_util->source;
6164
6165 volume = scale * synth_util->volume;
6166
6167 i = 0;
6168
6169 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
6170 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6171
6172 if(synth_util->offset + i_stop > synth_util->frame_count){
6173 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6174 }
6175
6176 for(; i < i_stop;){
6177 ags_v8double v_buffer, v_square;
6178
6179 tmp_source = source;
6180
6181 v_buffer = (ags_v8double) {
6182 (gdouble) *(tmp_source),
6183 (gdouble) *(tmp_source += synth_util->source_stride),
6184 (gdouble) *(tmp_source += synth_util->source_stride),
6185 (gdouble) *(tmp_source += synth_util->source_stride),
6186 (gdouble) *(tmp_source += synth_util->source_stride),
6187 (gdouble) *(tmp_source += synth_util->source_stride),
6188 (gdouble) *(tmp_source += synth_util->source_stride),
6189 (gdouble) *(tmp_source += synth_util->source_stride)
6190 };
6191
6192 v_square = (ags_v8double) {
6193 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6194 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6195 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6196 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6197 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6198 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6199 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6200 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6201 };
6202
6203 i++;
6204
6205 v_square *= volume;
6206
6207 v_buffer += v_square;
6208
6209 *(source) = (gint16) v_buffer[0];
6210 *(source += synth_util->source_stride) = (gint16) v_buffer[1];
6211 *(source += synth_util->source_stride) = (gint16) v_buffer[2];
6212 *(source += synth_util->source_stride) = (gint16) v_buffer[3];
6213 *(source += synth_util->source_stride) = (gint16) v_buffer[4];
6214 *(source += synth_util->source_stride) = (gint16) v_buffer[5];
6215 *(source += synth_util->source_stride) = (gint16) v_buffer[6];
6216 *(source += synth_util->source_stride) = (gint16) v_buffer[7];
6217
6218 source += synth_util->source_stride;
6219 }
6220 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
6221 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6222
6223 if(synth_util->offset + i_stop > synth_util->frame_count){
6224 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6225 }
6226
6227 for(; i < i_stop;){
6228 double ret_v_buffer[8], tmp_ret_v_buffer[8];
6229
6230 tmp_source = source;
6231
6232 double v_buffer[] = {
6233 (double) *(tmp_source),
6234 (double) *(tmp_source += synth_util->source_stride),
6235 (double) *(tmp_source += synth_util->source_stride),
6236 (double) *(tmp_source += synth_util->source_stride),
6237 (double) *(tmp_source += synth_util->source_stride),
6238 (double) *(tmp_source += synth_util->source_stride),
6239 (double) *(tmp_source += synth_util->source_stride),
6240 (double) *(tmp_source += synth_util->source_stride)};
6241 double v_square[] = {
6242 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6243 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6244 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6245 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6246 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6247 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6248 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6249 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume};
6250
6251 double v_volume[] = {(double) volume};
6252
6253 i++;
6254
6255 vDSP_vmulD(v_square, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
6256 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
6257
6258 *(source) = (gint16) ret_v_buffer[0];
6259 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[1];
6260 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[2];
6261 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[3];
6262 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[4];
6263 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[5];
6264 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[6];
6265 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[7];
6266
6267 source += synth_util->source_stride;
6268 }
6269 #else
6270 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6271
6272 if(synth_util->offset + i_stop > synth_util->frame_count){
6273 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6274 }
6275
6276 for(; i < i_stop;){
6277 tmp_source = source;
6278
6279 (*source) = (gint16) ((gint32) (tmp_source)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6280 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6281 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6282 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6283 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6284 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6285 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6286 *(source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6287
6288 source += synth_util->source_stride;
6289 i++;
6290 }
6291 #endif
6292
6293 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
6294 source[0] = (gint16) ((gint32) source[0] + (gint32) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6295
6296 source += synth_util->source_stride;
6297 i++;
6298 }
6299 }
6300
6301 /**
6302 * ags_synth_util_compute_square_s24:
6303 * @synth_util: the #AgsSynthUtil-struct
6304 *
6305 * Compute square synth of signed 24 bit data.
6306 *
6307 * Since: 3.9.3
6308 */
6309 void
ags_synth_util_compute_square_s24(AgsSynthUtil * synth_util)6310 ags_synth_util_compute_square_s24(AgsSynthUtil *synth_util)
6311 {
6312 gint32 *source, *tmp_source;
6313
6314 gdouble volume;
6315 guint i, i_stop;
6316
6317 static const gdouble scale = 8388607.0;
6318
6319 if(synth_util == NULL ||
6320 synth_util->source == NULL){
6321 return;
6322 }
6323
6324 source = synth_util->source;
6325
6326 volume = scale * synth_util->volume;
6327
6328 i = 0;
6329
6330 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
6331 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6332
6333 if(synth_util->offset + i_stop > synth_util->frame_count){
6334 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6335 }
6336
6337 for(; i < i_stop;){
6338 ags_v8double v_buffer, v_square;
6339
6340 tmp_source = source;
6341
6342 v_buffer = (ags_v8double) {
6343 (gdouble) *(tmp_source),
6344 (gdouble) *(tmp_source += synth_util->source_stride),
6345 (gdouble) *(tmp_source += synth_util->source_stride),
6346 (gdouble) *(tmp_source += synth_util->source_stride),
6347 (gdouble) *(tmp_source += synth_util->source_stride),
6348 (gdouble) *(tmp_source += synth_util->source_stride),
6349 (gdouble) *(tmp_source += synth_util->source_stride),
6350 (gdouble) *(tmp_source += synth_util->source_stride)
6351 };
6352
6353 v_square = (ags_v8double) {
6354 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6355 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6356 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6357 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6358 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6359 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6360 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6361 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6362 };
6363
6364 i++;
6365
6366 v_square *= volume;
6367
6368 v_buffer += v_square;
6369
6370 *(source) = (gint32) v_buffer[0];
6371 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
6372 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
6373 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
6374 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
6375 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
6376 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
6377 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
6378
6379 source += synth_util->source_stride;
6380 }
6381 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
6382 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6383
6384 if(synth_util->offset + i_stop > synth_util->frame_count){
6385 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6386 }
6387
6388 for(; i < i_stop;){
6389 double ret_v_buffer[8], tmp_ret_v_buffer[8];
6390
6391 tmp_source = source;
6392
6393 double v_buffer[] = {
6394 (double) *(tmp_source),
6395 (double) *(tmp_source += synth_util->source_stride),
6396 (double) *(tmp_source += synth_util->source_stride),
6397 (double) *(tmp_source += synth_util->source_stride),
6398 (double) *(tmp_source += synth_util->source_stride),
6399 (double) *(tmp_source += synth_util->source_stride),
6400 (double) *(tmp_source += synth_util->source_stride),
6401 (double) *(tmp_source += synth_util->source_stride)};
6402 double v_square[] = {
6403 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6404 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6405 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6406 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6407 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6408 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6409 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6410 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume};
6411
6412 double v_volume[] = {(double) volume};
6413
6414 i++;
6415
6416 vDSP_vmulD(v_square, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
6417 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
6418
6419 *(source) = (gint32) ret_v_buffer[0];
6420 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
6421 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
6422 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
6423 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
6424 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
6425 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
6426 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
6427
6428 source += synth_util->source_stride;
6429 }
6430 #else
6431 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6432
6433 if(synth_util->offset + i_stop > synth_util->frame_count){
6434 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6435 }
6436
6437 for(; i < i_stop;){
6438 tmp_source = source;
6439
6440 (*source) = (gint32) ((gint32) (tmp_source)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6441 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6442 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6443 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6444 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6445 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6446 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6447 *(source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (gint32) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6448
6449 source += synth_util->source_stride;
6450 i++;
6451 }
6452 #endif
6453
6454 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
6455 source[0] = (gint32) ((gint32) source[0] + (gint32) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6456
6457 source += synth_util->source_stride;
6458 i++;
6459 }
6460 }
6461
6462 /**
6463 * ags_synth_util_compute_square_s32:
6464 * @synth_util: the #AgsSynthUtil-struct
6465 *
6466 * Compute square synth of signed 32 bit data.
6467 *
6468 * Since: 3.9.3
6469 */
6470 void
ags_synth_util_compute_square_s32(AgsSynthUtil * synth_util)6471 ags_synth_util_compute_square_s32(AgsSynthUtil *synth_util)
6472 {
6473 gint32 *source, *tmp_source;
6474
6475 gdouble volume;
6476 guint i, i_stop;
6477
6478 static const gdouble scale = 214748363.0;
6479
6480 if(synth_util == NULL ||
6481 synth_util->source == NULL){
6482 return;
6483 }
6484
6485 source = synth_util->source;
6486
6487 volume = scale * synth_util->volume;
6488
6489 i = 0;
6490
6491 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
6492 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6493
6494 if(synth_util->offset + i_stop > synth_util->frame_count){
6495 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6496 }
6497
6498 for(; i < i_stop;){
6499 ags_v8double v_buffer, v_square;
6500
6501 tmp_source = source;
6502
6503 v_buffer = (ags_v8double) {
6504 (gdouble) *(tmp_source),
6505 (gdouble) *(tmp_source += synth_util->source_stride),
6506 (gdouble) *(tmp_source += synth_util->source_stride),
6507 (gdouble) *(tmp_source += synth_util->source_stride),
6508 (gdouble) *(tmp_source += synth_util->source_stride),
6509 (gdouble) *(tmp_source += synth_util->source_stride),
6510 (gdouble) *(tmp_source += synth_util->source_stride),
6511 (gdouble) *(tmp_source += synth_util->source_stride)
6512 };
6513
6514 v_square = (ags_v8double) {
6515 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6516 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6517 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6518 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6519 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6520 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6521 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6522 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6523 };
6524
6525 i++;
6526
6527 v_square *= volume;
6528
6529 v_buffer += v_square;
6530
6531 *(source) = (gint32) v_buffer[0];
6532 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
6533 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
6534 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
6535 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
6536 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
6537 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
6538 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
6539
6540 source += synth_util->source_stride;
6541 }
6542 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
6543 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6544
6545 if(synth_util->offset + i_stop > synth_util->frame_count){
6546 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6547 }
6548
6549 for(; i < i_stop;){
6550 double ret_v_buffer[8], tmp_ret_v_buffer[8];
6551
6552 tmp_source = source;
6553
6554 double v_buffer[] = {
6555 (double) *(tmp_source),
6556 (double) *(tmp_source += synth_util->source_stride),
6557 (double) *(tmp_source += synth_util->source_stride),
6558 (double) *(tmp_source += synth_util->source_stride),
6559 (double) *(tmp_source += synth_util->source_stride),
6560 (double) *(tmp_source += synth_util->source_stride),
6561 (double) *(tmp_source += synth_util->source_stride),
6562 (double) *(tmp_source += synth_util->source_stride)};
6563 double v_square[] = {
6564 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6565 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6566 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6567 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6568 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6569 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6570 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6571 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume};
6572
6573 double v_volume[] = {(double) volume};
6574
6575 i++;
6576
6577 vDSP_vmulD(v_square, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
6578 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
6579
6580 *(source) = (gint32) ret_v_buffer[0];
6581 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
6582 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
6583 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
6584 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
6585 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
6586 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
6587 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
6588
6589 source += synth_util->source_stride;
6590 }
6591 #else
6592 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6593
6594 if(synth_util->offset + i_stop > synth_util->frame_count){
6595 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6596 }
6597
6598 for(; i < i_stop;){
6599 tmp_source = source;
6600
6601 (*source) = (gint32) ((gint64) (tmp_source)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6602 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6603 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6604 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6605 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6606 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6607 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6608 *(source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6609
6610 source += synth_util->source_stride;
6611 i++;
6612 }
6613 #endif
6614
6615 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
6616 source[0] = (gint32) ((gint64) source[0] + (gint64) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6617
6618 source += synth_util->source_stride;
6619 i++;
6620 }
6621 }
6622
6623 /**
6624 * ags_synth_util_compute_square_s64:
6625 * @synth_util: the #AgsSynthUtil-struct
6626 *
6627 * Compute square synth of signed 64 bit data.
6628 *
6629 * Since: 3.9.3
6630 */
6631 void
ags_synth_util_compute_square_s64(AgsSynthUtil * synth_util)6632 ags_synth_util_compute_square_s64(AgsSynthUtil *synth_util)
6633 {
6634 gint64 *source, *tmp_source;
6635
6636 gdouble volume;
6637 guint i, i_stop;
6638
6639 static const gdouble scale = 9223372036854775807.0;
6640
6641 if(synth_util == NULL ||
6642 synth_util->source == NULL){
6643 return;
6644 }
6645
6646 source = synth_util->source;
6647
6648 volume = scale * synth_util->volume;
6649
6650 i = 0;
6651
6652 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
6653 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6654
6655 if(synth_util->offset + i_stop > synth_util->frame_count){
6656 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6657 }
6658
6659 for(; i < i_stop;){
6660 ags_v8double v_buffer, v_square;
6661
6662 tmp_source = source;
6663
6664 v_buffer = (ags_v8double) {
6665 (gdouble) *(tmp_source),
6666 (gdouble) *(tmp_source += synth_util->source_stride),
6667 (gdouble) *(tmp_source += synth_util->source_stride),
6668 (gdouble) *(tmp_source += synth_util->source_stride),
6669 (gdouble) *(tmp_source += synth_util->source_stride),
6670 (gdouble) *(tmp_source += synth_util->source_stride),
6671 (gdouble) *(tmp_source += synth_util->source_stride),
6672 (gdouble) *(tmp_source += synth_util->source_stride)
6673 };
6674
6675 v_square = (ags_v8double) {
6676 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6677 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6678 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6679 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6680 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6681 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6682 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6683 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6684 };
6685
6686 i++;
6687
6688 v_square *= volume;
6689
6690 v_buffer += v_square;
6691
6692 *(source) = (gint64) v_buffer[0];
6693 *(source += synth_util->source_stride) = (gint64) v_buffer[1];
6694 *(source += synth_util->source_stride) = (gint64) v_buffer[2];
6695 *(source += synth_util->source_stride) = (gint64) v_buffer[3];
6696 *(source += synth_util->source_stride) = (gint64) v_buffer[4];
6697 *(source += synth_util->source_stride) = (gint64) v_buffer[5];
6698 *(source += synth_util->source_stride) = (gint64) v_buffer[6];
6699 *(source += synth_util->source_stride) = (gint64) v_buffer[7];
6700
6701 source += synth_util->source_stride;
6702 }
6703 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
6704 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6705
6706 if(synth_util->offset + i_stop > synth_util->frame_count){
6707 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6708 }
6709
6710 for(; i < i_stop;){
6711 double ret_v_buffer[8], tmp_ret_v_buffer[8];
6712
6713 tmp_source = source;
6714
6715 double v_buffer[] = {
6716 (double) *(tmp_source),
6717 (double) *(tmp_source += synth_util->source_stride),
6718 (double) *(tmp_source += synth_util->source_stride),
6719 (double) *(tmp_source += synth_util->source_stride),
6720 (double) *(tmp_source += synth_util->source_stride),
6721 (double) *(tmp_source += synth_util->source_stride),
6722 (double) *(tmp_source += synth_util->source_stride),
6723 (double) *(tmp_source += synth_util->source_stride)};
6724 double v_square[] = {
6725 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6726 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6727 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6728 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6729 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6730 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6731 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6732 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume};
6733
6734 double v_volume[] = {(double) volume};
6735
6736 i++;
6737
6738 vDSP_vmulD(v_square, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
6739 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
6740
6741 *(source) = (gint64) ret_v_buffer[0];
6742 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[1];
6743 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[2];
6744 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[3];
6745 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[4];
6746 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[5];
6747 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[6];
6748 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[7];
6749
6750 source += synth_util->source_stride;
6751 }
6752 #else
6753 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6754
6755 if(synth_util->offset + i_stop > synth_util->frame_count){
6756 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6757 }
6758
6759 for(; i < i_stop;){
6760 tmp_source = source;
6761
6762 (*source) = (gint64) ((gint64) (tmp_source)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6763 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6764 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6765 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6766 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6767 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6768 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6769 *(source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (gint64) ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6770
6771 source += synth_util->source_stride;
6772 i++;
6773 }
6774 #endif
6775
6776 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
6777 source[0] = (gint64) ((gint64) source[0] + (gint64) ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6778
6779 source += synth_util->source_stride;
6780 i++;
6781 }
6782 }
6783
6784 /**
6785 * ags_synth_util_compute_square_float:
6786 * @synth_util: the #AgsSynthUtil-struct
6787 *
6788 * Compute square synth of floating point data.
6789 *
6790 * Since: 3.9.3
6791 */
6792 void
ags_synth_util_compute_square_float(AgsSynthUtil * synth_util)6793 ags_synth_util_compute_square_float(AgsSynthUtil *synth_util)
6794 {
6795 gfloat *source, *tmp_source;
6796
6797 gdouble volume;
6798 guint i, i_stop;
6799
6800 static const gdouble scale = 127.0;
6801
6802 if(synth_util == NULL ||
6803 synth_util->source == NULL){
6804 return;
6805 }
6806
6807 source = synth_util->source;
6808
6809 volume = synth_util->volume;
6810
6811 i = 0;
6812
6813 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
6814 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6815
6816 if(synth_util->offset + i_stop > synth_util->frame_count){
6817 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6818 }
6819
6820 for(; i < i_stop;){
6821 ags_v8double v_buffer, v_square;
6822
6823 tmp_source = source;
6824
6825 v_buffer = (ags_v8double) {
6826 (gdouble) *(tmp_source),
6827 (gdouble) *(tmp_source += synth_util->source_stride),
6828 (gdouble) *(tmp_source += synth_util->source_stride),
6829 (gdouble) *(tmp_source += synth_util->source_stride),
6830 (gdouble) *(tmp_source += synth_util->source_stride),
6831 (gdouble) *(tmp_source += synth_util->source_stride),
6832 (gdouble) *(tmp_source += synth_util->source_stride),
6833 (gdouble) *(tmp_source += synth_util->source_stride)
6834 };
6835
6836 v_square = (ags_v8double) {
6837 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6838 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6839 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6840 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6841 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6842 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6843 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6844 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6845 };
6846
6847 i++;
6848
6849 v_square *= volume;
6850
6851 v_buffer += v_square;
6852
6853 *(source) = (gfloat) v_buffer[0];
6854 *(source += synth_util->source_stride) = (gfloat) v_buffer[1];
6855 *(source += synth_util->source_stride) = (gfloat) v_buffer[2];
6856 *(source += synth_util->source_stride) = (gfloat) v_buffer[3];
6857 *(source += synth_util->source_stride) = (gfloat) v_buffer[4];
6858 *(source += synth_util->source_stride) = (gfloat) v_buffer[5];
6859 *(source += synth_util->source_stride) = (gfloat) v_buffer[6];
6860 *(source += synth_util->source_stride) = (gfloat) v_buffer[7];
6861
6862 source += synth_util->source_stride;
6863 }
6864 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
6865 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6866
6867 if(synth_util->offset + i_stop > synth_util->frame_count){
6868 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6869 }
6870
6871 for(; i < i_stop;){
6872 double ret_v_buffer[8], tmp_ret_v_buffer[8];
6873
6874 tmp_source = source;
6875
6876 double v_buffer[] = {
6877 (double) *(tmp_source),
6878 (double) *(tmp_source += synth_util->source_stride),
6879 (double) *(tmp_source += synth_util->source_stride),
6880 (double) *(tmp_source += synth_util->source_stride),
6881 (double) *(tmp_source += synth_util->source_stride),
6882 (double) *(tmp_source += synth_util->source_stride),
6883 (double) *(tmp_source += synth_util->source_stride),
6884 (double) *(tmp_source += synth_util->source_stride)};
6885 double v_square[] = {
6886 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6887 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6888 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6889 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6890 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6891 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6892 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6893 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume};
6894
6895 double v_volume[] = {(double) volume};
6896
6897 i++;
6898
6899 vDSP_vmulD(v_square, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
6900 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
6901
6902 *(source) = (gfloat) ret_v_buffer[0];
6903 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[1];
6904 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[2];
6905 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[3];
6906 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[4];
6907 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[5];
6908 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[6];
6909 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[7];
6910
6911 source += synth_util->source_stride;
6912 }
6913 #else
6914 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6915
6916 if(synth_util->offset + i_stop > synth_util->frame_count){
6917 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6918 }
6919
6920 for(; i < i_stop;){
6921 tmp_source = source;
6922
6923 (*source) = (gfloat) ((tmp_source)[0] + ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6924 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6925 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6926 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6927 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6928 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6929 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6930 *(source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6931
6932 source += synth_util->source_stride;
6933 i++;
6934 }
6935 #endif
6936
6937 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
6938 source[0] = (gfloat) (source[0] + ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
6939
6940 source += synth_util->source_stride;
6941 i++;
6942 }
6943 }
6944
6945 /**
6946 * ags_synth_util_compute_square_double:
6947 * @synth_util: the #AgsSynthUtil-struct
6948 *
6949 * Compute square synth of double precision floating point data.
6950 *
6951 * Since: 3.9.3
6952 */
6953 void
ags_synth_util_compute_square_double(AgsSynthUtil * synth_util)6954 ags_synth_util_compute_square_double(AgsSynthUtil *synth_util)
6955 {
6956 gdouble *source, *tmp_source;
6957
6958 gdouble volume;
6959 guint i, i_stop;
6960
6961 if(synth_util == NULL ||
6962 synth_util->source == NULL){
6963 return;
6964 }
6965
6966 source = synth_util->source;
6967
6968 volume = synth_util->volume;
6969
6970 i = 0;
6971
6972 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
6973 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
6974
6975 if(synth_util->offset + i_stop > synth_util->frame_count){
6976 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
6977 }
6978
6979 for(; i < i_stop;){
6980 ags_v8double v_buffer, v_square;
6981
6982 tmp_source = source;
6983
6984 v_buffer = (ags_v8double) {
6985 (gdouble) *(tmp_source),
6986 (gdouble) *(tmp_source += synth_util->source_stride),
6987 (gdouble) *(tmp_source += synth_util->source_stride),
6988 (gdouble) *(tmp_source += synth_util->source_stride),
6989 (gdouble) *(tmp_source += synth_util->source_stride),
6990 (gdouble) *(tmp_source += synth_util->source_stride),
6991 (gdouble) *(tmp_source += synth_util->source_stride),
6992 (gdouble) *(tmp_source += synth_util->source_stride)
6993 };
6994
6995 v_square = (ags_v8double) {
6996 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6997 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6998 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
6999 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7000 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7001 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7002 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7003 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7004 };
7005
7006 i++;
7007
7008 v_square *= volume;
7009
7010 v_buffer += v_square;
7011
7012 *(source) = (gdouble) v_buffer[0];
7013 *(source += synth_util->source_stride) = (gdouble) v_buffer[1];
7014 *(source += synth_util->source_stride) = (gdouble) v_buffer[2];
7015 *(source += synth_util->source_stride) = (gdouble) v_buffer[3];
7016 *(source += synth_util->source_stride) = (gdouble) v_buffer[4];
7017 *(source += synth_util->source_stride) = (gdouble) v_buffer[5];
7018 *(source += synth_util->source_stride) = (gdouble) v_buffer[6];
7019 *(source += synth_util->source_stride) = (gdouble) v_buffer[7];
7020
7021 source += synth_util->source_stride;
7022 }
7023 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
7024 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7025
7026 if(synth_util->offset + i_stop > synth_util->frame_count){
7027 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7028 }
7029
7030 for(; i < i_stop;){
7031 double ret_v_buffer[8], tmp_ret_v_buffer[8];
7032
7033 tmp_source = source;
7034
7035 double v_buffer[] = {
7036 (double) *(tmp_source),
7037 (double) *(tmp_source += synth_util->source_stride),
7038 (double) *(tmp_source += synth_util->source_stride),
7039 (double) *(tmp_source += synth_util->source_stride),
7040 (double) *(tmp_source += synth_util->source_stride),
7041 (double) *(tmp_source += synth_util->source_stride),
7042 (double) *(tmp_source += synth_util->source_stride),
7043 (double) *(tmp_source += synth_util->source_stride)};
7044 double v_square[] = {
7045 ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7046 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7047 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7048 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7049 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7050 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7051 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume,
7052 ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0) ? 1.0: -1.0) * volume};
7053
7054 double v_volume[] = {(double) volume};
7055
7056 i++;
7057
7058 vDSP_vmulD(v_square, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
7059 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
7060
7061 *(source) = (gdouble) ret_v_buffer[0];
7062 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[1];
7063 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[2];
7064 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[3];
7065 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[4];
7066 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[5];
7067 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[6];
7068 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[7];
7069
7070 source += synth_util->source_stride;
7071 }
7072 #else
7073 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7074
7075 if(synth_util->offset + i_stop > synth_util->frame_count){
7076 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7077 }
7078
7079 for(; i < i_stop;){
7080 tmp_source = source;
7081
7082 (*source) = (gdouble) ((tmp_source)[0] + ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7083 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7084 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7085 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7086 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7087 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7088 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7089 *(source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + ((sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7090
7091 source += synth_util->source_stride;
7092 i++;
7093 }
7094 #endif
7095
7096 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
7097 source[0] = (gdouble) (source[0] + ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume));
7098
7099 source += synth_util->source_stride;
7100 i++;
7101 }
7102 }
7103
7104 /**
7105 * ags_synth_util_compute_square_complex:
7106 * @synth_util: the #AgsSynthUtil-struct
7107 *
7108 * Compute square synth of complex data.
7109 *
7110 * Since: 3.9.3
7111 */
7112 void
ags_synth_util_compute_square_complex(AgsSynthUtil * synth_util)7113 ags_synth_util_compute_square_complex(AgsSynthUtil *synth_util)
7114 {
7115 AgsComplex *source;
7116
7117 gdouble volume;
7118 guint i;
7119
7120 if(synth_util == NULL ||
7121 synth_util->source == NULL){
7122 return;
7123 }
7124
7125 source = synth_util->source;
7126
7127 volume = synth_util->volume;
7128
7129 i = 0;
7130
7131 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
7132 ags_complex_set(source,
7133 (gdouble) (ags_complex_get(source) + ((sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= 0.0 ? 1.0: -1.0) * volume)));
7134
7135 source += synth_util->source_stride;
7136 i++;
7137 }
7138 }
7139
7140 /**
7141 * ags_synth_util_compute_square:
7142 * @synth_util: the #AgsSynthUtil-struct
7143 *
7144 * Compute square synth.
7145 *
7146 * Since: 3.9.3
7147 */
7148 void
ags_synth_util_compute_square(AgsSynthUtil * synth_util)7149 ags_synth_util_compute_square(AgsSynthUtil *synth_util)
7150 {
7151 if(synth_util == NULL ||
7152 synth_util->source == NULL){
7153 return;
7154 }
7155
7156 switch(synth_util->audio_buffer_util_format){
7157 case AGS_AUDIO_BUFFER_UTIL_S8:
7158 {
7159 ags_synth_util_compute_square_s8(synth_util);
7160 }
7161 break;
7162 case AGS_AUDIO_BUFFER_UTIL_S16:
7163 {
7164 ags_synth_util_compute_square_s16(synth_util);
7165 }
7166 break;
7167 case AGS_AUDIO_BUFFER_UTIL_S24:
7168 {
7169 ags_synth_util_compute_square_s24(synth_util);
7170 }
7171 break;
7172 case AGS_AUDIO_BUFFER_UTIL_S32:
7173 {
7174 ags_synth_util_compute_square_s32(synth_util);
7175 }
7176 break;
7177 case AGS_AUDIO_BUFFER_UTIL_S64:
7178 {
7179 ags_synth_util_compute_square_s64(synth_util);
7180 }
7181 break;
7182 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
7183 {
7184 ags_synth_util_compute_square_float(synth_util);
7185 }
7186 break;
7187 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
7188 {
7189 ags_synth_util_compute_square_double(synth_util);
7190 }
7191 break;
7192 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
7193 {
7194 ags_synth_util_compute_square_complex(synth_util);
7195 }
7196 break;
7197 }
7198 }
7199
7200 /**
7201 * ags_synth_util_square_s8:
7202 * @buffer: the audio buffer
7203 * @freq: the frequency of the square wave
7204 * @phase: the phase of the square wave
7205 * @volume: the volume of the square wave
7206 * @samplerate: the samplerate
7207 * @offset: start frame
7208 * @n_frames: generate n frames
7209 *
7210 * Generate square wave.
7211 *
7212 * Since: 3.0.0
7213 */
7214 void
ags_synth_util_square_s8(gint8 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)7215 ags_synth_util_square_s8(gint8 *buffer,
7216 gdouble freq, gdouble phase, gdouble volume,
7217 guint samplerate,
7218 guint offset, guint n_frames)
7219 {
7220 static const gdouble scale = 127.0;
7221 guint i;
7222
7223 if(buffer == NULL){
7224 return;
7225 }
7226
7227 for(i = offset; i < offset + n_frames; i++){
7228 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= 0.0){
7229 buffer[i] = (gint8) (0xff & ((gint16) buffer[i] + (gint16) (1.0 * scale * volume)));
7230 }else{
7231 buffer[i] = (gint8) (0xff & ((gint16) buffer[i] + (gint16) (-1.0 * scale * volume)));
7232 }
7233 }
7234 }
7235
7236 /**
7237 * ags_synth_util_square_s16:
7238 * @buffer: the audio buffer
7239 * @freq: the frequency of the square wave
7240 * @phase: the phase of the square wave
7241 * @volume: the volume of the square wave
7242 * @samplerate: the samplerate
7243 * @offset: start frame
7244 * @n_frames: generate n frames
7245 *
7246 * Generate square wave.
7247 *
7248 * Since: 3.0.0
7249 */
7250 void
ags_synth_util_square_s16(gint16 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)7251 ags_synth_util_square_s16(gint16 *buffer,
7252 gdouble freq, gdouble phase, gdouble volume,
7253 guint samplerate,
7254 guint offset, guint n_frames)
7255 {
7256 static const gdouble scale = 32767.0;
7257 guint i;
7258
7259 if(buffer == NULL){
7260 return;
7261 }
7262
7263 for(i = offset; i < offset + n_frames; i++){
7264 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= 0.0){
7265 buffer[i] = (gint16) (0xffff & ((gint32) buffer[i] + (gint32) (1.0 * scale * volume)));
7266 }else{
7267 buffer[i] = (gint16) (0xffff & ((gint32) buffer[i] + (gint32) (-1.0 * scale * volume)));
7268 }
7269 }
7270 }
7271
7272 /**
7273 * ags_synth_util_square_s24:
7274 * @buffer: the audio buffer
7275 * @freq: the frequency of the square wave
7276 * @phase: the phase of the square wave
7277 * @volume: the volume of the square wave
7278 * @samplerate: the samplerate
7279 * @offset: start frame
7280 * @n_frames: generate n frames
7281 *
7282 * Generate square wave.
7283 *
7284 * Since: 3.0.0
7285 */
7286 void
ags_synth_util_square_s24(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)7287 ags_synth_util_square_s24(gint32 *buffer,
7288 gdouble freq, gdouble phase, gdouble volume,
7289 guint samplerate,
7290 guint offset, guint n_frames)
7291 {
7292 static const gdouble scale = 8388607.0;
7293 guint i;
7294
7295 if(buffer == NULL){
7296 return;
7297 }
7298
7299 for(i = offset; i < offset + n_frames; i++){
7300 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= 0.0){
7301 buffer[i] = (gint32) (0xffffffff & ((gint32) buffer[i] + (gint32) (1.0 * scale * volume)));
7302 }else{
7303 buffer[i] = (gint32) (0xffffffff & ((gint32) buffer[i] + (gint32) (-1.0 * scale * volume)));
7304 }
7305 }
7306 }
7307
7308 /**
7309 * ags_synth_util_square_s32:
7310 * @buffer: the audio buffer
7311 * @freq: the frequency of the square wave
7312 * @phase: the phase of the square wave
7313 * @volume: the volume of the square wave
7314 * @samplerate: the samplerate
7315 * @offset: start frame
7316 * @n_frames: generate n frames
7317 *
7318 * Generate square wave.
7319 *
7320 * Since: 3.0.0
7321 */
7322 void
ags_synth_util_square_s32(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)7323 ags_synth_util_square_s32(gint32 *buffer,
7324 gdouble freq, gdouble phase, gdouble volume,
7325 guint samplerate,
7326 guint offset, guint n_frames)
7327 {
7328 static const gdouble scale = 214748363.0;
7329 guint i;
7330
7331 if(buffer == NULL){
7332 return;
7333 }
7334
7335 for(i = offset; i < offset + n_frames; i++){
7336 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= 0.0){
7337 buffer[i] = (gint32) (0xffffffff & ((gint64) buffer[i] + (gint64) (1.0 * scale * volume)));
7338 }else{
7339 buffer[i] = (gint32) (0xffffffff & ((gint64) buffer[i] + (gint64) (-1.0 * scale * volume)));
7340 }
7341 }
7342 }
7343
7344 /**
7345 * ags_synth_util_square_s64:
7346 * @buffer: the audio buffer
7347 * @freq: the frequency of the square wave
7348 * @phase: the phase of the square wave
7349 * @volume: the volume of the square wave
7350 * @samplerate: the samplerate
7351 * @offset: start frame
7352 * @n_frames: generate n frames
7353 *
7354 * Generate square wave.
7355 *
7356 * Since: 3.0.0
7357 */
7358 void
ags_synth_util_square_s64(gint64 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)7359 ags_synth_util_square_s64(gint64 *buffer,
7360 gdouble freq, gdouble phase, gdouble volume,
7361 guint samplerate,
7362 guint offset, guint n_frames)
7363 {
7364 static const gdouble scale = 9223372036854775807.0;
7365 guint i;
7366
7367 if(buffer == NULL){
7368 return;
7369 }
7370
7371 for(i = offset; i < offset + n_frames; i++){
7372 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= 0.0){
7373 buffer[i] = (gint64) (0xffffffffffffffff & ((gint64) buffer[i] + (gint64) (1.0 * scale * volume)));
7374 }else{
7375 buffer[i] = (gint64) (0xffffffffffffffff & ((gint64) buffer[i] + (gint64) (-1.0 * scale * volume)));
7376 }
7377 }
7378 }
7379
7380 /**
7381 * ags_synth_util_square_float:
7382 * @buffer: the audio buffer
7383 * @freq: the frequency of the square wave
7384 * @phase: the phase of the square wave
7385 * @volume: the volume of the square wave
7386 * @samplerate: the samplerate
7387 * @offset: start frame
7388 * @n_frames: generate n frames
7389 *
7390 * Generate square wave.
7391 *
7392 * Since: 3.0.0
7393 */
7394 void
ags_synth_util_square_float(gfloat * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)7395 ags_synth_util_square_float(gfloat *buffer,
7396 gdouble freq, gdouble phase, gdouble volume,
7397 guint samplerate,
7398 guint offset, guint n_frames)
7399 {
7400 guint i;
7401
7402 if(buffer == NULL){
7403 return;
7404 }
7405
7406 for(i = offset; i < offset + n_frames; i++){
7407 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= 0.0){
7408 buffer[i] = (buffer[i] + (1.0 * volume));
7409 }else{
7410 buffer[i] = (buffer[i] + (-1.0 * volume));
7411 }
7412 }
7413 }
7414
7415 /**
7416 * ags_synth_util_square_double:
7417 * @buffer: the audio buffer
7418 * @freq: the frequency of the square wave
7419 * @phase: the phase of the square wave
7420 * @volume: the volume of the square wave
7421 * @samplerate: the samplerate
7422 * @offset: start frame
7423 * @n_frames: generate n frames
7424 *
7425 * Generate square wave.
7426 *
7427 * Since: 3.0.0
7428 */
7429 void
ags_synth_util_square_double(gdouble * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)7430 ags_synth_util_square_double(gdouble *buffer,
7431 gdouble freq, gdouble phase, gdouble volume,
7432 guint samplerate,
7433 guint offset, guint n_frames)
7434 {
7435 guint i;
7436
7437 if(buffer == NULL){
7438 return;
7439 }
7440
7441 for(i = offset; i < offset + n_frames; i++){
7442 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= 0.0){
7443 buffer[i] = (buffer[i] + (1.0 * volume));
7444 }else{
7445 buffer[i] = (buffer[i] + (-1.0 * volume));
7446 }
7447 }
7448 }
7449
7450 /**
7451 * ags_synth_util_square_complex:
7452 * @buffer: the audio buffer
7453 * @freq: the frequency of the square wave
7454 * @phase: the phase of the square wave
7455 * @volume: the volume of the square wave
7456 * @samplerate: the samplerate
7457 * @offset: start frame
7458 * @n_frames: generate n frames
7459 *
7460 * Generate square wave.
7461 *
7462 * Since: 3.0.0
7463 */
7464 void
ags_synth_util_square_complex(AgsComplex * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)7465 ags_synth_util_square_complex(AgsComplex *buffer,
7466 gdouble freq, gdouble phase, gdouble volume,
7467 guint samplerate,
7468 guint offset, guint n_frames)
7469 {
7470 AgsComplex *c_ptr;
7471 AgsComplex **c_ptr_ptr;
7472
7473 double y;
7474
7475 guint i_stop;
7476 guint i;
7477
7478 if(buffer == NULL){
7479 return;
7480 }
7481
7482 c_ptr = buffer;
7483 c_ptr_ptr = &c_ptr;
7484
7485 i_stop = offset + n_frames;
7486
7487 for(i = offset; i < i_stop; i++, c_ptr++){
7488 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= 0.0){
7489 y = (1.0 * volume);
7490 }else{
7491 y = (-1.0 * volume);
7492 }
7493
7494 AGS_AUDIO_BUFFER_UTIL_DOUBLE_TO_COMPLEX(y, c_ptr_ptr);
7495 }
7496 }
7497
7498 /**
7499 * ags_synth_util_square:
7500 * @buffer: the audio buffer
7501 * @freq: the frequency of the square wave
7502 * @phase: the phase of the square wave
7503 * @volume: the volume of the square wave
7504 * @samplerate: the samplerate
7505 * @audio_buffer_util_format: the audio data format
7506 * @offset: start frame
7507 * @n_frames: generate n frames
7508 *
7509 * Generate square wave.
7510 *
7511 * Since: 3.0.0
7512 */
7513 void
ags_synth_util_square(void * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint audio_buffer_util_format,guint offset,guint n_frames)7514 ags_synth_util_square(void *buffer,
7515 gdouble freq, gdouble phase, gdouble volume,
7516 guint samplerate, guint audio_buffer_util_format,
7517 guint offset, guint n_frames)
7518 {
7519 if(buffer == NULL){
7520 return;
7521 }
7522
7523 switch(audio_buffer_util_format){
7524 case AGS_AUDIO_BUFFER_UTIL_S8:
7525 {
7526 ags_synth_util_square_s8((gint8 *) buffer,
7527 freq, phase, volume,
7528 samplerate,
7529 offset, n_frames);
7530 }
7531 break;
7532 case AGS_AUDIO_BUFFER_UTIL_S16:
7533 {
7534 ags_synth_util_square_s16((gint16 *) buffer,
7535 freq, phase, volume,
7536 samplerate,
7537 offset, n_frames);
7538 }
7539 break;
7540 case AGS_AUDIO_BUFFER_UTIL_S24:
7541 {
7542 ags_synth_util_square_s24((gint32 *) buffer,
7543 freq, phase, volume,
7544 samplerate,
7545 offset, n_frames);
7546 }
7547 break;
7548 case AGS_AUDIO_BUFFER_UTIL_S32:
7549 {
7550 ags_synth_util_square_s32((gint32 *) buffer,
7551 freq, phase, volume,
7552 samplerate,
7553 offset, n_frames);
7554 }
7555 break;
7556 case AGS_AUDIO_BUFFER_UTIL_S64:
7557 {
7558 ags_synth_util_square_s64((gint64 *) buffer,
7559 freq, phase, volume,
7560 samplerate,
7561 offset, n_frames);
7562 }
7563 break;
7564 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
7565 {
7566 ags_synth_util_square_float((gfloat *) buffer,
7567 freq, phase, volume,
7568 samplerate,
7569 offset, n_frames);
7570 }
7571 break;
7572 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
7573 {
7574 ags_synth_util_square_double((gdouble *) buffer,
7575 freq, phase, volume,
7576 samplerate,
7577 offset, n_frames);
7578 }
7579 break;
7580 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
7581 {
7582 ags_synth_util_square_complex((AgsComplex *) buffer,
7583 freq, phase, volume,
7584 samplerate,
7585 offset, n_frames);
7586 }
7587 break;
7588 default:
7589 {
7590 g_warning("ags_synth_util_square() - unsupported format");
7591 }
7592 }
7593 }
7594
7595 /**
7596 * ags_synth_util_compute_impulse_s8:
7597 * @synth_util: the #AgsSynthUtil-struct
7598 *
7599 * Compute impulse synth of signed 8 bit data.
7600 *
7601 * Since: 3.9.3
7602 */
7603 void
ags_synth_util_compute_impulse_s8(AgsSynthUtil * synth_util)7604 ags_synth_util_compute_impulse_s8(AgsSynthUtil *synth_util)
7605 {
7606 gint8 *source, *tmp_source;
7607
7608 gdouble volume;
7609 guint i, i_stop;
7610
7611 static const gdouble scale = 127.0;
7612
7613 if(synth_util == NULL ||
7614 synth_util->source == NULL){
7615 return;
7616 }
7617
7618 source = synth_util->source;
7619
7620 volume = scale * synth_util->volume;
7621
7622 i = 0;
7623
7624 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
7625 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7626
7627 if(synth_util->offset + i_stop > synth_util->frame_count){
7628 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7629 }
7630
7631 for(; i < i_stop;){
7632 ags_v8double v_buffer, v_impulse;
7633
7634 tmp_source = source;
7635
7636 v_buffer = (ags_v8double) {
7637 (gdouble) *(tmp_source),
7638 (gdouble) *(tmp_source += synth_util->source_stride),
7639 (gdouble) *(tmp_source += synth_util->source_stride),
7640 (gdouble) *(tmp_source += synth_util->source_stride),
7641 (gdouble) *(tmp_source += synth_util->source_stride),
7642 (gdouble) *(tmp_source += synth_util->source_stride),
7643 (gdouble) *(tmp_source += synth_util->source_stride),
7644 (gdouble) *(tmp_source += synth_util->source_stride)
7645 };
7646
7647 v_impulse = (ags_v8double) {
7648 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7649 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7650 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7651 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7652 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7653 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7654 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7655 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume
7656 };
7657
7658 i++;
7659
7660 v_impulse *= volume;
7661
7662 v_buffer += v_impulse;
7663
7664 *(source) = (gint8) v_buffer[0];
7665 *(source += synth_util->source_stride) = (gint8) v_buffer[1];
7666 *(source += synth_util->source_stride) = (gint8) v_buffer[2];
7667 *(source += synth_util->source_stride) = (gint8) v_buffer[3];
7668 *(source += synth_util->source_stride) = (gint8) v_buffer[4];
7669 *(source += synth_util->source_stride) = (gint8) v_buffer[5];
7670 *(source += synth_util->source_stride) = (gint8) v_buffer[6];
7671 *(source += synth_util->source_stride) = (gint8) v_buffer[7];
7672
7673 source += synth_util->source_stride;
7674 }
7675 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
7676 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7677
7678 if(synth_util->offset + i_stop > synth_util->frame_count){
7679 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7680 }
7681
7682 for(; i < i_stop;){
7683 double ret_v_buffer[8], tmp_ret_v_buffer[8];
7684
7685 tmp_source = source;
7686
7687 double v_buffer[] = {
7688 (double) *(tmp_source),
7689 (double) *(tmp_source += synth_util->source_stride),
7690 (double) *(tmp_source += synth_util->source_stride),
7691 (double) *(tmp_source += synth_util->source_stride),
7692 (double) *(tmp_source += synth_util->source_stride),
7693 (double) *(tmp_source += synth_util->source_stride),
7694 (double) *(tmp_source += synth_util->source_stride),
7695 (double) *(tmp_source += synth_util->source_stride)};
7696 double v_impulse[] = {
7697 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7698 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7699 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7700 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7701 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7702 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7703 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7704 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume};
7705
7706 double v_volume[] = {(double) volume};
7707
7708 i++;
7709
7710 vDSP_vmulD(v_impulse, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
7711 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
7712
7713 *(source) = (gint8) ret_v_buffer[0];
7714 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[1];
7715 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[2];
7716 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[3];
7717 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[4];
7718 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[5];
7719 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[6];
7720 *(source += synth_util->source_stride) = (gint8) ret_v_buffer[7];
7721
7722 source += synth_util->source_stride;
7723 }
7724 #else
7725 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7726
7727 if(synth_util->offset + i_stop > synth_util->frame_count){
7728 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7729 }
7730
7731 for(; i < i_stop;){
7732 tmp_source = source;
7733
7734 (*source) = (gint8) ((gint16) (tmp_source)[0] + (gint16) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7735 (*source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7736 (*source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7737 (*source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7738 (*source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7739 (*source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7740 (*source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7741 (*source += synth_util->source_stride) = (gint8) ((gint16) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7742
7743 source += synth_util->source_stride;
7744 i++;
7745 }
7746 #endif
7747
7748 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
7749 source[0] = (gint8) ((gint16) source[0] + (gint16) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7750
7751 source += synth_util->source_stride;
7752 i++;
7753 }
7754 }
7755
7756 /**
7757 * ags_synth_util_compute_impulse_s16:
7758 * @synth_util: the #AgsSynthUtil-struct
7759 *
7760 * Compute impulse synth of signed 16 bit data.
7761 *
7762 * Since: 3.9.3
7763 */
7764 void
ags_synth_util_compute_impulse_s16(AgsSynthUtil * synth_util)7765 ags_synth_util_compute_impulse_s16(AgsSynthUtil *synth_util)
7766 {
7767 gint16 *source, *tmp_source;
7768
7769 gdouble volume;
7770 guint i, i_stop;
7771
7772 static const gdouble scale = 32767.0;
7773
7774 if(synth_util == NULL ||
7775 synth_util->source == NULL){
7776 return;
7777 }
7778
7779 source = synth_util->source;
7780
7781 volume = scale * synth_util->volume;
7782
7783 i = 0;
7784
7785 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
7786 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7787
7788 if(synth_util->offset + i_stop > synth_util->frame_count){
7789 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7790 }
7791
7792 for(; i < i_stop;){
7793 ags_v8double v_buffer, v_impulse;
7794
7795 tmp_source = source;
7796
7797 v_buffer = (ags_v8double) {
7798 (gdouble) *(tmp_source),
7799 (gdouble) *(tmp_source += synth_util->source_stride),
7800 (gdouble) *(tmp_source += synth_util->source_stride),
7801 (gdouble) *(tmp_source += synth_util->source_stride),
7802 (gdouble) *(tmp_source += synth_util->source_stride),
7803 (gdouble) *(tmp_source += synth_util->source_stride),
7804 (gdouble) *(tmp_source += synth_util->source_stride),
7805 (gdouble) *(tmp_source += synth_util->source_stride)
7806 };
7807
7808 v_impulse = (ags_v8double) {
7809 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7810 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7811 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7812 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7813 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7814 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7815 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7816 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume
7817 };
7818
7819 i++;
7820
7821 v_impulse *= volume;
7822
7823 v_buffer += v_impulse;
7824
7825 *(source) = (gint16) v_buffer[0];
7826 *(source += synth_util->source_stride) = (gint16) v_buffer[1];
7827 *(source += synth_util->source_stride) = (gint16) v_buffer[2];
7828 *(source += synth_util->source_stride) = (gint16) v_buffer[3];
7829 *(source += synth_util->source_stride) = (gint16) v_buffer[4];
7830 *(source += synth_util->source_stride) = (gint16) v_buffer[5];
7831 *(source += synth_util->source_stride) = (gint16) v_buffer[6];
7832 *(source += synth_util->source_stride) = (gint16) v_buffer[7];
7833
7834 source += synth_util->source_stride;
7835 }
7836 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
7837 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7838
7839 if(synth_util->offset + i_stop > synth_util->frame_count){
7840 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7841 }
7842
7843 for(; i < i_stop;){
7844 double ret_v_buffer[8], tmp_ret_v_buffer[8];
7845
7846 tmp_source = source;
7847
7848 double v_buffer[] = {
7849 (double) *(tmp_source),
7850 (double) *(tmp_source += synth_util->source_stride),
7851 (double) *(tmp_source += synth_util->source_stride),
7852 (double) *(tmp_source += synth_util->source_stride),
7853 (double) *(tmp_source += synth_util->source_stride),
7854 (double) *(tmp_source += synth_util->source_stride),
7855 (double) *(tmp_source += synth_util->source_stride),
7856 (double) *(tmp_source += synth_util->source_stride)};
7857 double v_impulse[] = {
7858 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7859 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7860 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7861 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7862 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7863 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7864 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7865 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume};
7866
7867 double v_volume[] = {(double) volume};
7868
7869 i++;
7870
7871 vDSP_vmulD(v_impulse, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
7872 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
7873
7874 *(source) = (gint16) ret_v_buffer[0];
7875 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[1];
7876 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[2];
7877 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[3];
7878 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[4];
7879 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[5];
7880 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[6];
7881 *(source += synth_util->source_stride) = (gint16) ret_v_buffer[7];
7882
7883 source += synth_util->source_stride;
7884 }
7885 #else
7886 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7887
7888 if(synth_util->offset + i_stop > synth_util->frame_count){
7889 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7890 }
7891
7892 for(; i < i_stop;){
7893 tmp_source = source;
7894
7895 (*source) = (gint16) ((gint32) (tmp_source)[0] + (gint32) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7896 (*source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7897 (*source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7898 (*source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7899 (*source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7900 (*source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7901 (*source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7902 (*source += synth_util->source_stride) = (gint16) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7903
7904 source += synth_util->source_stride;
7905 i++;
7906 }
7907 #endif
7908
7909 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
7910 source[0] = (gint16) ((gint32) source[0] + (gint32) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
7911
7912 source += synth_util->source_stride;
7913 i++;
7914 }
7915 }
7916
7917 /**
7918 * ags_synth_util_compute_impulse_s24:
7919 * @synth_util: the #AgsSynthUtil-struct
7920 *
7921 * Compute impulse synth of signed 24 bit data.
7922 *
7923 * Since: 3.9.3
7924 */
7925 void
ags_synth_util_compute_impulse_s24(AgsSynthUtil * synth_util)7926 ags_synth_util_compute_impulse_s24(AgsSynthUtil *synth_util)
7927 {
7928 gint32 *source, *tmp_source;
7929
7930 gdouble volume;
7931 guint i, i_stop;
7932
7933 static const gdouble scale = 8388607.0;
7934
7935 if(synth_util == NULL ||
7936 synth_util->source == NULL){
7937 return;
7938 }
7939
7940 source = synth_util->source;
7941
7942 volume = scale * synth_util->volume;
7943
7944 i = 0;
7945
7946 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
7947 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7948
7949 if(synth_util->offset + i_stop > synth_util->frame_count){
7950 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
7951 }
7952
7953 for(; i < i_stop;){
7954 ags_v8double v_buffer, v_impulse;
7955
7956 tmp_source = source;
7957
7958 v_buffer = (ags_v8double) {
7959 (gdouble) *(tmp_source),
7960 (gdouble) *(tmp_source += synth_util->source_stride),
7961 (gdouble) *(tmp_source += synth_util->source_stride),
7962 (gdouble) *(tmp_source += synth_util->source_stride),
7963 (gdouble) *(tmp_source += synth_util->source_stride),
7964 (gdouble) *(tmp_source += synth_util->source_stride),
7965 (gdouble) *(tmp_source += synth_util->source_stride),
7966 (gdouble) *(tmp_source += synth_util->source_stride)
7967 };
7968
7969 v_impulse = (ags_v8double) {
7970 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7971 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7972 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7973 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7974 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7975 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7976 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
7977 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume
7978 };
7979
7980 i++;
7981
7982 v_impulse *= volume;
7983
7984 v_buffer += v_impulse;
7985
7986 *(source) = (gint32) v_buffer[0];
7987 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
7988 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
7989 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
7990 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
7991 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
7992 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
7993 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
7994
7995 source += synth_util->source_stride;
7996 }
7997 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
7998 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
7999
8000 if(synth_util->offset + i_stop > synth_util->frame_count){
8001 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8002 }
8003
8004 for(; i < i_stop;){
8005 double ret_v_buffer[8], tmp_ret_v_buffer[8];
8006
8007 tmp_source = source;
8008
8009 double v_buffer[] = {
8010 (double) *(tmp_source),
8011 (double) *(tmp_source += synth_util->source_stride),
8012 (double) *(tmp_source += synth_util->source_stride),
8013 (double) *(tmp_source += synth_util->source_stride),
8014 (double) *(tmp_source += synth_util->source_stride),
8015 (double) *(tmp_source += synth_util->source_stride),
8016 (double) *(tmp_source += synth_util->source_stride),
8017 (double) *(tmp_source += synth_util->source_stride)};
8018 double v_impulse[] = {
8019 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8020 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8021 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8022 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8023 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8024 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8025 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8026 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume};
8027
8028 double v_volume[] = {(double) volume};
8029
8030 i++;
8031
8032 vDSP_vmulD(v_impulse, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
8033 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
8034
8035 *(source) = (gint32) ret_v_buffer[0];
8036 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
8037 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
8038 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
8039 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
8040 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
8041 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
8042 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
8043
8044 source += synth_util->source_stride;
8045 }
8046 #else
8047 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8048
8049 if(synth_util->offset + i_stop > synth_util->frame_count){
8050 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8051 }
8052
8053 for(; i < i_stop;){
8054 tmp_source = source;
8055
8056 (*source) = (gint32) ((gint32) (tmp_source)[0] + (gint32) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8057 (*source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8058 (*source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8059 (*source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8060 (*source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8061 (*source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8062 (*source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8063 (*source += synth_util->source_stride) = (gint32) ((gint32) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8064
8065 source += synth_util->source_stride;
8066 i++;
8067 }
8068 #endif
8069
8070 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
8071 source[0] = (gint32) ((gint32) source[0] + (gint32) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8072
8073 source += synth_util->source_stride;
8074 i++;
8075 }
8076 }
8077
8078 /**
8079 * ags_synth_util_compute_impulse_s32:
8080 * @synth_util: the #AgsSynthUtil-struct
8081 *
8082 * Compute impulse synth of signed 32 bit data.
8083 *
8084 * Since: 3.9.3
8085 */
8086 void
ags_synth_util_compute_impulse_s32(AgsSynthUtil * synth_util)8087 ags_synth_util_compute_impulse_s32(AgsSynthUtil *synth_util)
8088 {
8089 gint32 *source, *tmp_source;
8090
8091 gdouble volume;
8092 guint i, i_stop;
8093
8094 static const gdouble scale = 214748363.0;
8095
8096 if(synth_util == NULL ||
8097 synth_util->source == NULL){
8098 return;
8099 }
8100
8101 source = synth_util->source;
8102
8103 volume = scale * synth_util->volume;
8104
8105 i = 0;
8106
8107 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
8108 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8109
8110 if(synth_util->offset + i_stop > synth_util->frame_count){
8111 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8112 }
8113
8114 for(; i < i_stop;){
8115 ags_v8double v_buffer, v_impulse;
8116
8117 tmp_source = source;
8118
8119 v_buffer = (ags_v8double) {
8120 (gdouble) *(tmp_source),
8121 (gdouble) *(tmp_source += synth_util->source_stride),
8122 (gdouble) *(tmp_source += synth_util->source_stride),
8123 (gdouble) *(tmp_source += synth_util->source_stride),
8124 (gdouble) *(tmp_source += synth_util->source_stride),
8125 (gdouble) *(tmp_source += synth_util->source_stride),
8126 (gdouble) *(tmp_source += synth_util->source_stride),
8127 (gdouble) *(tmp_source += synth_util->source_stride)
8128 };
8129
8130 v_impulse = (ags_v8double) {
8131 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8132 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8133 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8134 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8135 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8136 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8137 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8138 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume
8139 };
8140
8141 i++;
8142
8143 v_impulse *= volume;
8144
8145 v_buffer += v_impulse;
8146
8147 *(source) = (gint32) v_buffer[0];
8148 *(source += synth_util->source_stride) = (gint32) v_buffer[1];
8149 *(source += synth_util->source_stride) = (gint32) v_buffer[2];
8150 *(source += synth_util->source_stride) = (gint32) v_buffer[3];
8151 *(source += synth_util->source_stride) = (gint32) v_buffer[4];
8152 *(source += synth_util->source_stride) = (gint32) v_buffer[5];
8153 *(source += synth_util->source_stride) = (gint32) v_buffer[6];
8154 *(source += synth_util->source_stride) = (gint32) v_buffer[7];
8155
8156 source += synth_util->source_stride;
8157 }
8158 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
8159 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8160
8161 if(synth_util->offset + i_stop > synth_util->frame_count){
8162 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8163 }
8164
8165 for(; i < i_stop;){
8166 double ret_v_buffer[8], tmp_ret_v_buffer[8];
8167
8168 tmp_source = source;
8169
8170 double v_buffer[] = {
8171 (double) *(tmp_source),
8172 (double) *(tmp_source += synth_util->source_stride),
8173 (double) *(tmp_source += synth_util->source_stride),
8174 (double) *(tmp_source += synth_util->source_stride),
8175 (double) *(tmp_source += synth_util->source_stride),
8176 (double) *(tmp_source += synth_util->source_stride),
8177 (double) *(tmp_source += synth_util->source_stride),
8178 (double) *(tmp_source += synth_util->source_stride)};
8179 double v_impulse[] = {
8180 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8181 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8182 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8183 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8184 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8185 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8186 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8187 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume};
8188
8189 double v_volume[] = {(double) volume};
8190
8191 i++;
8192
8193 vDSP_vmulD(v_impulse, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
8194 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
8195
8196 *(source) = (gint32) ret_v_buffer[0];
8197 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[1];
8198 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[2];
8199 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[3];
8200 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[4];
8201 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[5];
8202 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[6];
8203 *(source += synth_util->source_stride) = (gint32) ret_v_buffer[7];
8204
8205 source += synth_util->source_stride;
8206 }
8207 #else
8208 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8209
8210 if(synth_util->offset + i_stop > synth_util->frame_count){
8211 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8212 }
8213
8214 for(; i < i_stop;){
8215 tmp_source = source;
8216
8217 (*source) = (gint32) ((gint64) (tmp_source)[0] + (gint64) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8218 (*source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8219 (*source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8220 (*source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8221 (*source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8222 (*source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8223 (*source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8224 (*source += synth_util->source_stride) = (gint32) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8225
8226 source += synth_util->source_stride;
8227 i++;
8228 }
8229 #endif
8230
8231 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
8232 source[0] = (gint32) ((gint64) source[0] + (gint64) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8233
8234 source += synth_util->source_stride;
8235 i++;
8236 }
8237 }
8238
8239 /**
8240 * ags_synth_util_compute_impulse_s64:
8241 * @synth_util: the #AgsSynthUtil-struct
8242 *
8243 * Compute impulse synth of signed 64 bit data.
8244 *
8245 * Since: 3.9.3
8246 */
8247 void
ags_synth_util_compute_impulse_s64(AgsSynthUtil * synth_util)8248 ags_synth_util_compute_impulse_s64(AgsSynthUtil *synth_util)
8249 {
8250 gint64 *source, *tmp_source;
8251
8252 gdouble volume;
8253 guint i, i_stop;
8254
8255 static const gdouble scale = 9223372036854775807.0;
8256
8257 if(synth_util == NULL ||
8258 synth_util->source == NULL){
8259 return;
8260 }
8261
8262 source = synth_util->source;
8263
8264 volume = scale * synth_util->volume;
8265
8266 i = 0;
8267
8268 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
8269 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8270
8271 if(synth_util->offset + i_stop > synth_util->frame_count){
8272 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8273 }
8274
8275 for(; i < i_stop;){
8276 ags_v8double v_buffer, v_impulse;
8277
8278 tmp_source = source;
8279
8280 v_buffer = (ags_v8double) {
8281 (gdouble) *(tmp_source),
8282 (gdouble) *(tmp_source += synth_util->source_stride),
8283 (gdouble) *(tmp_source += synth_util->source_stride),
8284 (gdouble) *(tmp_source += synth_util->source_stride),
8285 (gdouble) *(tmp_source += synth_util->source_stride),
8286 (gdouble) *(tmp_source += synth_util->source_stride),
8287 (gdouble) *(tmp_source += synth_util->source_stride),
8288 (gdouble) *(tmp_source += synth_util->source_stride)
8289 };
8290
8291 v_impulse = (ags_v8double) {
8292 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8293 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8294 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8295 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8296 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8297 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8298 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8299 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume
8300 };
8301
8302 i++;
8303
8304 v_impulse *= volume;
8305
8306 v_buffer += v_impulse;
8307
8308 *(source) = (gint64) v_buffer[0];
8309 *(source += synth_util->source_stride) = (gint64) v_buffer[1];
8310 *(source += synth_util->source_stride) = (gint64) v_buffer[2];
8311 *(source += synth_util->source_stride) = (gint64) v_buffer[3];
8312 *(source += synth_util->source_stride) = (gint64) v_buffer[4];
8313 *(source += synth_util->source_stride) = (gint64) v_buffer[5];
8314 *(source += synth_util->source_stride) = (gint64) v_buffer[6];
8315 *(source += synth_util->source_stride) = (gint64) v_buffer[7];
8316
8317 source += synth_util->source_stride;
8318 }
8319 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
8320 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8321
8322 if(synth_util->offset + i_stop > synth_util->frame_count){
8323 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8324 }
8325
8326 for(; i < i_stop;){
8327 double ret_v_buffer[8], tmp_ret_v_buffer[8];
8328
8329 tmp_source = source;
8330
8331 double v_buffer[] = {
8332 (double) *(tmp_source),
8333 (double) *(tmp_source += synth_util->source_stride),
8334 (double) *(tmp_source += synth_util->source_stride),
8335 (double) *(tmp_source += synth_util->source_stride),
8336 (double) *(tmp_source += synth_util->source_stride),
8337 (double) *(tmp_source += synth_util->source_stride),
8338 (double) *(tmp_source += synth_util->source_stride),
8339 (double) *(tmp_source += synth_util->source_stride)};
8340 double v_impulse[] = {
8341 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8342 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8343 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8344 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8345 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8346 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8347 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8348 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume};
8349
8350 double v_volume[] = {(double) volume};
8351
8352 i++;
8353
8354 vDSP_vmulD(v_impulse, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
8355 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
8356
8357 *(source) = (gint64) ret_v_buffer[0];
8358 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[1];
8359 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[2];
8360 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[3];
8361 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[4];
8362 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[5];
8363 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[6];
8364 *(source += synth_util->source_stride) = (gint64) ret_v_buffer[7];
8365
8366 source += synth_util->source_stride;
8367 }
8368 #else
8369 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8370
8371 if(synth_util->offset + i_stop > synth_util->frame_count){
8372 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8373 }
8374
8375 for(; i < i_stop;){
8376 tmp_source = source;
8377
8378 (*source) = (gint64) ((gint64) (tmp_source)[0] + (gint64) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8379 (*source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8380 (*source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8381 (*source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8382 (*source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8383 (*source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8384 (*source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8385 (*source += synth_util->source_stride) = (gint64) ((gint64) (tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8386
8387 source += synth_util->source_stride;
8388 i++;
8389 }
8390 #endif
8391
8392 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
8393 source[0] = (gint64) ((gint64) source[0] + (gint64) (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8394
8395 source += synth_util->source_stride;
8396 i++;
8397 }
8398 }
8399
8400 /**
8401 * ags_synth_util_compute_impulse_float:
8402 * @synth_util: the #AgsSynthUtil-struct
8403 *
8404 * Compute impulse synth of floating point data.
8405 *
8406 * Since: 3.9.3
8407 */
8408 void
ags_synth_util_compute_impulse_float(AgsSynthUtil * synth_util)8409 ags_synth_util_compute_impulse_float(AgsSynthUtil *synth_util)
8410 {
8411 gfloat *source, *tmp_source;
8412
8413 gdouble volume;
8414 guint i, i_stop;
8415
8416 if(synth_util == NULL ||
8417 synth_util->source == NULL){
8418 return;
8419 }
8420
8421 source = synth_util->source;
8422
8423 volume = synth_util->volume;
8424
8425 i = 0;
8426
8427 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
8428 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8429
8430 if(synth_util->offset + i_stop > synth_util->frame_count){
8431 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8432 }
8433
8434 for(; i < i_stop;){
8435 ags_v8double v_buffer, v_impulse;
8436
8437 tmp_source = source;
8438
8439 v_buffer = (ags_v8double) {
8440 (gdouble) *(tmp_source),
8441 (gdouble) *(tmp_source += synth_util->source_stride),
8442 (gdouble) *(tmp_source += synth_util->source_stride),
8443 (gdouble) *(tmp_source += synth_util->source_stride),
8444 (gdouble) *(tmp_source += synth_util->source_stride),
8445 (gdouble) *(tmp_source += synth_util->source_stride),
8446 (gdouble) *(tmp_source += synth_util->source_stride),
8447 (gdouble) *(tmp_source += synth_util->source_stride)
8448 };
8449
8450 v_impulse = (ags_v8double) {
8451 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8452 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8453 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8454 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8455 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8456 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8457 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8458 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume
8459 };
8460
8461 i++;
8462
8463 v_impulse *= volume;
8464
8465 v_buffer += v_impulse;
8466
8467 *(source) = (gfloat) v_buffer[0];
8468 *(source += synth_util->source_stride) = (gfloat) v_buffer[1];
8469 *(source += synth_util->source_stride) = (gfloat) v_buffer[2];
8470 *(source += synth_util->source_stride) = (gfloat) v_buffer[3];
8471 *(source += synth_util->source_stride) = (gfloat) v_buffer[4];
8472 *(source += synth_util->source_stride) = (gfloat) v_buffer[5];
8473 *(source += synth_util->source_stride) = (gfloat) v_buffer[6];
8474 *(source += synth_util->source_stride) = (gfloat) v_buffer[7];
8475
8476 source += synth_util->source_stride;
8477 }
8478 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
8479 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8480
8481 if(synth_util->offset + i_stop > synth_util->frame_count){
8482 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8483 }
8484
8485 for(; i < i_stop;){
8486 double ret_v_buffer[8], tmp_ret_v_buffer[8];
8487
8488 tmp_source = source;
8489
8490 double v_buffer[] = {
8491 (double) *(tmp_source),
8492 (double) *(tmp_source += synth_util->source_stride),
8493 (double) *(tmp_source += synth_util->source_stride),
8494 (double) *(tmp_source += synth_util->source_stride),
8495 (double) *(tmp_source += synth_util->source_stride),
8496 (double) *(tmp_source += synth_util->source_stride),
8497 (double) *(tmp_source += synth_util->source_stride),
8498 (double) *(tmp_source += synth_util->source_stride)};
8499 double v_impulse[] = {
8500 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8501 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8502 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8503 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8504 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8505 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8506 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8507 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume};
8508
8509 double v_volume[] = {(double) volume};
8510
8511 i++;
8512
8513 vDSP_vmulD(v_impulse, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
8514 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
8515
8516 *(source) = (gfloat) ret_v_buffer[0];
8517 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[1];
8518 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[2];
8519 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[3];
8520 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[4];
8521 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[5];
8522 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[6];
8523 *(source += synth_util->source_stride) = (gfloat) ret_v_buffer[7];
8524
8525 source += synth_util->source_stride;
8526 }
8527 #else
8528 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8529
8530 if(synth_util->offset + i_stop > synth_util->frame_count){
8531 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8532 }
8533
8534 for(; i < i_stop;){
8535 tmp_source = source;
8536
8537 (*source) = (gfloat) ((tmp_source)[0] + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8538 (*source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8539 (*source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8540 (*source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8541 (*source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8542 (*source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8543 (*source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8544 (*source += synth_util->source_stride) = (gfloat) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8545
8546 source += synth_util->source_stride;
8547 i++;
8548 }
8549 #endif
8550
8551 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
8552 source[0] = (gfloat) (source[0] + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8553
8554 source += synth_util->source_stride;
8555 i++;
8556 }
8557 }
8558
8559 /**
8560 * ags_synth_util_compute_impulse_double:
8561 * @synth_util: the #AgsSynthUtil-struct
8562 *
8563 * Compute impulse synth of double precision floating point data.
8564 *
8565 * Since: 3.9.3
8566 */
8567 void
ags_synth_util_compute_impulse_double(AgsSynthUtil * synth_util)8568 ags_synth_util_compute_impulse_double(AgsSynthUtil *synth_util)
8569 {
8570 gdouble *source, *tmp_source;
8571
8572 gdouble volume;
8573 guint i, i_stop;
8574
8575 if(synth_util == NULL ||
8576 synth_util->source == NULL){
8577 return;
8578 }
8579
8580 source = synth_util->source;
8581
8582 volume = synth_util->volume;
8583
8584 i = 0;
8585
8586 #if defined(AGS_VECTORIZED_BUILTIN_FUNCTIONS)
8587 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8588
8589 if(synth_util->offset + i_stop > synth_util->frame_count){
8590 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8591 }
8592
8593 for(; i < i_stop;){
8594 ags_v8double v_buffer, v_impulse;
8595
8596 tmp_source = source;
8597
8598 v_buffer = (ags_v8double) {
8599 (gdouble) *(tmp_source),
8600 (gdouble) *(tmp_source += synth_util->source_stride),
8601 (gdouble) *(tmp_source += synth_util->source_stride),
8602 (gdouble) *(tmp_source += synth_util->source_stride),
8603 (gdouble) *(tmp_source += synth_util->source_stride),
8604 (gdouble) *(tmp_source += synth_util->source_stride),
8605 (gdouble) *(tmp_source += synth_util->source_stride),
8606 (gdouble) *(tmp_source += synth_util->source_stride)
8607 };
8608
8609 v_impulse = (ags_v8double) {
8610 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8611 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8612 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8613 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8614 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8615 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8616 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8617 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume
8618 };
8619
8620 i++;
8621
8622 v_impulse *= volume;
8623
8624 v_buffer += v_impulse;
8625
8626 *(source) = (gdouble) v_buffer[0];
8627 *(source += synth_util->source_stride) = (gdouble) v_buffer[1];
8628 *(source += synth_util->source_stride) = (gdouble) v_buffer[2];
8629 *(source += synth_util->source_stride) = (gdouble) v_buffer[3];
8630 *(source += synth_util->source_stride) = (gdouble) v_buffer[4];
8631 *(source += synth_util->source_stride) = (gdouble) v_buffer[5];
8632 *(source += synth_util->source_stride) = (gdouble) v_buffer[6];
8633 *(source += synth_util->source_stride) = (gdouble) v_buffer[7];
8634
8635 source += synth_util->source_stride;
8636 }
8637 #elif defined(AGS_OSX_ACCELERATE_BUILTIN_FUNCTIONS)
8638 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8639
8640 if(synth_util->offset + i_stop > synth_util->frame_count){
8641 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8642 }
8643
8644 for(; i < i_stop;){
8645 double ret_v_buffer[8], tmp_ret_v_buffer[8];
8646
8647 tmp_source = source;
8648
8649 double v_buffer[] = {
8650 (double) *(tmp_source),
8651 (double) *(tmp_source += synth_util->source_stride),
8652 (double) *(tmp_source += synth_util->source_stride),
8653 (double) *(tmp_source += synth_util->source_stride),
8654 (double) *(tmp_source += synth_util->source_stride),
8655 (double) *(tmp_source += synth_util->source_stride),
8656 (double) *(tmp_source += synth_util->source_stride),
8657 (double) *(tmp_source += synth_util->source_stride)};
8658 double v_impulse[] = {
8659 (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8660 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8661 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8662 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8663 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8664 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8665 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume,
8666 (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume};
8667
8668 double v_volume[] = {(double) volume};
8669
8670 i++;
8671
8672 vDSP_vmulD(v_impulse, 1, v_volume, 0, tmp_ret_v_buffer, 1, 8);
8673 vDSP_vaddD(v_buffer, 1, tmp_ret_v_buffer, 1, ret_v_buffer, 1, 8);
8674
8675 *(source) = (gdouble) ret_v_buffer[0];
8676 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[1];
8677 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[2];
8678 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[3];
8679 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[4];
8680 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[5];
8681 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[6];
8682 *(source += synth_util->source_stride) = (gdouble) ret_v_buffer[7];
8683
8684 source += synth_util->source_stride;
8685 }
8686 #else
8687 i_stop = synth_util->buffer_length - (synth_util->buffer_length % 8);
8688
8689 if(synth_util->offset + i_stop > synth_util->frame_count){
8690 i_stop = (synth_util->frame_count - synth_util->offset) - ((synth_util->frame_count - synth_util->offset) % 8);
8691 }
8692
8693 for(; i < i_stop;){
8694 tmp_source = source;
8695
8696 (*source) = (gdouble) ((tmp_source)[0] + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8697 (*source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8698 (*source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8699 (*source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8700 (*source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8701 (*source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8702 (*source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8703 (*source += synth_util->source_stride) = (gdouble) ((tmp_source += synth_util->source_stride)[0] + (sin((gdouble) ((synth_util->offset + (i++)) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8704
8705 source += synth_util->source_stride;
8706 i++;
8707 }
8708 #endif
8709
8710 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
8711 source[0] = (gdouble) (source[0] + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume);
8712
8713 source += synth_util->source_stride;
8714 i++;
8715 }
8716 }
8717
8718 /**
8719 * ags_synth_util_compute_impulse_complex:
8720 * @synth_util: the #AgsSynthUtil-struct
8721 *
8722 * Compute impulse synth of complex data.
8723 *
8724 * Since: 3.9.3
8725 */
8726 void
ags_synth_util_compute_impulse_complex(AgsSynthUtil * synth_util)8727 ags_synth_util_compute_impulse_complex(AgsSynthUtil *synth_util)
8728 {
8729 AgsComplex *source;
8730
8731 gdouble volume;
8732 guint i;
8733
8734 if(synth_util == NULL ||
8735 synth_util->source == NULL){
8736 return;
8737 }
8738
8739 source = synth_util->source;
8740
8741 volume = synth_util->volume;
8742
8743 i = 0;
8744
8745 for(; i < synth_util->buffer_length && synth_util->offset + i < synth_util->frame_count;){
8746 ags_complex_set(source,
8747 (gdouble) (ags_complex_get(source) + (sin((gdouble) ((synth_util->offset + i) + synth_util->phase) * 2.0 * M_PI * synth_util->frequency / (gdouble) synth_util->samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0) ? 1.0: -1.0) * volume));
8748
8749 source += synth_util->source_stride;
8750 i++;
8751 }
8752 }
8753
8754 /**
8755 * ags_synth_util_compute_impulse:
8756 * @synth_util: the #AgsSynthUtil-struct
8757 *
8758 * Compute impulse synth.
8759 *
8760 * Since: 3.9.3
8761 */
8762 void
ags_synth_util_compute_impulse(AgsSynthUtil * synth_util)8763 ags_synth_util_compute_impulse(AgsSynthUtil *synth_util)
8764 {
8765 if(synth_util == NULL ||
8766 synth_util->source == NULL){
8767 return;
8768 }
8769
8770 switch(synth_util->audio_buffer_util_format){
8771 case AGS_AUDIO_BUFFER_UTIL_S8:
8772 {
8773 ags_synth_util_compute_impulse_s8(synth_util);
8774 }
8775 break;
8776 case AGS_AUDIO_BUFFER_UTIL_S16:
8777 {
8778 ags_synth_util_compute_impulse_s16(synth_util);
8779 }
8780 break;
8781 case AGS_AUDIO_BUFFER_UTIL_S24:
8782 {
8783 ags_synth_util_compute_impulse_s24(synth_util);
8784 }
8785 break;
8786 case AGS_AUDIO_BUFFER_UTIL_S32:
8787 {
8788 ags_synth_util_compute_impulse_s32(synth_util);
8789 }
8790 break;
8791 case AGS_AUDIO_BUFFER_UTIL_S64:
8792 {
8793 ags_synth_util_compute_impulse_s64(synth_util);
8794 }
8795 break;
8796 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
8797 {
8798 ags_synth_util_compute_impulse_float(synth_util);
8799 }
8800 break;
8801 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
8802 {
8803 ags_synth_util_compute_impulse_double(synth_util);
8804 }
8805 break;
8806 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
8807 {
8808 ags_synth_util_compute_impulse_complex(synth_util);
8809 }
8810 break;
8811 }
8812 }
8813
8814 /**
8815 * ags_synth_util_impulse_s8:
8816 * @buffer: the audio buffer
8817 * @freq: the frequency of the impulse wave
8818 * @phase: the phase of the impulse wave
8819 * @volume: the volume of the impulse wave
8820 * @samplerate: the samplerate
8821 * @offset: start frame
8822 * @n_frames: generate n frames
8823 *
8824 * Generate impulse wave.
8825 *
8826 * Since: 3.0.0
8827 */
8828 void
ags_synth_util_impulse_s8(gint8 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)8829 ags_synth_util_impulse_s8(gint8 *buffer,
8830 gdouble freq, gdouble phase, gdouble volume,
8831 guint samplerate,
8832 guint offset, guint n_frames)
8833 {
8834 static const gdouble scale = 127.0;
8835 guint i;
8836
8837 if(buffer == NULL){
8838 return;
8839 }
8840
8841 for(i = offset; i < offset + n_frames; i++){
8842 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0)){
8843 buffer[i] = (gint8) (0xff & ((gint16) buffer[i] + (gint16) (1.0 * scale * volume)));
8844 }else{
8845 buffer[i] = (gint8) (0xff & ((gint16) buffer[i] + (gint16) (-1.0 * scale * volume)));
8846 }
8847 }
8848 }
8849
8850 /**
8851 * ags_synth_util_impulse_s16:
8852 * @buffer: the audio buffer
8853 * @freq: the frequency of the impulse wave
8854 * @phase: the phase of the impulse wave
8855 * @volume: the volume of the impulse wave
8856 * @samplerate: the samplerate
8857 * @offset: start frame
8858 * @n_frames: generate n frames
8859 *
8860 * Generate impulse wave.
8861 *
8862 * Since: 3.0.0
8863 */
8864 void
ags_synth_util_impulse_s16(gint16 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)8865 ags_synth_util_impulse_s16(gint16 *buffer,
8866 gdouble freq, gdouble phase, gdouble volume,
8867 guint samplerate,
8868 guint offset, guint n_frames)
8869 {
8870 static const gdouble scale = 32767.0;
8871 guint i;
8872
8873 if(buffer == NULL){
8874 return;
8875 }
8876
8877 for(i = offset; i < offset + n_frames; i++){
8878 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0)){
8879 buffer[i] = (gint16) (0xffff & ((gint32) buffer[i] + (gint32) (1.0 * scale * volume)));
8880 }else{
8881 buffer[i] = (gint16) (0xffff & ((gint32) buffer[i] + (gint32) (-1.0 * scale * volume)));
8882 }
8883 }
8884 }
8885
8886 /**
8887 * ags_synth_util_impulse_s24:
8888 * @buffer: the audio buffer
8889 * @freq: the frequency of the impulse wave
8890 * @phase: the phase of the impulse wave
8891 * @volume: the volume of the impulse wave
8892 * @samplerate: the samplerate
8893 * @offset: start frame
8894 * @n_frames: generate n frames
8895 *
8896 * Generate impulse wave.
8897 *
8898 * Since: 3.0.0
8899 */
8900 void
ags_synth_util_impulse_s24(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)8901 ags_synth_util_impulse_s24(gint32 *buffer,
8902 gdouble freq, gdouble phase, gdouble volume,
8903 guint samplerate,
8904 guint offset, guint n_frames)
8905 {
8906 static const gdouble scale = 8388607.0;
8907 guint i;
8908
8909 if(buffer == NULL){
8910 return;
8911 }
8912
8913 for(i = offset; i < offset + n_frames; i++){
8914 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0)){
8915 buffer[i] = (gint32) (0xffffffff & ((gint32) buffer[i] + (gint32) (1.0 * scale * volume)));
8916 }else{
8917 buffer[i] = (gint32) (0xffffffff & ((gint32) buffer[i] + (gint32) (-1.0 * scale * volume)));
8918 }
8919 }
8920 }
8921
8922 /**
8923 * ags_synth_util_impulse_s32:
8924 * @buffer: the audio buffer
8925 * @freq: the frequency of the impulse wave
8926 * @phase: the phase of the impulse wave
8927 * @volume: the volume of the impulse wave
8928 * @samplerate: the samplerate
8929 * @offset: start frame
8930 * @n_frames: generate n frames
8931 *
8932 * Generate impulse wave.
8933 *
8934 * Since: 3.0.0
8935 */
8936 void
ags_synth_util_impulse_s32(gint32 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)8937 ags_synth_util_impulse_s32(gint32 *buffer,
8938 gdouble freq, gdouble phase, gdouble volume,
8939 guint samplerate,
8940 guint offset, guint n_frames)
8941 {
8942 static const gdouble scale = 214748363.0;
8943 guint i;
8944
8945 if(buffer == NULL){
8946 return;
8947 }
8948
8949 for(i = offset; i < offset + n_frames; i++){
8950 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0)){
8951 buffer[i] = (gint32) (0xffffffff & ((gint64) buffer[i] + (gint64) (1.0 * scale * volume)));
8952 }else{
8953 buffer[i] = (gint32) (0xffffffff & ((gint64) buffer[i] + (gint64) (-1.0 * scale * volume)));
8954 }
8955 }
8956 }
8957
8958 /**
8959 * ags_synth_util_impulse_s64:
8960 * @buffer: the audio buffer
8961 * @freq: the frequency of the impulse wave
8962 * @phase: the phase of the impulse wave
8963 * @volume: the volume of the impulse wave
8964 * @samplerate: the samplerate
8965 * @offset: start frame
8966 * @n_frames: generate n frames
8967 *
8968 * Generate impulse wave.
8969 *
8970 * Since: 3.0.0
8971 */
8972 void
ags_synth_util_impulse_s64(gint64 * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)8973 ags_synth_util_impulse_s64(gint64 *buffer,
8974 gdouble freq, gdouble phase, gdouble volume,
8975 guint samplerate,
8976 guint offset, guint n_frames)
8977 {
8978 static const gdouble scale = 9223372036854775807.0;
8979 guint i;
8980
8981 if(buffer == NULL){
8982 return;
8983 }
8984
8985 for(i = offset; i < offset + n_frames; i++){
8986 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0)){
8987 buffer[i] = (gint64) (0xffffffffffffffff & ((gint64) buffer[i] + (gint64) (1.0 * scale * volume)));
8988 }else{
8989 buffer[i] = (gint64) (0xffffffffffffffff & ((gint64) buffer[i] + (gint64) (-1.0 * scale * volume)));
8990 }
8991 }
8992 }
8993
8994 /**
8995 * ags_synth_util_impulse_float:
8996 * @buffer: the audio buffer
8997 * @freq: the frequency of the impulse wave
8998 * @phase: the phase of the impulse wave
8999 * @volume: the volume of the impulse wave
9000 * @samplerate: the samplerate
9001 * @offset: start frame
9002 * @n_frames: generate n frames
9003 *
9004 * Generate impulse wave.
9005 *
9006 * Since: 3.0.0
9007 */
9008 void
ags_synth_util_impulse_float(gfloat * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)9009 ags_synth_util_impulse_float(gfloat *buffer,
9010 gdouble freq, gdouble phase, gdouble volume,
9011 guint samplerate,
9012 guint offset, guint n_frames)
9013 {
9014 guint i;
9015
9016 if(buffer == NULL){
9017 return;
9018 }
9019
9020 for(i = offset; i < offset + n_frames; i++){
9021 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0)){
9022 buffer[i] = (buffer[i] + (1.0 * volume));
9023 }else{
9024 buffer[i] = (buffer[i] + (-1.0 * volume));
9025 }
9026 }
9027 }
9028
9029 /**
9030 * ags_synth_util_impulse_double:
9031 * @buffer: the audio buffer
9032 * @freq: the frequency of the impulse wave
9033 * @phase: the phase of the impulse wave
9034 * @volume: the volume of the impulse wave
9035 * @samplerate: the samplerate
9036 * @offset: start frame
9037 * @n_frames: generate n frames
9038 *
9039 * Generate impulse wave.
9040 *
9041 * Since: 3.0.0
9042 */
9043 void
ags_synth_util_impulse_double(gdouble * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)9044 ags_synth_util_impulse_double(gdouble *buffer,
9045 gdouble freq, gdouble phase, gdouble volume,
9046 guint samplerate,
9047 guint offset, guint n_frames)
9048 {
9049 guint i;
9050
9051 if(buffer == NULL){
9052 return;
9053 }
9054
9055 for(i = offset; i < offset + n_frames; i++){
9056 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0)){
9057 buffer[i] = (buffer[i] + (1.0 * volume));
9058 }else{
9059 buffer[i] = (buffer[i] + (-1.0 * volume));
9060 }
9061 }
9062 }
9063
9064 /**
9065 * ags_synth_util_impulse_complex:
9066 * @buffer: the audio buffer
9067 * @freq: the frequency of the impulse wave
9068 * @phase: the phase of the impulse wave
9069 * @volume: the volume of the impulse wave
9070 * @samplerate: the samplerate
9071 * @offset: start frame
9072 * @n_frames: generate n frames
9073 *
9074 * Generate impulse wave.
9075 *
9076 * Since: 3.0.0
9077 */
9078 void
ags_synth_util_impulse_complex(AgsComplex * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint offset,guint n_frames)9079 ags_synth_util_impulse_complex(AgsComplex *buffer,
9080 gdouble freq, gdouble phase, gdouble volume,
9081 guint samplerate,
9082 guint offset, guint n_frames)
9083 {
9084 AgsComplex *c_ptr;
9085 AgsComplex **c_ptr_ptr;
9086
9087 double y;
9088
9089 guint i_stop;
9090 guint i;
9091
9092 if(buffer == NULL){
9093 return;
9094 }
9095
9096 c_ptr = buffer;
9097 c_ptr_ptr = &c_ptr;
9098
9099 i_stop = offset + n_frames;
9100
9101 for(i = offset; i < i_stop; i++, c_ptr++){
9102 if(sin((gdouble) (i + phase) * 2.0 * M_PI * freq / (gdouble) samplerate) >= sin(2.0 * M_PI * 3.0 / 5.0)){
9103 y = (1.0 * volume);
9104 }else{
9105 y = (-1.0 * volume);
9106 }
9107
9108 AGS_AUDIO_BUFFER_UTIL_DOUBLE_TO_COMPLEX(y, c_ptr_ptr);
9109 }
9110 }
9111
9112 /**
9113 * ags_synth_util_impulse:
9114 * @buffer: the audio buffer
9115 * @freq: the frequency of the impulse wave
9116 * @phase: the phase of the impulse wave
9117 * @volume: the volume of the impulse wave
9118 * @samplerate: the samplerate
9119 * @audio_buffer_util_format: the audio data format
9120 * @offset: start frame
9121 * @n_frames: generate n frames
9122 *
9123 * Generate impulse wave.
9124 *
9125 * Since: 3.0.0
9126 */
9127 void
ags_synth_util_impulse(void * buffer,gdouble freq,gdouble phase,gdouble volume,guint samplerate,guint audio_buffer_util_format,guint offset,guint n_frames)9128 ags_synth_util_impulse(void *buffer,
9129 gdouble freq, gdouble phase, gdouble volume,
9130 guint samplerate, guint audio_buffer_util_format,
9131 guint offset, guint n_frames)
9132 {
9133 if(buffer == NULL){
9134 return;
9135 }
9136
9137 switch(audio_buffer_util_format){
9138 case AGS_AUDIO_BUFFER_UTIL_S8:
9139 {
9140 ags_synth_util_impulse_s8((gint8 *) buffer,
9141 freq, phase, volume,
9142 samplerate,
9143 offset, n_frames);
9144 }
9145 break;
9146 case AGS_AUDIO_BUFFER_UTIL_S16:
9147 {
9148 ags_synth_util_impulse_s16((gint16 *) buffer,
9149 freq, phase, volume,
9150 samplerate,
9151 offset, n_frames);
9152 }
9153 break;
9154 case AGS_AUDIO_BUFFER_UTIL_S24:
9155 {
9156 ags_synth_util_impulse_s24((gint32 *) buffer,
9157 freq, phase, volume,
9158 samplerate,
9159 offset, n_frames);
9160 }
9161 break;
9162 case AGS_AUDIO_BUFFER_UTIL_S32:
9163 {
9164 ags_synth_util_impulse_s32((gint32 *) buffer,
9165 freq, phase, volume,
9166 samplerate,
9167 offset, n_frames);
9168 }
9169 break;
9170 case AGS_AUDIO_BUFFER_UTIL_S64:
9171 {
9172 ags_synth_util_impulse_s64((gint64 *) buffer,
9173 freq, phase, volume,
9174 samplerate,
9175 offset, n_frames);
9176 }
9177 break;
9178 case AGS_AUDIO_BUFFER_UTIL_FLOAT:
9179 {
9180 ags_synth_util_impulse_float((gfloat *) buffer,
9181 freq, phase, volume,
9182 samplerate,
9183 offset, n_frames);
9184 }
9185 break;
9186 case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
9187 {
9188 ags_synth_util_impulse_double((gdouble *) buffer,
9189 freq, phase, volume,
9190 samplerate,
9191 offset, n_frames);
9192 }
9193 break;
9194 case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
9195 {
9196 ags_synth_util_impulse_complex((AgsComplex *) buffer,
9197 freq, phase, volume,
9198 samplerate,
9199 offset, n_frames);
9200 }
9201 break;
9202 default:
9203 {
9204 g_warning("ags_synth_util_impulse() - unsupported format");
9205 }
9206 }
9207 }
9208