-
Notifications
You must be signed in to change notification settings - Fork 28
/
mixer_effects.t
240 lines (209 loc) · 6.06 KB
/
mixer_effects.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/perl -w
use strict;
use warnings;
use SDL;
use SDL::Config;
my $audiodriver;
BEGIN {
use Config;
if ( !$Config{'useithreads'} ) {
print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
exit(0);
}
require threads;
require threads::shared;
use Test::More;
use lib 't/lib';
use SDL::TestTool;
$audiodriver = $ENV{SDL_AUDIODRIVER};
$ENV{SDL_AUDIODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};
if ( !SDL::TestTool->init(SDL_INIT_AUDIO) ) {
plan( skip_all => 'Failed to init sound' );
} elsif ( !SDL::Config->has('SDL_mixer') ) {
plan( skip_all => 'SDL_mixer support not compiled' );
}
}
use SDL::Mixer;
use SDL::Mixer::Channels;
use SDL::Mixer::Effects;
use SDL::Mixer::Samples;
my $can_open = SDL::Mixer::open_audio( 44100, SDL::Audio::AUDIO_S16SYS, 2, 4096 );
unless($can_open == 0)
{
plan( skip_all => 'Cannot open audio :'.SDL::get_error() );
}
is( $can_open ,
0, '[open_audio] ran'
);
my $delay = 500;
my $audio_test_file = 'test/data/silence.wav';
SDL::Mixer::Channels::volume( -1, 1 );
if ( $ENV{'SDL_RELEASE_TESTING'} ) {
SDL::Mixer::Channels::volume( -1, 20 );
$delay = 1000;
$audio_test_file = 'test/data/sample.wav';
}
my $effect_func_called : shared = 0;
my $effect_done_called : shared = 0;
my @last_stream = ();
sub echo_effect_func {
my $channel = shift;
my $samples = shift;
my $position = shift;
my @stream = @_;
$effect_func_called++;
printf(
"[effect_func] callback: channel=%2s, position=%8s, samples=%6s\n",
$channel, $position, scalar(@stream)
);
my @stream2 = @stream;
my $offset = $samples / 2;
for ( my $i = 0; $i < $samples; $i += 2 ) {
if ( $i < $offset ) {
if ( scalar(@last_stream) == $samples ) {
$stream2[$i] = $stream[$i] * 0.6 + $last_stream[ $samples + $i - $offset ] * 0.4; # left
$stream2[ $i + 1 ] =
$stream[ $i + 1 ] * 0.6 + $last_stream[ $samples + $i - $offset + 1 ] * 0.4; # right
}
} else {
$stream2[$i] = $stream[$i] * 0.6 + $stream[ $i - $offset ] * 0.4; # left
$stream2[ $i + 1 ] = $stream[ $i + 1 ] * 0.6 + $stream[ $i - $offset + 1 ] * 0.4; # right
}
}
@last_stream = @stream;
push( @stream2, $position + $samples );
return @stream2;
}
sub echo_effect_func2 {
my $channel = shift;
my $samples = shift;
my $position = shift;
my @stream = @_;
$effect_func_called++;
printf(
"[effect_func2] callback: channel=%2s, position=%8s, samples=%6s\n",
$channel, $position, scalar(@stream)
);
push( @stream, $position + $samples );
return @stream;
}
sub effect_done2 {
printf("[effect_done2] called\n");
$effect_done_called++;
}
sub effect_done {
printf("[effect_done] called\n");
$effect_done_called++;
}
my $sample_chunk = SDL::Mixer::Samples::load_WAV($audio_test_file);
my $playing_channel = SDL::Mixer::Channels::play_channel( -1, $sample_chunk, -1 );
is( $playing_channel >= 0, 1, "[play_channel] playing $audio_test_file" );
SDL::delay($delay);
my $effect_id = SDL::Mixer::Effects::register(
$playing_channel, "main::echo_effect_func2",
"main::effect_done2", 0
);
isnt( $effect_id, -1, '[register] registerering echo effect callback' );
SDL::delay($delay);
my $check = SDL::Mixer::Effects::unregister( $playing_channel, $effect_id );
isnt(
$check, 0,
'[unregister] unregistering effect_func will call effect_done'
);
SDL::delay(200);
is( $effect_func_called > 0,
1, "[effect_func] called $effect_func_called times"
);
is( $effect_done_called > 0,
1, "[effect_done] called $effect_done_called times"
);
SDL::delay($delay);
$effect_func_called = 0;
$effect_done_called = 0;
$effect_id = SDL::Mixer::Effects::register(
$playing_channel, "main::echo_effect_func2",
"main::effect_done2", 0
);
isnt( $effect_id, -1, '[register] registerering echo effect callback' );
SDL::delay($delay);
$check = SDL::Mixer::Effects::unregister( $playing_channel, $effect_id );
isnt(
$check, 0,
'[unregister] unregistering effect_func will call effect_done'
);
SDL::delay(200);
is( $effect_func_called > 0,
1, "[effect_func] called $effect_func_called times"
);
is( $effect_done_called > 0,
1, "[effect_done] called $effect_done_called times"
);
$effect_func_called = 0;
$effect_done_called = 0;
my $effect_id_all = SDL::Mixer::Effects::register( MIX_CHANNEL_POST, "main::echo_effect_func",
"main::effect_done", 0
);
isnt( $effect_id_all, -1, '[register] registerering echo effect callback' );
SDL::delay($delay);
isnt(
SDL::Mixer::Effects::unregister_all(MIX_CHANNEL_POST),
0, '[unregister_all] unregistering all will call effect_done'
);
SDL::delay(200);
is( $effect_func_called > 0,
1, "[effect_func] called $effect_func_called times"
);
is( $effect_done_called > 0,
1, "[effect_done] called $effect_done_called times"
);
$effect_func_called = 0;
is( SDL::Mixer::Effects::set_post_mix( "main::echo_effect_func", 0 ),
undef, '[set_post_mix] registering echo effect callback'
);
SDL::delay($delay);
is( SDL::Mixer::Effects::set_post_mix(),
undef, '[set_post_mix] unregistering echo effect callback'
);
SDL::delay(200);
is( $effect_func_called > 0,
1, "[effect_func] called $effect_func_called times"
);
SDL::delay($delay);
isnt(
SDL::Mixer::Effects::set_panning( $playing_channel, 128, 255 ),
0, '[set_panning] 50% left, 100% right'
);
SDL::delay($delay);
isnt(
SDL::Mixer::Effects::set_position( $playing_channel, 225, 80 ),
0, '[set_position] left-behind, 33% away'
);
SDL::delay($delay);
isnt(
SDL::Mixer::Effects::set_distance( $playing_channel, 160 ),
0, '[set_distance] 66% away'
);
SDL::delay($delay);
isnt(
SDL::Mixer::Effects::set_position( $playing_channel, 0, 0 ),
0, '[set_position] front, 0% away'
);
SDL::delay($delay);
isnt(
SDL::Mixer::Effects::set_reverse_stereo( $playing_channel, 1 ),
0, '[set_reverse_stereo] on'
);
SDL::delay($delay);
isnt(
SDL::Mixer::Effects::set_reverse_stereo( $playing_channel, 0 ),
0, '[set_reverse_stereo] off'
);
SDL::delay($delay);
SDL::Mixer::close_audio();
pass '[close_audio] ran';
if ($audiodriver) {
$ENV{SDL_AUDIODRIVER} = $audiodriver;
} else {
delete $ENV{SDL_AUDIODRIVER};
}
done_testing();