session-ios/Signal/test/audio/AudioStretcherTest.m

58 lines
8.4 KiB
Objective-C

#import <XCTest/XCTest.h>
#import "TestUtil.h"
#import "AudioStretcher.h"
#import "time_scale.h"
@interface AudioStretcherTest : XCTestCase
@end
@implementation AudioStretcherTest
-(void) testStretchAudioStretches {
for (NSNumber* s in @[@0.5, @1.0, @1.5]) {
NSUInteger inputSampleCount = 8000;
double stretch = [s doubleValue];
double freq = 300;
AudioStretcher* a = [AudioStretcher audioStretcher];
NSData* inputData = sineWave(freq, 8000, 8000);
NSData* outputData = [a stretchAudioData:inputData stretchFactor:stretch];
NSUInteger outputSampleCount = outputData.length/sizeof(int16_t);
if ([s doubleValue] == 1) {
test([inputData isEqualToData:outputData]);
}
double ratio = outputSampleCount / (double)inputSampleCount / stretch;
test(ratio > 0.95 && ratio < 1.05);
}
}
-(void) test_canary_NotifyWhenBufferOverflowProtectionNoLongerNeeded {
const int n = 3;
const int w = 320;
short inputs[n][w] = {
{29593, 25345, 5871, 23036, -7921, 22600, -2450, 26816, 6028, -31281, -11191, -24190, 17105, -1747, -18221, 24834, -13982, 10205, -1368, 22550, -21512, -2823, -29879, 24782, 19460, 5414, 30806, 8476, -12498, -22518, 20241, -1965, -26719, 8553, 29847, -583, 24945, 24205, 10621, -11207, 27652, -17364, -27913, -2355, 11277, 9590, 8674, -31100, -10774, -24770, -16868, -28454, -22725, 11867, -28941, -5457, -5315, -19396, 32704, 18380, -21602, 5413, 6845, 20456, 18733, 7545, -7905, -1497, -26965, -19259, 7667, 14198, 4469, 1407, 32393, 9149, -2689, -20581, -32092, 19779, 21708, 18827, 21402, 20224, -18782, -12377, 16262, -19360, -9738, -16557, 20938, -11333, 25689, 8905, 2075, 4803, -15729, 6786, 22270, -28718, 21090, 6076, -19868, -1166, 8667, -23357, -22894, -15858, -22665, -26901, 2442, -7185, 3300, -18683, -14602, 13849, 26092, 24149, -1319, -9217, 9503, -15344, 11839, 3023, -10796, -24677, -21107, -29135, 17020, -27703, 9287, 23388, -22669, 18299, -145, -15371, -12090, -16524, 16310, -5061, 466, -31532, 10136, -15088, -11506, 32315, -14472, -6147, -11109, -4070, 18645, 25105, -30333, -25597, -22902, -27084, 29528, -29820, 31182, 8621, 2347, -14475, -30841, -4303, -14232, 21256, -27047, -10944, 3839, 14591, 25596, 22680, -10, -7082, -27586, 16623, 24035, 25903, -21242, 15530, -5809, -25675, 7863, -8833, 22340, -10642, 15490, -15517, 6493, 19786, -7243, -10804, -18438, 20666, -12891, -20450, -11876, 1342, -8325, 403, 6175, 477, -10150, 7929, -27228, 27425, 14396, 7585, 24801, 24254, 27037, 15259, 30575, -30350, 16330, -15511, -7608, 9911, -19101, 2909, -11012, -8427, -24594, -10304, -4200, 27065, -26258, -10258, -19863, -2951, -13571, 6243, 8130, -7005, -1271, 9074, 28367, 22257, 28786, 9821, 22175, -18788, -12534, 12537, 7825, -15617, 5602, -20364, 20336, 24508, -6161, 14067, -11290, 13569, -8258, -18446, 8148, -28258, -5123, 10530, 21888, 25441, -4722, 28105, 24214, -23702, -22384, -12081, -21275, -17067, -19690, -25240, -14455, 12121, -9167, 5625, 18616, -29093, -18756, -28542, 2511, -30102, 26452, 18298, 5483, 30320, 29931, 275, -8046, 6160, 9664, 25936, -20862, 5699, -12450, 31748, -25558, 30359, 9445, 26397, 1487, 21035, -1563, -17300, 17430, -18614, 4537, -30211, -15, 4653, 6498, 29555, -18415, 22209, -14577, 10007, 9679, -8446, -31521, -1745},
{-13708, -20644, 9494, -13923, -3450, -32647, 25071, 8566, 10161, 20212, -30857, -10576, -19547, -2302, -19539, 8808, -5873, 22604, 24253, 9994, 3417, 25212, -26787, -1657, 23364, 14145, -20434, -16627, 19833, -16481, -13187, 21370, 9922, -5474, -814, 10920, -20644, 6911, -428, -30320, -16378, 1619, -25419, 21595, 2345, 14553, 8422, 20712, 13058, -10788, -3304, 19492, -31510, -2232, -22280, 4482, -83, -13718, 30882, 28284, -25419, -20965, 3923, 28932, 4027, -4243, -20099, 17757, -27741, 25280, 28916, -26974, 9034, -17958, -11100, -21633, 10280, 24876, 27652, -5246, 6707, 30913, 22165, 15203, 32050, -27096, 29795, -22790, -13534, 6473, -10304, 15600, 14299, 6910, -4858, -10597, -336, -22715, -17639, 27749, -22381, 22605, 570, -20333, 15693, -28071, -35, 22245, -11820, -22534, 8788, -5860, 29496, 2234, -3963, 2580, -31041, 15319, 11667, -1150, -15659, 21725, 5267, 131, 30220, -9252, -28260, 7489, -4618, 11864, -3871, -8927, -14728, -18047, 25562, -19154, -25242, 10921, 12856, 8459, -25105, -19635, 2454, -12726, -24101, -18101, 15365, 10029, 21503, 29075, -25031, -9718, -27039, -26525, -18829, 29668, 5664, -8531, -32610, -12659, 9892, -14240, 10271, 31035, 9542, 20606, 12334, 4083, -26853, -11863, 32353, -15219, 1579, 1816, 19711, -121, 4238, -382, 31478, 23744, -16349, -30272, -4266, -18368, -6383, -1016, 17975, -1090, -8103, -8099, 19597, -13337, 26524, 23105, 17176, 20066, 30117, -28017, 10310, -23647, 16879, -17511, -2410, -13080, 2089, 15261, -31333, -25179, 22969, -15372, -3088, -12631, -12777, 3828, 10504, -8861, 28023, -27242, 29366, -14064, 513, 5472, -12926, -24590, -4788, -31203, -15766, -6974, 7418, -20560, 23817, 21833, 13696, -31139, 22244, -10864, 11052, -30015, -27204, -5550, -26021, -8028, 13471, -3768, -22474, 29751, -9599, -11744, 7005, 3897, 28276, -18134, -19427, -9116, -24083, -1655, -10861, 254, 29915, 10604, 14639, 3693, 18836, -24341, 26980, -12318, -20334, 24271, 29876, 29040, -7750, -15486, -8596, -6137, 5236, 20834, -26842, 110, -2314, -31628, 19293, 25939, -14458, -8694, -28016, 14169, -825, 24375, -31768, 15894, 26257, 1579, 30235, 1386, 9644, 23804, -29970, 12280, 10681, -7203, -11583, -2987, 29261, 8299, 5895, -11543, -12758, 28031, 21916, 23135, -7399, -8482, -4973, 4219, 20574, -23296, -31976, -22138, 15988, -19405},
{-24948, -31416, 5362, 20375, 11055, -10144, -22488, -2942, 20790, 22398, -9709, 22087, -6155, -28, -30823, 32355, 5956, 27338, 15878, -2948, 1665, 65, 10405, 1528, -24667, -7417, 11863, 25980, -29917, 17610, 9288, 23745, 13186, -1105, 6001, -29219, -7987, -28899, -18436, 4503, -25648, -9290, 18145, -27660, 10996, 15022, -21563, 15577, 12429, -19359, -104, 11514, -5940, 2312, 27759, 9714, 26123, -8087, 15353, 20627, 11076, -27766, -7964, -9225, 12444, 6173, 7953, -23533, 20667, 26852, 29278, 6963, -1371, -12199, -31615, 20062, 12818, 21950, 12111, -27254, 8585, -20792, -30604, 13840, 20475, -19035, 26171, -13110, -28474, -13822, -26517, -21565, 6202, -9472, 17385, 15256, 10472, -6225, -4036, -1361, 13119, -10097, -14389, -18678, -10291, 841, 6774, -5896, -933, 8581, 2802, -1508, -18718, -16395, -30151, 23229, -23248, 26181, -19744, 28215, -2758, -16446, 27601, -15258, -14430, -10390, 28862, -25823, -11705, 31428, -5072, -14562, -29871, 4749, -11489, 16362, -15884, -7011, -5196, -10523, -1406, -25350, 19435, 20105, -11971, -23387, 9568, -7451, -4064, 19739, 18617, 27237, -10988, 1944, -26883, 17819, 29127, 13877, 30445, 10966, 14579, 23318, -24158, 31238, 24370, -2465, 7937, 26528, -16501, 145, 24192, 8915, -28816, 21434, -16877, -32258, 6299, -23867, -9356, -16269, 26760, -6318, -32420, -20761, -32650, 13681, -1888, -26285, -12860, 4344, -27211, 30924, -20588, -12267, 21668, -16287, -8375, -12445, -29227, -21916, 24257, -14884, -10715, 26480, -24345, -28872, -11907, -162, 20348, -16375, -12813, 24876, 19978, -32695, -2351, 29274, -7489, 1043, -21655, 1813, -7605, 7185, 6124, -4327, 1872, -3739, 409, 4190, 4269, -2791, -2367, -1235, 6986, 30000, -16223, 31122, 13832, -19600, 8654, 26173, 15660, 156, 10761, -29322, 75, -3676, 29399, 12568, 14374, -21264, 9528, -4725, -12452, 13783, 29590, -9291, -11153, -25868, 2669, 27689, 22907, 31630, -27195, -16591, 28549, 9759, -273, -32661, -12164, -6473, 22601, 16161, -8908, -8464, -2136, 10480, 5422, -22522, -5863, -30754, -146, 5198, -7935, 578, 15279, 13933, -18002, -5182, -30120, -22862, 8570, 17750, 13824, 11566, -28101, 22769, -29638, 10678, 290, -29299, -25085, 7119, -21888, 21641, 4260, -10331, 9152, -26694, -191, -21253, 3730, 25843, 23825, 20304, -30431, -31927, -25894, 637, 22325, -3515}
};
struct time_scale_state_s s;
int b = w*10;
short outputs[b];
bool underTheLimit = true;
test(time_scale_init(&s, 8000, 0.8f) != NULL);
for (int i = 0; i < n; i++) {
test(underTheLimit);
test(time_scale_rate(&s, 0.8f) == 0);
int m = time_scale_max_output_len(&s, w);
test(m <= b);
int a = time_scale(&s, outputs, inputs[i], w);
underTheLimit &= a <= m;
}
// If this starts failing, great! The spandsp buffer overflow might be fixed. Do some fuzz testing to make sure, though.
// If the bug is fixed, remove the extra buffer allocation in AudioStretcher.
test(!underTheLimit);
}
@end