/src/mozilla-central/media/libcubeb/gtest/test_audio.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2013 Sebastien Alaiwan <sebastien.alaiwan@gmail.com> |
3 | | * |
4 | | * This program is made available under an ISC-style license. See the |
5 | | * accompanying file LICENSE for details. |
6 | | */ |
7 | | |
8 | | /* libcubeb api/function exhaustive test. Plays a series of tones in different |
9 | | * conditions. */ |
10 | | #include "gtest/gtest.h" |
11 | | #if !defined(_XOPEN_SOURCE) |
12 | | #define _XOPEN_SOURCE 600 |
13 | | #endif |
14 | | #include <stdio.h> |
15 | | #include <stdlib.h> |
16 | | #include <math.h> |
17 | | #include <memory> |
18 | | #include <string.h> |
19 | | #include "cubeb/cubeb.h" |
20 | | #include "common.h" |
21 | | #include <string> |
22 | | |
23 | | using namespace std; |
24 | | |
25 | 0 | #define MAX_NUM_CHANNELS 32 |
26 | | |
27 | | #if !defined(M_PI) |
28 | | #define M_PI 3.14159265358979323846 |
29 | | #endif |
30 | | |
31 | 0 | #define VOLUME 0.2 |
32 | | |
33 | | float get_frequency(int channel_index) |
34 | 0 | { |
35 | 0 | return 220.0f * (channel_index+1); |
36 | 0 | } |
37 | | |
38 | | template<typename T> T ConvertSample(double input); |
39 | 0 | template<> float ConvertSample(double input) { return input; } |
40 | 0 | template<> short ConvertSample(double input) { return short(input * 32767.0f); } |
41 | | |
42 | | /* store the phase of the generated waveform */ |
43 | | struct synth_state { |
44 | | synth_state(int num_channels_, float sample_rate_) |
45 | | : num_channels(num_channels_), |
46 | | sample_rate(sample_rate_) |
47 | 0 | { |
48 | 0 | for(int i=0;i < MAX_NUM_CHANNELS;++i) |
49 | 0 | phase[i] = 0.0f; |
50 | 0 | } |
51 | | |
52 | | template<typename T> |
53 | | void run(T* audiobuffer, long nframes) |
54 | 0 | { |
55 | 0 | for(int c=0;c < num_channels;++c) { |
56 | 0 | float freq = get_frequency(c); |
57 | 0 | float phase_inc = 2.0 * M_PI * freq / sample_rate; |
58 | 0 | for(long n=0;n < nframes;++n) { |
59 | 0 | audiobuffer[n*num_channels+c] = ConvertSample<T>(sin(phase[c]) * VOLUME); |
60 | 0 | phase[c] += phase_inc; |
61 | 0 | } |
62 | 0 | } |
63 | 0 | } Unexecuted instantiation: void synth_state::run<float>(float*, long) Unexecuted instantiation: void synth_state::run<short>(short*, long) |
64 | | |
65 | | private: |
66 | | int num_channels; |
67 | | float phase[MAX_NUM_CHANNELS]; |
68 | | float sample_rate; |
69 | | }; |
70 | | |
71 | | template<typename T> |
72 | | long data_cb(cubeb_stream * /*stream*/, void * user, const void * /*inputbuffer*/, void * outputbuffer, long nframes) |
73 | 0 | { |
74 | 0 | synth_state *synth = (synth_state *)user; |
75 | 0 | synth->run((T*)outputbuffer, nframes); |
76 | 0 | return nframes; |
77 | 0 | } Unexecuted instantiation: long data_cb<float>(cubeb_stream*, void*, void const*, void*, long) Unexecuted instantiation: long data_cb<short>(cubeb_stream*, void*, void const*, void*, long) |
78 | | |
79 | | void state_cb_audio(cubeb_stream * /*stream*/, void * /*user*/, cubeb_state /*state*/) |
80 | 0 | { |
81 | 0 | } |
82 | | |
83 | | /* Our android backends don't support float, only int16. */ |
84 | | int supports_float32(string backend_id) |
85 | 0 | { |
86 | 0 | return backend_id != "opensl" |
87 | 0 | && backend_id != "audiotrack"; |
88 | 0 | } |
89 | | |
90 | | /* Some backends don't have code to deal with more than mono or stereo. */ |
91 | | int supports_channel_count(string backend_id, int nchannels) |
92 | 0 | { |
93 | 0 | return nchannels <= 2 || |
94 | 0 | (backend_id != "opensl" && backend_id != "audiotrack"); |
95 | 0 | } |
96 | | |
97 | | int run_test(int num_channels, int sampling_rate, int is_float) |
98 | 0 | { |
99 | 0 | int r = CUBEB_OK; |
100 | 0 |
|
101 | 0 | cubeb *ctx = NULL; |
102 | 0 |
|
103 | 0 | r = common_init(&ctx, "Cubeb audio test: channels"); |
104 | 0 | if (r != CUBEB_OK) { |
105 | 0 | fprintf(stderr, "Error initializing cubeb library\n"); |
106 | 0 | return r; |
107 | 0 | } |
108 | 0 | std::unique_ptr<cubeb, decltype(&cubeb_destroy)> |
109 | 0 | cleanup_cubeb_at_exit(ctx, cubeb_destroy); |
110 | 0 |
|
111 | 0 | const char * backend_id = cubeb_get_backend_id(ctx); |
112 | 0 |
|
113 | 0 | if ((is_float && !supports_float32(backend_id)) || |
114 | 0 | !supports_channel_count(backend_id, num_channels)) { |
115 | 0 | /* don't treat this as a test failure. */ |
116 | 0 | return CUBEB_OK; |
117 | 0 | } |
118 | 0 | |
119 | 0 | fprintf(stderr, "Testing %d channel(s), %d Hz, %s (%s)\n", num_channels, sampling_rate, is_float ? "float" : "short", cubeb_get_backend_id(ctx)); |
120 | 0 |
|
121 | 0 | cubeb_stream_params params; |
122 | 0 | params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16NE; |
123 | 0 | params.rate = sampling_rate; |
124 | 0 | params.channels = num_channels; |
125 | 0 | params.layout = CUBEB_LAYOUT_UNDEFINED; |
126 | 0 | params.prefs = CUBEB_STREAM_PREF_NONE; |
127 | 0 |
|
128 | 0 | synth_state synth(params.channels, params.rate); |
129 | 0 |
|
130 | 0 | cubeb_stream *stream = NULL; |
131 | 0 | r = cubeb_stream_init(ctx, &stream, "test tone", NULL, NULL, NULL, ¶ms, |
132 | 0 | 4096, is_float ? &data_cb<float> : &data_cb<short>, state_cb_audio, &synth); |
133 | 0 | if (r != CUBEB_OK) { |
134 | 0 | fprintf(stderr, "Error initializing cubeb stream: %d\n", r); |
135 | 0 | return r; |
136 | 0 | } |
137 | 0 | |
138 | 0 | std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)> |
139 | 0 | cleanup_stream_at_exit(stream, cubeb_stream_destroy); |
140 | 0 |
|
141 | 0 | cubeb_stream_start(stream); |
142 | 0 | delay(200); |
143 | 0 | cubeb_stream_stop(stream); |
144 | 0 |
|
145 | 0 | return r; |
146 | 0 | } |
147 | | |
148 | | int run_panning_volume_test(int is_float) |
149 | 0 | { |
150 | 0 | int r = CUBEB_OK; |
151 | 0 |
|
152 | 0 | cubeb *ctx = NULL; |
153 | 0 |
|
154 | 0 | r = common_init(&ctx, "Cubeb audio test"); |
155 | 0 | if (r != CUBEB_OK) { |
156 | 0 | fprintf(stderr, "Error initializing cubeb library\n"); |
157 | 0 | return r; |
158 | 0 | } |
159 | 0 | |
160 | 0 | std::unique_ptr<cubeb, decltype(&cubeb_destroy)> |
161 | 0 | cleanup_cubeb_at_exit(ctx, cubeb_destroy); |
162 | 0 |
|
163 | 0 | const char * backend_id = cubeb_get_backend_id(ctx); |
164 | 0 |
|
165 | 0 | if ((is_float && !supports_float32(backend_id))) { |
166 | 0 | /* don't treat this as a test failure. */ |
167 | 0 | return CUBEB_OK; |
168 | 0 | } |
169 | 0 | |
170 | 0 | cubeb_stream_params params; |
171 | 0 | params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16NE; |
172 | 0 | params.rate = 44100; |
173 | 0 | params.channels = 2; |
174 | 0 | params.layout = CUBEB_LAYOUT_STEREO; |
175 | 0 | params.prefs = CUBEB_STREAM_PREF_NONE; |
176 | 0 |
|
177 | 0 | synth_state synth(params.channels, params.rate); |
178 | 0 |
|
179 | 0 | cubeb_stream *stream = NULL; |
180 | 0 | r = cubeb_stream_init(ctx, &stream, "test tone", NULL, NULL, NULL, ¶ms, |
181 | 0 | 4096, is_float ? &data_cb<float> : &data_cb<short>, |
182 | 0 | state_cb_audio, &synth); |
183 | 0 | if (r != CUBEB_OK) { |
184 | 0 | fprintf(stderr, "Error initializing cubeb stream: %d\n", r); |
185 | 0 | return r; |
186 | 0 | } |
187 | 0 | |
188 | 0 | std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)> |
189 | 0 | cleanup_stream_at_exit(stream, cubeb_stream_destroy); |
190 | 0 |
|
191 | 0 | fprintf(stderr, "Testing: volume\n"); |
192 | 0 | for(int i=0;i <= 4; ++i) |
193 | 0 | { |
194 | 0 | fprintf(stderr, "Volume: %d%%\n", i*25); |
195 | 0 |
|
196 | 0 | cubeb_stream_set_volume(stream, i/4.0f); |
197 | 0 | cubeb_stream_start(stream); |
198 | 0 | delay(400); |
199 | 0 | cubeb_stream_stop(stream); |
200 | 0 | delay(100); |
201 | 0 | } |
202 | 0 |
|
203 | 0 | fprintf(stderr, "Testing: panning\n"); |
204 | 0 | for(int i=-4;i <= 4; ++i) |
205 | 0 | { |
206 | 0 | fprintf(stderr, "Panning: %.2f\n", i/4.0f); |
207 | 0 |
|
208 | 0 | cubeb_stream_set_panning(stream, i/4.0f); |
209 | 0 | cubeb_stream_start(stream); |
210 | 0 | delay(400); |
211 | 0 | cubeb_stream_stop(stream); |
212 | 0 | delay(100); |
213 | 0 | } |
214 | 0 |
|
215 | 0 | return r; |
216 | 0 | } |
217 | | |
218 | | TEST(cubeb, run_panning_volume_test_short) |
219 | 0 | { |
220 | 0 | ASSERT_EQ(run_panning_volume_test(0), CUBEB_OK); |
221 | 0 | } |
222 | | |
223 | | TEST(cubeb, run_panning_volume_test_float) |
224 | 0 | { |
225 | 0 | ASSERT_EQ(run_panning_volume_test(1), CUBEB_OK); |
226 | 0 | } |
227 | | |
228 | | TEST(cubeb, run_channel_rate_test) |
229 | 0 | { |
230 | 0 | unsigned int channel_values[] = { |
231 | 0 | 1, |
232 | 0 | 2, |
233 | 0 | 3, |
234 | 0 | 4, |
235 | 0 | 6, |
236 | 0 | }; |
237 | 0 |
|
238 | 0 | int freq_values[] = { |
239 | 0 | 16000, |
240 | 0 | 24000, |
241 | 0 | 44100, |
242 | 0 | 48000, |
243 | 0 | }; |
244 | 0 |
|
245 | 0 | for(auto channels : channel_values) { |
246 | 0 | for(auto freq : freq_values) { |
247 | 0 | ASSERT_TRUE(channels < MAX_NUM_CHANNELS); |
248 | 0 | fprintf(stderr, "--------------------------\n"); |
249 | 0 | ASSERT_EQ(run_test(channels, freq, 0), CUBEB_OK); |
250 | 0 | ASSERT_EQ(run_test(channels, freq, 1), CUBEB_OK); |
251 | 0 | } |
252 | 0 | } |
253 | 0 | } |