1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  *
4  */
5 
6 #include <stdlib.h>
7 #include <stdint.h>
8 
9 
fade_out_proc_8bit(uint8_t * buffer,int nbytes,int * pos,int scope,int channels)10 int fade_out_proc_8bit(uint8_t *buffer, int nbytes,
11 	int *pos, int scope, int channels)
12 {
13 	char *ptr = (char *)buffer;
14 	int sample_pos = *pos;
15 	int samples;
16 	int sample_index;
17 	int channel_index;
18 	double sample_val;
19 
20 	samples = nbytes / channels;
21 	for (sample_index = 0; sample_index < samples; sample_index++) {
22 		for (channel_index = 0; channel_index < channels;
23 			channel_index++) {
24 			sample_val = (double)*ptr;
25 			sample_val *= (double)sample_pos;
26 			sample_val /= (double)scope;
27 			*ptr = (uint8_t)sample_val;
28 			ptr++;
29 		}
30 
31 		if (sample_pos > 0)
32 			sample_pos--;
33 	}
34 
35 	*pos = sample_pos;
36 	return 0;
37 }
38 
fade_in_proc_8bit(uint8_t * buffer,int nbytes,int * pos,int scope,int channels)39 int fade_in_proc_8bit(uint8_t *buffer, int nbytes,
40 	int *pos, int scope, int channels)
41 {
42 	char *ptr = (char *)buffer;
43 	int sample_pos = *pos;
44 	int samples;
45 	int sample_index;
46 	int channel_index;
47 	double sample_val;
48 
49 	if (sample_pos >= scope)
50 		goto __exit;
51 
52 	samples = nbytes / channels;
53 	for (sample_index = 0; sample_index < samples; sample_index++) {
54 		for (channel_index = 0; channel_index < channels;
55 			channel_index++) {
56 			sample_val = (double)*ptr;
57 			sample_val *= (double)sample_pos;
58 			sample_val /= (double)scope;
59 			*ptr = (uint8_t)sample_val;
60 			ptr++;
61 		}
62 
63 		if (sample_pos < scope)
64 			sample_pos++;
65 
66 		if (sample_pos >= scope)
67 			break;
68 	}
69 
70 	*pos = sample_pos;
71 
72 __exit:
73 	return 0;
74 }
75 
fade_out_proc_16bit(uint8_t * buffer,int nbytes,int * pos,int scope,int channels)76 int fade_out_proc_16bit(uint8_t *buffer, int nbytes,
77 	int *pos, int scope, int channels)
78 {
79 	short *ptr = (short *)buffer;
80 	int sample_pos = *pos;
81 	int samples;
82 	int sample_index;
83 	int channel_index;
84 	double sample_val;
85 
86 	samples = nbytes / (channels * 2);
87 	for (sample_index = 0; sample_index < samples; sample_index++) {
88 		for (channel_index = 0; channel_index < channels;
89 			channel_index++) {
90 			sample_val = (double)*ptr;
91 			sample_val *= (double)sample_pos;
92 			sample_val /= (double)scope;
93 			*ptr = (short)sample_val;
94 			ptr++;
95 		}
96 
97 		if (sample_pos > 0)
98 			sample_pos--;
99 	}
100 
101 	*pos = sample_pos;
102 	return 0;
103 }
104 
fade_in_proc_16bit(uint8_t * buffer,int nbytes,int * pos,int scope,int channels)105 int fade_in_proc_16bit(uint8_t *buffer, int nbytes,
106 	int *pos, int scope, int channels)
107 {
108 	short *ptr = (short *)buffer;
109 	int sample_pos = *pos;
110 	int samples;
111 	int sample_index;
112 	int channel_index;
113 	double sample_val;
114 
115 	if (sample_pos >= scope)
116 		goto __exit;
117 
118 	samples = nbytes / (channels * 2);
119 	for (sample_index = 0; sample_index < samples; sample_index++) {
120 		for (channel_index = 0; channel_index < channels;
121 			channel_index++) {
122 			sample_val = (double)*ptr;
123 			sample_val *= (double)sample_pos;
124 			sample_val /= (double)scope;
125 			*ptr = (short)sample_val;
126 			ptr++;
127 		}
128 
129 		if (sample_pos < scope)
130 			sample_pos++;
131 
132 		if (sample_pos >= scope)
133 			break;
134 	}
135 
136 	*pos = sample_pos;
137 
138 __exit:
139 	return 0;
140 }
141 
142