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_resample_util.h>
21 
22 #include <ags/audio/ags_audio_buffer_util.h>
23 
24 /**
25  * SECTION:ags_resample_util
26  * @short_description: Boxed type of resample util
27  * @title: AgsResampleUtil
28  * @section_id:
29  * @include: ags/audio/ags_resample_util.h
30  *
31  * Boxed type of resample util data type.
32  */
33 
34 GType
ags_resample_util_get_type(void)35 ags_resample_util_get_type(void)
36 {
37   static volatile gsize g_define_type_id__volatile = 0;
38 
39   if(g_once_init_enter (&g_define_type_id__volatile)){
40     GType ags_type_resample_util = 0;
41 
42     ags_type_resample_util =
43       g_boxed_type_register_static("AgsResampleUtil",
44 				   (GBoxedCopyFunc) ags_resample_util_copy,
45 				   (GBoxedFreeFunc) ags_resample_util_free);
46 
47     g_once_init_leave(&g_define_type_id__volatile, ags_type_resample_util);
48   }
49 
50   return g_define_type_id__volatile;
51 }
52 
53 /**
54  * ags_resample_util_alloc:
55  *
56  * Allocate #AgsResampleUtil-struct
57  *
58  * Returns: a new #AgsResampleUtil-struct
59  *
60  * Since: 3.9.2
61  */
62 AgsResampleUtil*
ags_resample_util_alloc()63 ags_resample_util_alloc()
64 {
65   AgsResampleUtil *ptr;
66 
67   ptr = (AgsResampleUtil *) g_new(AgsResampleUtil,
68 				  1);
69 
70   ptr->secret_rabbit.src_ratio = 1.0;
71 
72   ptr->secret_rabbit.input_frames = 0;
73   ptr->secret_rabbit.data_in = NULL;
74 
75   ptr->secret_rabbit.output_frames = 0;
76   ptr->secret_rabbit.data_out = NULL;
77 
78   ptr->destination = NULL;
79   ptr->destination_stride = 1;
80 
81   ptr->source = NULL;
82   ptr->source_stride = 1;
83 
84   ptr->buffer_length = 0;
85   ptr->format = AGS_RESAMPLE_UTIL_DEFAULT_FORMAT;
86   ptr->samplerate = AGS_RESAMPLE_UTIL_DEFAULT_SAMPLERATE;
87 
88   ptr->audio_buffer_util_format = AGS_RESAMPLE_UTIL_DEFAULT_AUDIO_BUFFER_UTIL_FORMAT;
89 
90   ptr->target_samplerate = AGS_RESAMPLE_UTIL_DEFAULT_TARGET_SAMPLERATE;
91 
92   return(ptr);
93 }
94 
95 /**
96  * ags_resample_util_copy:
97  * @ptr: the original #AgsResampleUtil-struct
98  *
99  * Create a copy of @ptr.
100  *
101  * Returns: a pointer of the new #AgsResampleUtil-struct
102  *
103  * Since: 3.9.2
104  */
105 gpointer
ags_resample_util_copy(AgsResampleUtil * ptr)106 ags_resample_util_copy(AgsResampleUtil *ptr)
107 {
108   AgsResampleUtil *new_ptr;
109 
110   new_ptr = (AgsResampleUtil *) g_new(AgsResampleUtil,
111 				      1);
112 
113   new_ptr->secret_rabbit.src_ratio = 1.0;
114 
115   new_ptr->secret_rabbit.input_frames = ptr->secret_rabbit.input_frames;
116   new_ptr->secret_rabbit.data_in = NULL;
117 
118   if(new_ptr->secret_rabbit.input_frames > 0){
119     new_ptr->secret_rabbit.data_in = (gfloat *) g_malloc(new_ptr->secret_rabbit.input_frames * sizeof(gfloat));
120   }
121 
122   new_ptr->secret_rabbit.output_frames = ptr->secret_rabbit.output_frames;
123   new_ptr->secret_rabbit.data_out = NULL;
124 
125   if(new_ptr->secret_rabbit.output_frames > 0){
126     new_ptr->secret_rabbit.data_out = (gfloat *) g_malloc(new_ptr->secret_rabbit.output_frames * sizeof(gfloat));
127   }
128 
129   new_ptr->destination = ptr->destination;
130   new_ptr->destination_stride = ptr->destination_stride;
131 
132   new_ptr->source = ptr->source;
133   new_ptr->source_stride = ptr->source_stride;
134 
135   new_ptr->buffer_length = ptr->buffer_length;
136   new_ptr->format = ptr->format;
137   new_ptr->samplerate = ptr->samplerate;
138 
139   new_ptr->audio_buffer_util_format = ptr->audio_buffer_util_format;
140 
141   new_ptr->target_samplerate = ptr->target_samplerate;
142 
143   return(new_ptr);
144 }
145 
146 /**
147  * ags_resample_util_free:
148  * @ptr: the #AgsResampleUtil-struct
149  *
150  * Free the memory of @ptr.
151  *
152  * Since: 3.9.2
153  */
154 void
ags_resample_util_free(AgsResampleUtil * ptr)155 ags_resample_util_free(AgsResampleUtil *ptr)
156 {
157   g_free(ptr->secret_rabbit.data_in);
158   g_free(ptr->secret_rabbit.data_out);
159 
160   g_free(ptr->destination);
161 
162   if(ptr->destination != ptr->source){
163     g_free(ptr->source);
164   }
165 
166   g_free(ptr);
167 }
168 
169 /**
170  * ags_resample_util_get_destination:
171  * @resample_util: the #AgsResampleUtil-struct
172  *
173  * Get destination buffer of @resample_util.
174  *
175  * Returns: the destination buffer
176  *
177  * Since: 3.9.2
178  */
179 gpointer
ags_resample_util_get_destination(AgsResampleUtil * resample_util)180 ags_resample_util_get_destination(AgsResampleUtil *resample_util)
181 {
182   if(resample_util == NULL){
183     return(NULL);
184   }
185 
186   return(resample_util->destination);
187 }
188 
189 /**
190  * ags_resample_util_set_destination:
191  * @resample_util: the #AgsResampleUtil-struct
192  * @destination: the destination buffer
193  *
194  * Set @destination buffer of @resample_util.
195  *
196  * Since: 3.9.2
197  */
198 void
ags_resample_util_set_destination(AgsResampleUtil * resample_util,gpointer destination)199 ags_resample_util_set_destination(AgsResampleUtil *resample_util,
200 				  gpointer destination)
201 {
202   if(resample_util == NULL){
203     return;
204   }
205 
206   resample_util->destination = destination;
207 }
208 
209 /**
210  * ags_resample_util_get_destination_stride:
211  * @resample_util: the #AgsResampleUtil-struct
212  *
213  * Get destination stride of @resample_util.
214  *
215  * Returns: the destination buffer stride
216  *
217  * Since: 3.9.2
218  */
219 guint
ags_resample_util_get_destination_stride(AgsResampleUtil * resample_util)220 ags_resample_util_get_destination_stride(AgsResampleUtil *resample_util)
221 {
222   if(resample_util == NULL){
223     return(0);
224   }
225 
226   return(resample_util->destination_stride);
227 }
228 
229 /**
230  * ags_resample_util_set_destination_stride:
231  * @resample_util: the #AgsResampleUtil-struct
232  * @destination_stride: the destination buffer stride
233  *
234  * Set @destination stride of @resample_util.
235  *
236  * Since: 3.9.2
237  */
238 void
ags_resample_util_set_destination_stride(AgsResampleUtil * resample_util,guint destination_stride)239 ags_resample_util_set_destination_stride(AgsResampleUtil *resample_util,
240 					 guint destination_stride)
241 {
242   if(resample_util == NULL){
243     return;
244   }
245 
246   resample_util->destination_stride = destination_stride;
247 }
248 
249 /**
250  * ags_resample_util_get_source:
251  * @resample_util: the #AgsResampleUtil-struct
252  *
253  * Get source buffer of @resample_util.
254  *
255  * Returns: the source buffer
256  *
257  * Since: 3.9.2
258  */
259 gpointer
ags_resample_util_get_source(AgsResampleUtil * resample_util)260 ags_resample_util_get_source(AgsResampleUtil *resample_util)
261 {
262   if(resample_util == NULL){
263     return(NULL);
264   }
265 
266   return(resample_util->source);
267 }
268 
269 /**
270  * ags_resample_util_set_source:
271  * @resample_util: the #AgsResampleUtil-struct
272  * @source: the source buffer
273  *
274  * Set @source buffer of @resample_util.
275  *
276  * Since: 3.9.2
277  */
278 void
ags_resample_util_set_source(AgsResampleUtil * resample_util,gpointer source)279 ags_resample_util_set_source(AgsResampleUtil *resample_util,
280 			     gpointer source)
281 {
282   if(resample_util == NULL){
283     return;
284   }
285 
286   resample_util->source = source;
287 }
288 
289 /**
290  * ags_resample_util_get_source_stride:
291  * @resample_util: the #AgsResampleUtil-struct
292  *
293  * Get source stride of @resample_util.
294  *
295  * Returns: the source buffer stride
296  *
297  * Since: 3.9.2
298  */
299 guint
ags_resample_util_get_source_stride(AgsResampleUtil * resample_util)300 ags_resample_util_get_source_stride(AgsResampleUtil *resample_util)
301 {
302   if(resample_util == NULL){
303     return(0);
304   }
305 
306   return(resample_util->source_stride);
307 }
308 
309 /**
310  * ags_resample_util_set_source_stride:
311  * @resample_util: the #AgsResampleUtil-struct
312  * @source_stride: the source buffer stride
313  *
314  * Set @source stride of @resample_util.
315  *
316  * Since: 3.9.2
317  */
318 void
ags_resample_util_set_source_stride(AgsResampleUtil * resample_util,guint source_stride)319 ags_resample_util_set_source_stride(AgsResampleUtil *resample_util,
320 				    guint source_stride)
321 {
322   if(resample_util == NULL){
323     return;
324   }
325 
326   resample_util->source_stride = source_stride;
327 }
328 
329 /**
330  * ags_resample_util_get_buffer_length:
331  * @resample_util: the #AgsResampleUtil-struct
332  *
333  * Get buffer length of @resample_util.
334  *
335  * Returns: the buffer length
336  *
337  * Since: 3.9.2
338  */
339 guint
ags_resample_util_get_buffer_length(AgsResampleUtil * resample_util)340 ags_resample_util_get_buffer_length(AgsResampleUtil *resample_util)
341 {
342   if(resample_util == NULL){
343     return(0);
344   }
345 
346   return(resample_util->buffer_length);
347 }
348 
349 /**
350  * ags_resample_util_set_buffer_length:
351  * @resample_util: the #AgsResampleUtil-struct
352  * @buffer_length: the buffer length
353  *
354  * Set @buffer_length of @resample_util.
355  *
356  * Since: 3.9.2
357  */
358 void
ags_resample_util_set_buffer_length(AgsResampleUtil * resample_util,guint buffer_length)359 ags_resample_util_set_buffer_length(AgsResampleUtil *resample_util,
360 				    guint buffer_length)
361 {
362   if(resample_util == NULL ||
363      resample_util->buffer_length == buffer_length){
364     return;
365   }
366 
367   resample_util->buffer_length = buffer_length;
368 
369   if(buffer_length > 0){
370     resample_util->secret_rabbit.input_frames = buffer_length;
371     resample_util->secret_rabbit.output_frames = ceil(resample_util->secret_rabbit.src_ratio * buffer_length);
372 
373     g_free(resample_util->secret_rabbit.data_in);
374     g_free(resample_util->secret_rabbit.data_out);
375 
376     resample_util->secret_rabbit.data_in = (gfloat *) g_malloc(buffer_length * sizeof(gfloat));
377     resample_util->secret_rabbit.data_out = (gfloat *) g_malloc(resample_util->secret_rabbit.output_frames * sizeof(gfloat));
378   }else{
379     resample_util->secret_rabbit.input_frames = 0;
380     resample_util->secret_rabbit.output_frames = 0;
381 
382     g_free(resample_util->secret_rabbit.data_out);
383     g_free(resample_util->secret_rabbit.data_in);
384 
385     resample_util->secret_rabbit.data_out = NULL;
386     resample_util->secret_rabbit.data_in = NULL;
387   }
388 }
389 
390 /**
391  * ags_resample_util_get_format:
392  * @resample_util: the #AgsResampleUtil-struct
393  *
394  * Get format of @resample_util.
395  *
396  * Returns: the format
397  *
398  * Since: 3.9.6
399  */
400 guint
ags_resample_util_get_format(AgsResampleUtil * resample_util)401 ags_resample_util_get_format(AgsResampleUtil *resample_util)
402 {
403   if(resample_util == NULL){
404     return(0);
405   }
406 
407   return(resample_util->format);
408 }
409 
410 /**
411  * ags_resample_util_set_format:
412  * @resample_util: the #AgsResampleUtil-struct
413  * @format: the format
414  *
415  * Set @format of @resample_util.
416  *
417  * Since: 3.9.6
418  */
419 void
ags_resample_util_set_format(AgsResampleUtil * resample_util,guint format)420 ags_resample_util_set_format(AgsResampleUtil *resample_util,
421 			     guint format)
422 {
423   if(resample_util == NULL){
424     return;
425   }
426 
427   resample_util->format = format;
428 
429   resample_util->audio_buffer_util_format = ags_audio_buffer_util_format_from_soundcard(format);
430 }
431 
432 /**
433  * ags_resample_util_get_samplerate:
434  * @resample_util: the #AgsResampleUtil-struct
435  *
436  * Get samplerate of @resample_util.
437  *
438  * Returns: the samplerate
439  *
440  * Since: 3.9.2
441  */
442 guint
ags_resample_util_get_samplerate(AgsResampleUtil * resample_util)443 ags_resample_util_get_samplerate(AgsResampleUtil *resample_util)
444 {
445   if(resample_util == NULL){
446     return(AGS_RESAMPLE_UTIL_DEFAULT_SAMPLERATE);
447   }
448 
449   return(resample_util->samplerate);
450 }
451 
452 /**
453  * ags_resample_util_set_samplerate:
454  * @resample_util: the #AgsResampleUtil-struct
455  * @samplerate: the samplerate
456  *
457  * Set @samplerate of @resample_util.
458  *
459  * Since: 3.9.2
460  */
461 void
ags_resample_util_set_samplerate(AgsResampleUtil * resample_util,guint samplerate)462 ags_resample_util_set_samplerate(AgsResampleUtil *resample_util,
463 				 guint samplerate)
464 {
465   if(resample_util == NULL ||
466      resample_util->samplerate == samplerate){
467     return;
468   }
469 
470   resample_util->samplerate = samplerate;
471 
472   resample_util->secret_rabbit.src_ratio = resample_util->target_samplerate / samplerate;
473 
474   if(resample_util->buffer_length > 0){
475     resample_util->secret_rabbit.output_frames = ceil(resample_util->secret_rabbit.src_ratio * resample_util->buffer_length);
476 
477     g_free(resample_util->secret_rabbit.data_out);
478 
479     resample_util->secret_rabbit.data_out = (gfloat *) g_malloc(resample_util->secret_rabbit.output_frames * sizeof(gfloat));
480   }
481 }
482 
483 /**
484  * ags_resample_util_get_audio_buffer_util_format:
485  * @resample_util: the #AgsResampleUtil-struct
486  *
487  * Get audio buffer util format of @resample_util.
488  *
489  * Returns: the audio buffer util format
490  *
491  * Since: 3.9.2
492  */
493 guint
ags_resample_util_get_audio_buffer_util_format(AgsResampleUtil * resample_util)494 ags_resample_util_get_audio_buffer_util_format(AgsResampleUtil *resample_util)
495 {
496   if(resample_util == NULL){
497     return(0);
498   }
499 
500   return(resample_util->audio_buffer_util_format);
501 }
502 
503 /**
504  * ags_resample_util_set_audio_buffer_util_format:
505  * @resample_util: the #AgsResampleUtil-struct
506  * @audio_buffer_util_format: the audio buffer util format
507  *
508  * Set @audio_buffer_util_format of @resample_util.
509  *
510  * Since: 3.9.2
511  */
512 void
ags_resample_util_set_audio_buffer_util_format(AgsResampleUtil * resample_util,guint audio_buffer_util_format)513 ags_resample_util_set_audio_buffer_util_format(AgsResampleUtil *resample_util,
514 					       guint audio_buffer_util_format)
515 {
516   if(resample_util == NULL){
517     return;
518   }
519 
520   resample_util->audio_buffer_util_format = audio_buffer_util_format;
521 }
522 
523 /**
524  * ags_resample_util_get_target_samplerate:
525  * @resample_util: the #AgsResampleUtil-struct
526  *
527  * Get target samplerate of @resample_util.
528  *
529  * Returns: the  samplerate
530  *
531  * Since: 3.9.2
532  */
533 guint
ags_resample_util_get_target_samplerate(AgsResampleUtil * resample_util)534 ags_resample_util_get_target_samplerate(AgsResampleUtil *resample_util)
535 {
536   if(resample_util == NULL){
537     return(AGS_RESAMPLE_UTIL_DEFAULT_TARGET_SAMPLERATE);
538   }
539 
540   return(resample_util->target_samplerate);
541 }
542 
543 /**
544  * ags_resample_util_set_target_samplerate:
545  * @resample_util: the #AgsResampleUtil-struct
546  * @target_samplerate: the target samplerate
547  *
548  * Set @target_samplerate of @resample_util.
549  *
550  * Since: 3.9.2
551  */
552 void
ags_resample_util_set_target_samplerate(AgsResampleUtil * resample_util,guint target_samplerate)553 ags_resample_util_set_target_samplerate(AgsResampleUtil *resample_util,
554 					guint target_samplerate)
555 {
556   if(resample_util == NULL ||
557      resample_util->target_samplerate == target_samplerate){
558     return;
559   }
560 
561   resample_util->target_samplerate = target_samplerate;
562 
563   resample_util->secret_rabbit.src_ratio = target_samplerate / resample_util->samplerate;
564 
565   if(resample_util->buffer_length > 0){
566     resample_util->secret_rabbit.output_frames = ceil(resample_util->secret_rabbit.src_ratio * resample_util->buffer_length);
567 
568     g_free(resample_util->secret_rabbit.data_out);
569 
570     resample_util->secret_rabbit.data_out = (gfloat *) g_malloc(resample_util->secret_rabbit.output_frames * sizeof(gfloat));
571   }
572 }
573 
574 /**
575  * ags_resample_util_compute_s8:
576  * @resample_util: the #AgsResampleUtil-struct
577  *
578  * Compute resample of signed 8 bit data.
579  *
580  * Since: 3.9.2
581  */
582 void
ags_resample_util_compute_s8(AgsResampleUtil * resample_util)583 ags_resample_util_compute_s8(AgsResampleUtil *resample_util)
584 {
585   SRC_DATA *secret_rabbit;
586 
587   gint8 *destination;
588   gint8 *source;
589 
590   if(resample_util == NULL ||
591      resample_util->destination == NULL ||
592      resample_util->source == NULL){
593     return;
594   }
595 
596   secret_rabbit = &(resample_util->secret_rabbit);
597 
598   destination = (gint8 *) resample_util->destination;
599   source = (gint8 *) resample_util->source;
600 
601   ags_audio_buffer_util_clear_float(secret_rabbit->data_in, 1,
602 				    secret_rabbit->input_frames);
603 
604   ags_audio_buffer_util_copy_s8_to_float(secret_rabbit->data_in, 1,
605 					 source, resample_util->source_stride,
606 					 resample_util->buffer_length);
607 
608   src_simple(secret_rabbit,
609 	     SRC_SINC_BEST_QUALITY,
610 	     1);
611 
612   memset(destination, 0, resample_util->buffer_length * sizeof(gint8));
613 
614   ags_audio_buffer_util_copy_float_to_s8(destination, resample_util->destination_stride,
615 					 secret_rabbit->data_out, 1,
616 					 resample_util->buffer_length);
617 }
618 
619 /**
620  * ags_resample_util_compute_s16:
621  * @resample_util: the #AgsResampleUtil-struct
622  *
623  * Compute resample of signed 16 bit data.
624  *
625  * Since: 3.9.2
626  */
627 void
ags_resample_util_compute_s16(AgsResampleUtil * resample_util)628 ags_resample_util_compute_s16(AgsResampleUtil *resample_util)
629 {
630   SRC_DATA *secret_rabbit;
631 
632   gint16 *destination;
633   gint16 *source;
634 
635   if(resample_util == NULL ||
636      resample_util->destination == NULL ||
637      resample_util->source == NULL){
638     return;
639   }
640 
641   secret_rabbit = &(resample_util->secret_rabbit);
642 
643   destination = (gint16 *) resample_util->destination;
644   source = (gint16 *) resample_util->source;
645 
646   ags_audio_buffer_util_clear_float(secret_rabbit->data_in, 1,
647 				    secret_rabbit->input_frames);
648 
649   ags_audio_buffer_util_copy_s16_to_float(secret_rabbit->data_in, 1,
650 					  source, resample_util->source_stride,
651 					  resample_util->buffer_length);
652 
653   src_simple(secret_rabbit,
654 	     SRC_SINC_BEST_QUALITY,
655 	     1);
656 
657   memset(destination, 0, resample_util->buffer_length * sizeof(gint8));
658 
659   ags_audio_buffer_util_copy_float_to_s16(destination, resample_util->destination_stride,
660 					  secret_rabbit->data_out, 1,
661 					  resample_util->buffer_length);
662 }
663 
664 /**
665  * ags_resample_util_compute_s24:
666  * @resample_util: the #AgsResampleUtil-struct
667  *
668  * Compute resample of signed 24 bit data.
669  *
670  * Since: 3.9.2
671  */
672 void
ags_resample_util_compute_s24(AgsResampleUtil * resample_util)673 ags_resample_util_compute_s24(AgsResampleUtil *resample_util)
674 {
675   SRC_DATA *secret_rabbit;
676 
677   gint32 *destination;
678   gint32 *source;
679 
680   if(resample_util == NULL ||
681      resample_util->destination == NULL ||
682      resample_util->source == NULL){
683     return;
684   }
685 
686   secret_rabbit = &(resample_util->secret_rabbit);
687 
688   destination = (gint32 *) resample_util->destination;
689   source = (gint32 *) resample_util->source;
690 
691   ags_audio_buffer_util_clear_float(secret_rabbit->data_in, 1,
692 				    secret_rabbit->input_frames);
693 
694   ags_audio_buffer_util_copy_s24_to_float(secret_rabbit->data_in, 1,
695 					  source, resample_util->source_stride,
696 					  resample_util->buffer_length);
697 
698   src_simple(secret_rabbit,
699 	     SRC_SINC_BEST_QUALITY,
700 	     1);
701 
702   memset(destination, 0, resample_util->buffer_length * sizeof(gint8));
703 
704   ags_audio_buffer_util_copy_float_to_s24(destination, resample_util->destination_stride,
705 					  secret_rabbit->data_out, 1,
706 					  resample_util->buffer_length);
707 }
708 
709 /**
710  * ags_resample_util_compute_s32:
711  * @resample_util: the #AgsResampleUtil-struct
712  *
713  * Compute resample of signed 32 bit data.
714  *
715  * Since: 3.9.2
716  */
717 void
ags_resample_util_compute_s32(AgsResampleUtil * resample_util)718 ags_resample_util_compute_s32(AgsResampleUtil *resample_util)
719 {
720   SRC_DATA *secret_rabbit;
721 
722   gint32 *destination;
723   gint32 *source;
724 
725   if(resample_util == NULL ||
726      resample_util->destination == NULL ||
727      resample_util->source == NULL){
728     return;
729   }
730 
731   secret_rabbit = &(resample_util->secret_rabbit);
732 
733   destination = (gint32 *) resample_util->destination;
734   source = (gint32 *) resample_util->source;
735 
736   ags_audio_buffer_util_clear_float(secret_rabbit->data_in, 1,
737 				    secret_rabbit->input_frames);
738 
739   ags_audio_buffer_util_copy_s32_to_float(secret_rabbit->data_in, 1,
740 					  source, resample_util->source_stride,
741 					  resample_util->buffer_length);
742 
743   src_simple(secret_rabbit,
744 	     SRC_SINC_BEST_QUALITY,
745 	     1);
746 
747   memset(destination, 0, resample_util->buffer_length * sizeof(gint8));
748 
749   ags_audio_buffer_util_copy_float_to_s32(destination, resample_util->destination_stride,
750 					  secret_rabbit->data_out, 1,
751 					  resample_util->buffer_length);
752 }
753 
754 /**
755  * ags_resample_util_compute_s64:
756  * @resample_util: the #AgsResampleUtil-struct
757  *
758  * Compute resample of signed 64 bit data.
759  *
760  * Since: 3.9.2
761  */
762 void
ags_resample_util_compute_s64(AgsResampleUtil * resample_util)763 ags_resample_util_compute_s64(AgsResampleUtil *resample_util)
764 {
765   SRC_DATA *secret_rabbit;
766 
767   gint64 *destination;
768   gint64 *source;
769 
770   if(resample_util == NULL ||
771      resample_util->destination == NULL ||
772      resample_util->source == NULL){
773     return;
774   }
775 
776   secret_rabbit = &(resample_util->secret_rabbit);
777 
778   destination = (gint64 *) resample_util->destination;
779   source = (gint64 *) resample_util->source;
780 
781   ags_audio_buffer_util_clear_float(secret_rabbit->data_in, 1,
782 				    secret_rabbit->input_frames);
783 
784   ags_audio_buffer_util_copy_s64_to_float(secret_rabbit->data_in, 1,
785 					  source, resample_util->source_stride,
786 					  resample_util->buffer_length);
787 
788   src_simple(secret_rabbit,
789 	     SRC_SINC_BEST_QUALITY,
790 	     1);
791 
792   memset(destination, 0, resample_util->buffer_length * sizeof(gint8));
793 
794   ags_audio_buffer_util_copy_float_to_s64(destination, resample_util->destination_stride,
795 					  secret_rabbit->data_out, 1,
796 					  resample_util->buffer_length);
797 }
798 
799 /**
800  * ags_resample_util_compute_float:
801  * @resample_util: the #AgsResampleUtil-struct
802  *
803  * Compute resample of floating point data.
804  *
805  * Since: 3.9.2
806  */
807 void
ags_resample_util_compute_float(AgsResampleUtil * resample_util)808 ags_resample_util_compute_float(AgsResampleUtil *resample_util)
809 {
810   SRC_DATA *secret_rabbit;
811 
812   gfloat *destination;
813   gfloat *source;
814 
815   if(resample_util == NULL ||
816      resample_util->destination == NULL ||
817      resample_util->source == NULL){
818     return;
819   }
820 
821   secret_rabbit = &(resample_util->secret_rabbit);
822 
823   destination = (gfloat *) resample_util->destination;
824   source = (gfloat *) resample_util->source;
825 
826   ags_audio_buffer_util_clear_float(secret_rabbit->data_in, 1,
827 				    secret_rabbit->input_frames);
828 
829   ags_audio_buffer_util_copy_float_to_float(secret_rabbit->data_in, 1,
830 					    source, resample_util->source_stride,
831 					    resample_util->buffer_length);
832 
833   src_simple(secret_rabbit,
834 	     SRC_SINC_BEST_QUALITY,
835 	     1);
836 
837   memset(destination, 0, resample_util->buffer_length * sizeof(gint8));
838 
839   ags_audio_buffer_util_copy_float_to_float(destination, resample_util->destination_stride,
840 					    secret_rabbit->data_out, 1,
841 					    resample_util->buffer_length);
842 }
843 
844 /**
845  * ags_resample_util_compute_double:
846  * @resample_util: the #AgsResampleUtil-struct
847  *
848  * Compute resample of double floating point data.
849  *
850  * Since: 3.9.2
851  */
852 void
ags_resample_util_compute_double(AgsResampleUtil * resample_util)853 ags_resample_util_compute_double(AgsResampleUtil *resample_util)
854 {
855   SRC_DATA *secret_rabbit;
856 
857   gdouble *destination;
858   gdouble *source;
859 
860   if(resample_util == NULL ||
861      resample_util->destination == NULL ||
862      resample_util->source == NULL){
863     return;
864   }
865 
866   secret_rabbit = &(resample_util->secret_rabbit);
867 
868   destination = (gdouble *) resample_util->destination;
869   source = (gdouble *) resample_util->source;
870 
871   ags_audio_buffer_util_clear_float(secret_rabbit->data_in, 1,
872 				    secret_rabbit->input_frames);
873 
874   ags_audio_buffer_util_copy_double_to_float(secret_rabbit->data_in, 1,
875 					     source, resample_util->source_stride,
876 					     resample_util->buffer_length);
877 
878   src_simple(secret_rabbit,
879 	     SRC_SINC_BEST_QUALITY,
880 	     1);
881 
882   memset(destination, 0, resample_util->buffer_length * sizeof(gint8));
883 
884   ags_audio_buffer_util_copy_float_to_double(destination, resample_util->destination_stride,
885 					     secret_rabbit->data_out, 1,
886 					     resample_util->buffer_length);
887 }
888 
889 /**
890  * ags_resample_util_compute_complex:
891  * @resample_util: the #AgsResampleUtil-struct
892  *
893  * Compute resample of complex floating point data.
894  *
895  * Since: 3.9.2
896  */
897 void
ags_resample_util_compute_complex(AgsResampleUtil * resample_util)898 ags_resample_util_compute_complex(AgsResampleUtil *resample_util)
899 {
900   SRC_DATA *secret_rabbit;
901 
902   AgsComplex *destination;
903   AgsComplex *source;
904 
905   if(resample_util == NULL ||
906      resample_util->destination == NULL ||
907      resample_util->source == NULL){
908     return;
909   }
910 
911   secret_rabbit = &(resample_util->secret_rabbit);
912 
913   destination = (AgsComplex *) resample_util->destination;
914   source = (AgsComplex *) resample_util->source;
915 
916   ags_audio_buffer_util_clear_float(secret_rabbit->data_in, 1,
917 				    secret_rabbit->input_frames);
918 
919   ags_audio_buffer_util_copy_complex_to_float(secret_rabbit->data_in, 1,
920 					      source, resample_util->source_stride,
921 					      resample_util->buffer_length);
922 
923   src_simple(secret_rabbit,
924 	     SRC_SINC_BEST_QUALITY,
925 	     1);
926 
927   memset(destination, 0, resample_util->buffer_length * sizeof(gint8));
928 
929   ags_audio_buffer_util_copy_float_to_complex(destination, resample_util->destination_stride,
930 					      secret_rabbit->data_out, 1,
931 					      resample_util->buffer_length);
932 }
933 
934 /**
935  * ags_resample_util_compute:
936  * @resample_util: the #AgsResampleUtil-struct
937  *
938  * Compute resample.
939  *
940  * Since: 3.9.2
941  */
942 void
ags_resample_util_compute(AgsResampleUtil * resample_util)943 ags_resample_util_compute(AgsResampleUtil *resample_util)
944 {
945   if(resample_util == NULL ||
946      resample_util->destination == NULL ||
947      resample_util->source == NULL){
948     return;
949   }
950 
951   switch(resample_util->audio_buffer_util_format){
952   case AGS_AUDIO_BUFFER_UTIL_S8:
953   {
954     ags_resample_util_compute_s8(resample_util);
955   }
956   break;
957   case AGS_AUDIO_BUFFER_UTIL_S16:
958   {
959     ags_resample_util_compute_s16(resample_util);
960   }
961   break;
962   case AGS_AUDIO_BUFFER_UTIL_S24:
963   {
964     ags_resample_util_compute_s24(resample_util);
965   }
966   break;
967   case AGS_AUDIO_BUFFER_UTIL_S32:
968   {
969     ags_resample_util_compute_s32(resample_util);
970   }
971   break;
972   case AGS_AUDIO_BUFFER_UTIL_S64:
973   {
974     ags_resample_util_compute_s64(resample_util);
975   }
976   break;
977   case AGS_AUDIO_BUFFER_UTIL_FLOAT:
978   {
979     ags_resample_util_compute_float(resample_util);
980   }
981   break;
982   case AGS_AUDIO_BUFFER_UTIL_DOUBLE:
983   {
984     ags_resample_util_compute_double(resample_util);
985   }
986   break;
987   case AGS_AUDIO_BUFFER_UTIL_COMPLEX:
988   {
989     ags_resample_util_compute_complex(resample_util);
990   }
991   break;
992   }
993 }
994