11
11
#include <stdio.h>
12
12
#include <arm_math.h>
13
13
14
- #define FREQ_LIMIT_LOW 100
14
+ #define FREQ_LIMIT_LOW 100
15
15
#define FREQ_LIMIT_HIGH 10000
16
16
17
17
int tone_gen (int16_t * tone , size_t * tone_size , uint16_t tone_freq_hz , uint32_t smpl_freq_hz ,
@@ -31,7 +31,7 @@ int tone_gen(int16_t *tone, size_t *tone_size, uint16_t tone_freq_hz, uint32_t s
31
31
32
32
uint32_t samples_for_one_period = smpl_freq_hz / tone_freq_hz ;
33
33
34
- for (uint32_t i = 0 ; i < samples_for_one_period ; i ++ ) {
34
+ for (size_t i = 0 ; i < samples_for_one_period ; i ++ ) {
35
35
float curr_val = i * 2 * PI / samples_for_one_period ;
36
36
float32_t res = arm_sin_f32 (curr_val );
37
37
/* Generate one sine wave */
@@ -43,3 +43,123 @@ int tone_gen(int16_t *tone, size_t *tone_size, uint16_t tone_freq_hz, uint32_t s
43
43
44
44
return 0 ;
45
45
}
46
+
47
+ static int tone_gen_8 (int8_t * tone , size_t * tone_size , uint16_t tone_freq_hz , uint32_t smpl_freq_hz ,
48
+ uint8_t scale , uint32_t max , float amplitude )
49
+ {
50
+ uint32_t samples_for_one_period = smpl_freq_hz / tone_freq_hz ;
51
+
52
+ tone [0 ] = 0 ;
53
+
54
+ for (size_t i = 1 , j = samples_for_one_period - 1 ; i <= samples_for_one_period / 2 ;
55
+ i ++ , j -- ) {
56
+ float curr_val = i * 2 * PI / samples_for_one_period ;
57
+ float32_t res = arm_sin_f32 (curr_val );
58
+ /* Generate one sine wave */
59
+ tone [i ] = amplitude * res * max ;
60
+ tone [i ] <<= scale ;
61
+ tone [j ] = - tone [i ];
62
+ }
63
+
64
+ * tone_size = (size_t )samples_for_one_period * sizeof (int8_t );
65
+
66
+ return 0 ;
67
+ }
68
+
69
+ static int tone_gen_16 (int16_t * tone , size_t * tone_size , uint16_t tone_freq_hz ,
70
+ uint32_t smpl_freq_hz , uint8_t scale , uint32_t max , float amplitude )
71
+ {
72
+ uint32_t samples_for_one_period = smpl_freq_hz / tone_freq_hz ;
73
+
74
+ tone [0 ] = 0 ;
75
+
76
+ for (size_t i = 1 , j = samples_for_one_period - 1 ; i <= samples_for_one_period / 2 ;
77
+ i ++ , j -- ) {
78
+ float curr_val = i * 2 * PI / samples_for_one_period ;
79
+ float32_t res = arm_sin_f32 (curr_val );
80
+ /* Generate one sine wave */
81
+ tone [i ] = amplitude * res * max ;
82
+ tone [i ] <<= scale ;
83
+ tone [j ] = - tone [i ];
84
+ }
85
+
86
+ * tone_size = (size_t )samples_for_one_period * sizeof (int16_t );
87
+
88
+ return 0 ;
89
+ }
90
+
91
+ static int tone_gen_32 (int32_t * tone , size_t * tone_size , uint16_t tone_freq_hz ,
92
+ uint32_t smpl_freq_hz , uint8_t scale , int32_t max , float amplitude )
93
+ {
94
+ uint32_t samples_for_one_period = smpl_freq_hz / tone_freq_hz ;
95
+
96
+ tone [0 ] = 0 ;
97
+
98
+ for (size_t i = 1 , j = samples_for_one_period - 1 ; i <= samples_for_one_period / 2 ;
99
+ i ++ , j -- ) {
100
+ float curr_val = i * 2 * PI / samples_for_one_period ;
101
+ float32_t res = arm_sin_f32 (curr_val );
102
+ /* Generate one sine wave */
103
+ tone [i ] = amplitude * res * max ;
104
+ tone [i ] <<= scale ;
105
+ tone [j ] = - tone [i ];
106
+ }
107
+
108
+ * tone_size = (size_t )samples_for_one_period * sizeof (int32_t );
109
+
110
+ return 0 ;
111
+ }
112
+
113
+ int tone_gen_size (void * tone , size_t * tone_size , uint16_t tone_freq_hz , uint32_t sample_freq_hz ,
114
+ uint8_t sample_bits , uint8_t carrier_bits , float amplitude )
115
+ {
116
+ int ret ;
117
+ uint8_t scale ;
118
+ int32_t max ;
119
+
120
+ if (tone == NULL || tone_size == NULL ) {
121
+ return - ENXIO ;
122
+ }
123
+
124
+ if (!sample_freq_hz || tone_freq_hz < FREQ_LIMIT_LOW || tone_freq_hz > FREQ_LIMIT_HIGH ||
125
+ sample_freq_hz % tone_freq_hz ) {
126
+ return - EINVAL ;
127
+ }
128
+
129
+ if (amplitude > 1 || amplitude <= 0 ) {
130
+ return - EPERM ;
131
+ }
132
+
133
+ if (!sample_bits || !carrier_bits || sample_bits > carrier_bits ) {
134
+ return - EINVAL ;
135
+ }
136
+
137
+ if (sample_bits == 8 ) {
138
+ max = INT8_MAX ;
139
+ } else if (sample_bits == 16 ) {
140
+ max = INT16_MAX ;
141
+ } else if (sample_bits == 24 ) {
142
+ max = 0x007FFFFF ;
143
+ } else if (sample_bits == 32 ) {
144
+ max = INT32_MAX ;
145
+ } else {
146
+ return - EINVAL ;
147
+ }
148
+
149
+ scale = carrier_bits - sample_bits ;
150
+
151
+ if (carrier_bits == 8 ) {
152
+ ret = tone_gen_8 ((int8_t * )tone , tone_size , tone_freq_hz , sample_freq_hz , scale ,
153
+ max , amplitude );
154
+ } else if (carrier_bits == 16 ) {
155
+ ret = tone_gen_16 ((int16_t * )tone , tone_size , tone_freq_hz , sample_freq_hz , scale ,
156
+ max , amplitude );
157
+ } else if (carrier_bits == 32 ) {
158
+ ret = tone_gen_32 ((int32_t * )tone , tone_size , tone_freq_hz , sample_freq_hz , scale ,
159
+ max , amplitude );
160
+ } else {
161
+ return - EINVAL ;
162
+ }
163
+
164
+ return 0 ;
165
+ }
0 commit comments