void generate_music(short *buf, opus_int32 len) { opus_int32 a1,b1,a2,b2; opus_int32 c1,c2,d1,d2; opus_int32 i,j; a1=b1=a2=b2=0; c1=c2=d1=d2=0; j=0; /*60ms silence*/ for(i=0;i<2880;i++)buf[i*2]=buf[i*2+1]=0; for(i=2880;i<len;i++) { opus_uint32 r; opus_int32 v1,v2; v1=v2=(((j*((j>>12)^((j>>10|j>>12)&26&j>>7)))&128)+128)<<15; r=fast_rand();v1+=r&65535;v1-=r>>16; r=fast_rand();v2+=r&65535;v2-=r>>16; b1=v1-a1+((b1*61+32)>>6);a1=v1; b2=v2-a2+((b2*61+32)>>6);a2=v2; c1=(30*(c1+b1+d1)+32)>>6;d1=b1; c2=(30*(c2+b2+d2)+32)>>6;d2=b2; v1=(c1+128)>>8; v2=(c2+128)>>8; buf[i*2]=v1>32767?32767:(v1<-32768?-32768:v1); buf[i*2+1]=v2>32767?32767:(v2<-32768?-32768:v2); if(i%6==0)j++; } }
static void emptyhanded_move (int ant, unsigned x, unsigned y) { heading[ant] = ((15 < scent[ant] ? follow_gradient_unsigned (scent, heading[ant], x, y) : heading[ant]) + (fast_rand () % 3) - 1) % 8; { unsigned neighbor = move2 (x, y, heading[ant]); if (grid[neighbor] == food) { grid[ant] = carrying; gland[ant] = 16000; } else if (grid[neighbor] != empty) { heading[ant] = fast_rand () % 8; return; } grid[neighbor] = grid[ant]; heading[neighbor] = heading[ant]; grid[ant] = empty; } }
/* This implements a 16 bit quantization with full triangular dither and IIR noise shaping. The noise shaping filters were designed by Sebastian Gesemann based on the LAME ATH curves with flattening to limit their peak gain to 20 dB. (Everyone elses' noise shaping filters are mildly crazy) The 48kHz version of this filter is just a warped version of the 44.1kHz filter and probably could be improved by shifting the HF shelf up in frequency a little bit since 48k has a bit more room and being more conservative against bat-ears is probably more important than more noise suppression. This process can increase the peak level of the signal (in theory by the peak error of 1.5 +20 dB though this much is unobservable rare) so to avoid clipping the signal is attenuated by a couple thousandths of a dB. Initially the approach taken here was to only attenuate by the 99.9th percentile, making clipping rare but not impossible (like SoX) but the limited gain of the filter means that the worst case was only two thousandths of a dB more, so this just uses the worst case. The attenuation is probably also helpful to prevent clipping in the DAC reconstruction filters or downstream resampling in any case.*/ static inline void shape_dither_toshort(shapestate *_ss, short *_o, float *_i, int _n, int _CC) { const float gains[3]={32768.f-15.f,32768.f-15.f,32768.f-3.f}; const float fcoef[3][8] = { {2.2374f, -.7339f, -.1251f, -.6033f, 0.9030f, .0116f, -.5853f, -.2571f}, /* 48.0kHz noise shaping filter sd=2.34*/ {2.2061f, -.4706f, -.2534f, -.6214f, 1.0587f, .0676f, -.6054f, -.2738f}, /* 44.1kHz noise shaping filter sd=2.51*/ {1.0000f, 0.0000f, 0.0000f, 0.0000f, 0.0000f,0.0000f, 0.0000f, 0.0000f}, /* lowpass noise shaping filter sd=0.65*/ }; int i; int rate=_ss->fs==44100?1:(_ss->fs==48000?0:2); float gain=gains[rate]; float *b_buf; float *a_buf; int mute=_ss->mute; b_buf=_ss->b_buf; a_buf=_ss->a_buf; /*In order to avoid replacing digital silence with quiet dither noise we mute if the output has been silent for a while*/ if(mute>64) memset(a_buf,0,sizeof(float)*_CC*4); for(i=0;i<_n;i++) { int c; int pos = i*_CC; int silent=1; for(c=0;c<_CC;c++) { int j, si; float r,s,err=0; silent&=_i[pos+c]==0; s=_i[pos+c]*gain; for(j=0;j<4;j++) err += fcoef[rate][j]*b_buf[c*4+j] - fcoef[rate][j+4]*a_buf[c*4+j]; memmove(&a_buf[c*4+1],&a_buf[c*4],sizeof(float)*3); memmove(&b_buf[c*4+1],&b_buf[c*4],sizeof(float)*3); a_buf[c*4]=err; s = s - err; r=(float)fast_rand()*(1/(float)UINT_MAX) - (float)fast_rand()*(1/(float)UINT_MAX); if (mute>16)r=0; /*Clamp in float out of paranoia that the input will be >96 dBFS and wrap if the integer is clamped.*/ _o[pos+c] = si = float2int(fmaxf(-32768,fminf(s + r,32767))); /*Including clipping in the noise shaping is generally disastrous: the futile effort to restore the clipped energy results in more clipping. However, small amounts-- at the level which could normally be created by dither and rounding-- are harmless and can even reduce clipping somewhat due to the clipping sometimes reducing the dither+rounding error.*/ b_buf[c*4] = (mute>16)?0:fmaxf(-1.5f,fminf(si - s,1.5f)); } mute++; if(!silent)mute=0; } _ss->mute=MINI(mute,960); }
int main(int _argc, char **_argv) { const char * oversion; const char * env_seed; int env_used; if(_argc>2) { fprintf(stderr,"Usage: %s [<seed>]\n",_argv[0]); return 1; } env_used=0; env_seed=getenv("SEED"); if(_argc>1)iseed=atoi(_argv[1]); else if(env_seed) { iseed=atoi(env_seed); env_used=1; } else iseed=(opus_uint32)time(NULL)^((getpid()&65535)<<16); Rw=Rz=iseed; oversion=opus_get_version_string(); if(!oversion)test_failed(); fprintf(stderr,"Testing %s decoder. Random seed: %u (%.4X)\n", oversion, iseed, fast_rand() % 65535); if(env_used)fprintf(stderr," Random seed set from the environment (SEED=%s).\n", env_seed); /*Setting TEST_OPUS_NOFUZZ tells the tool not to send garbage data into the decoders. This is helpful because garbage data may cause the decoders to clip, which angers CLANG IOC.*/ test_decoder_code0(getenv("TEST_OPUS_NOFUZZ")!=NULL); return 0; }
static INLINE unsigned pick_lesser (unsigned v0, unsigned u0, unsigned v1, unsigned u1) { if (v0 < v1 || (v0 == v1 && fast_rand () & 1)) return u0; else return u1; }
//Returns new vector, with elements generated at random using given seed int64_t* random_vector(int64_t seed) { int64_t* vector = new_vector(); set_seed(seed); for (int64_t i = 0; i < g_length; i++) { vector[i] = fast_rand(); } return vector; }
/** * Returns new matrix with elements generated at random using given seed */ uint32_t* random_matrix(uint32_t seed) { uint32_t* matrix = new_matrix(); set_seed(seed); for (ssize_t i = 0; i < g_elements; i++) { matrix[i] = fast_rand(); } return matrix; }
static void carrying_move (int ant, unsigned x, unsigned y) { unsigned dir = uphill (x, y, heading[ant]); dir = (dir + fast_rand () % 3 - 1) % 8; heading[ant] = dir; { unsigned neighbor = move2 (x, y, dir); if (grid[neighbor] != empty) heading[ant] = fast_rand () % 8; else if (in_nest (x, y) && fast_rand () % 4 == 0) { grid[neighbor] = food; grid[ant] = emptyhanded; heading[ant] = (dir + 4) % 8; /* turn around */ } else { if (!in_nest (x, y)) { int gl = gland[ant]; if (0 < gl) { scent[ant] += gl; gland[ant] -= 128; } } grid[neighbor] = carrying; heading[neighbor] = dir; grid[ant] = empty; } } }
int test_decoder_code0(int no_fuzz) { static const opus_int32 fsv[5]={48000,24000,16000,12000,8000}; int err,skip,plen; int out_samples,fec; int t; opus_int32 i; OpusDecoder *dec[5*2]; opus_int32 decsize; OpusDecoder *decbak; opus_uint32 dec_final_range1,dec_final_range2,dec_final_acc; unsigned char *packet; unsigned char modes[4096]; short *outbuf_int; short *outbuf; dec_final_range1=dec_final_range2=2; packet=malloc(sizeof(unsigned char)*MAX_PACKET); if(packet==NULL)test_failed(); outbuf_int=malloc(sizeof(short)*(MAX_FRAME_SAMP+16)*2); for(i=0;i<(MAX_FRAME_SAMP+16)*2;i++)outbuf_int[i]=32749; outbuf=&outbuf_int[8*2]; fprintf(stdout," Starting %d decoders...\n",5*2); for(t=0;t<5*2;t++) { int fs=fsv[t>>1]; int c=(t&1)+1; err=OPUS_INTERNAL_ERROR; dec[t] = opus_decoder_create(fs, c, &err); if(err!=OPUS_OK || dec[t]==NULL)test_failed(); fprintf(stdout," opus_decoder_create(%5d,%d) OK. Copy ",fs,c); { OpusDecoder *dec2; /*The opus state structures contain no pointers and can be freely copied*/ dec2=(OpusDecoder *)malloc(opus_decoder_get_size(c)); if(dec2==NULL)test_failed(); memcpy(dec2,dec[t],opus_decoder_get_size(c)); memset(dec[t],255,opus_decoder_get_size(c)); opus_decoder_destroy(dec[t]); printf("OK.\n"); dec[t]=dec2; } } decsize=opus_decoder_get_size(1); decbak=(OpusDecoder *)malloc(decsize); if(decbak==NULL)test_failed(); for(t=0;t<5*2;t++) { int factor=48000/fsv[t>>1]; for(fec=0;fec<2;fec++) { opus_int32 dur; /*Test PLC on a fresh decoder*/ out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor, fec); if(out_samples!=120/factor)test_failed(); if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); if(dur!=120/factor)test_failed(); /*Test on a size which isn't a multiple of 2.5ms*/ out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor+2, fec); if(out_samples!=OPUS_BAD_ARG)test_failed(); /*Test null pointer input*/ out_samples = opus_decode(dec[t], 0, -1, outbuf, 120/factor, fec); if(out_samples!=120/factor)test_failed(); out_samples = opus_decode(dec[t], 0, 1, outbuf, 120/factor, fec); if(out_samples!=120/factor)test_failed(); out_samples = opus_decode(dec[t], 0, 10, outbuf, 120/factor, fec); if(out_samples!=120/factor)test_failed(); out_samples = opus_decode(dec[t], 0, fast_rand(), outbuf, 120/factor, fec); if(out_samples!=120/factor)test_failed(); if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); if(dur!=120/factor)test_failed(); /*Zero lengths*/ out_samples = opus_decode(dec[t], packet, 0, outbuf, 120/factor, fec); if(out_samples!=120/factor)test_failed(); /*Zero buffer*/ outbuf[0]=32749; out_samples = opus_decode(dec[t], packet, 0, outbuf, 0, fec); if(out_samples>0)test_failed(); #if !defined(OPUS_BUILD) && (OPUS_GNUC_PREREQ(4, 6) || (defined(__clang_major__) && __clang_major__ >= 3)) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wnonnull" #endif out_samples = opus_decode(dec[t], packet, 0, 0, 0, fec); #if !defined(OPUS_BUILD) && (OPUS_GNUC_PREREQ(4, 6) || (defined(__clang_major__) && __clang_major__ >= 3)) #pragma GCC diagnostic pop #endif if(out_samples>0)test_failed(); if(outbuf[0]!=32749)test_failed(); /*Invalid lengths*/ out_samples = opus_decode(dec[t], packet, -1, outbuf, MAX_FRAME_SAMP, fec); if(out_samples>=0)test_failed(); out_samples = opus_decode(dec[t], packet, INT_MIN, outbuf, MAX_FRAME_SAMP, fec); if(out_samples>=0)test_failed(); out_samples = opus_decode(dec[t], packet, -1, outbuf, -1, fec); if(out_samples>=0)test_failed(); /*Crazy FEC values*/ out_samples = opus_decode(dec[t], packet, 1, outbuf, MAX_FRAME_SAMP, fec?-1:2); if(out_samples>=0)test_failed(); /*Reset the decoder*/ if(opus_decoder_ctl(dec[t], OPUS_RESET_STATE)!=OPUS_OK)test_failed(); } } fprintf(stdout," dec[all] initial frame PLC OK.\n"); /*Count code 0 tests*/ for(i=0;i<64;i++) { opus_int32 dur; int j,expected[5*2]; packet[0]=i<<2; packet[1]=255; packet[2]=255; err=opus_packet_get_nb_channels(packet); if(err!=(i&1)+1)test_failed(); for(t=0;t<5*2;t++){ expected[t]=opus_decoder_get_nb_samples(dec[t],packet,1); if(expected[t]>2880)test_failed(); } for(j=0;j<256;j++) { packet[1]=j; for(t=0;t<5*2;t++) { out_samples = opus_decode(dec[t], packet, 3, outbuf, MAX_FRAME_SAMP, 0); if(out_samples!=expected[t])test_failed(); if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); if(dur!=out_samples)test_failed(); opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1)); if(t==0)dec_final_range2=dec_final_range1; else if(dec_final_range1!=dec_final_range2)test_failed(); } } for(t=0;t<5*2;t++){ int factor=48000/fsv[t>>1]; /* The PLC is run for 6 frames in order to get better PLC coverage. */ for(j=0;j<6;j++) { out_samples = opus_decode(dec[t], 0, 0, outbuf, expected[t], 0); if(out_samples!=expected[t])test_failed(); if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); if(dur!=out_samples)test_failed(); } /* Run the PLC once at 2.5ms, as a simulation of someone trying to do small drift corrections. */ if(expected[t]!=120/factor) { out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor, 0); if(out_samples!=120/factor)test_failed(); if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); if(dur!=out_samples)test_failed(); } out_samples = opus_decode(dec[t], packet, 2, outbuf, expected[t]-1, 0); if(out_samples>0)test_failed(); } } fprintf(stdout," dec[all] all 2-byte prefix for length 3 and PLC, all modes (64) OK.\n"); if(no_fuzz) { fprintf(stdout," Skipping many tests which fuzz the decoder as requested.\n"); free(decbak); for(t=0;t<5*2;t++)opus_decoder_destroy(dec[t]); printf(" Decoders stopped.\n"); err=0; for(i=0;i<8*2;i++)err|=outbuf_int[i]!=32749; for(i=MAX_FRAME_SAMP*2;i<(MAX_FRAME_SAMP+8)*2;i++)err|=outbuf[i]!=32749; if(err)test_failed(); free(outbuf_int); free(packet); return 0; } { /*We only test a subset of the modes here simply because the longer durations end up taking a long time.*/ static const int cmodes[4]={16,20,24,28}; static const opus_uint32 cres[4]={116290185,2172123586u,2172123586u,2172123586u}; static const opus_uint32 lres[3]={3285687739u,1481572662,694350475}; static const int lmodes[3]={0,4,8}; int mode=fast_rand()%4; packet[0]=cmodes[mode]<<3; dec_final_acc=0; t=fast_rand()%10; for(i=0;i<65536;i++) { int factor=48000/fsv[t>>1]; packet[1]=i>>8; packet[2]=i&255; packet[3]=255; out_samples = opus_decode(dec[t], packet, 4, outbuf, MAX_FRAME_SAMP, 0); if(out_samples!=120/factor)test_failed(); opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1)); dec_final_acc+=dec_final_range1; } if(dec_final_acc!=cres[mode])test_failed(); fprintf(stdout," dec[%3d] all 3-byte prefix for length 4, mode %2d OK.\n",t,cmodes[mode]); mode=fast_rand()%3; packet[0]=lmodes[mode]<<3; dec_final_acc=0; t=fast_rand()%10; for(i=0;i<65536;i++) { int factor=48000/fsv[t>>1]; packet[1]=i>>8; packet[2]=i&255; packet[3]=255; out_samples = opus_decode(dec[t], packet, 4, outbuf, MAX_FRAME_SAMP, 0); if(out_samples!=480/factor)test_failed(); opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1)); dec_final_acc+=dec_final_range1; } if(dec_final_acc!=lres[mode])test_failed(); fprintf(stdout," dec[%3d] all 3-byte prefix for length 4, mode %2d OK.\n",t,lmodes[mode]); } skip=fast_rand()%7; for(i=0;i<64;i++) { int j,expected[5*2]; packet[0]=i<<2; for(t=0;t<5*2;t++)expected[t]=opus_decoder_get_nb_samples(dec[t],packet,1); for(j=2+skip;j<1275;j+=4) { int jj; for(jj=0;jj<j;jj++)packet[jj+1]=fast_rand()&255; for(t=0;t<5*2;t++) { out_samples = opus_decode(dec[t], packet, j+1, outbuf, MAX_FRAME_SAMP, 0); if(out_samples!=expected[t])test_failed(); opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1)); if(t==0)dec_final_range2=dec_final_range1; else if(dec_final_range1!=dec_final_range2)test_failed(); } } } fprintf(stdout," dec[all] random packets, all modes (64), every 8th size from from %d bytes to maximum OK.\n",2+skip); debruijn2(64,modes); plen=(fast_rand()%18+3)*8+skip+3; for(i=0;i<4096;i++) { int j,expected[5*2]; packet[0]=modes[i]<<2; for(t=0;t<5*2;t++)expected[t]=opus_decoder_get_nb_samples(dec[t],packet,plen); for(j=0;j<plen;j++)packet[j+1]=(fast_rand()|fast_rand())&255; memcpy(decbak,dec[0],decsize); if(opus_decode(decbak, packet, plen+1, outbuf, expected[0], 1)!=expected[0])test_failed(); memcpy(decbak,dec[0],decsize); if(opus_decode(decbak, 0, 0, outbuf, MAX_FRAME_SAMP, 1)<20)test_failed(); memcpy(decbak,dec[0],decsize); if(opus_decode(decbak, 0, 0, outbuf, MAX_FRAME_SAMP, 0)<20)test_failed(); for(t=0;t<5*2;t++) { opus_int32 dur; out_samples = opus_decode(dec[t], packet, plen+1, outbuf, MAX_FRAME_SAMP, 0); if(out_samples!=expected[t])test_failed(); if(t==0)dec_final_range2=dec_final_range1; else if(dec_final_range1!=dec_final_range2)test_failed(); if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed(); if(dur!=out_samples)test_failed(); } } fprintf(stdout," dec[all] random packets, all mode pairs (4096), %d bytes/frame OK.\n",plen+1); plen=(fast_rand()%18+3)*8+skip+3; t=rand()&3; for(i=0;i<4096;i++) { int count,j,expected; packet[0]=modes[i]<<2; expected=opus_decoder_get_nb_samples(dec[t],packet,plen); for(count=0;count<10;count++) { for(j=0;j<plen;j++)packet[j+1]=(fast_rand()|fast_rand())&255; out_samples = opus_decode(dec[t], packet, plen+1, outbuf, MAX_FRAME_SAMP, 0); if(out_samples!=expected)test_failed(); } } fprintf(stdout," dec[%3d] random packets, all mode pairs (4096)*10, %d bytes/frame OK.\n",t,plen+1); { int tmodes[1]={25<<2}; opus_uint32 tseeds[1]={140441}; int tlen[1]={157}; opus_int32 tret[1]={480}; t=fast_rand()&1; for(i=0;i<1;i++) { int j; packet[0]=tmodes[i]; Rw=Rz=tseeds[i]; for(j=1;j<tlen[i];j++)packet[j]=fast_rand()&255; out_samples=opus_decode(dec[t], packet, tlen[i], outbuf, MAX_FRAME_SAMP, 0); if(out_samples!=tret[i])test_failed(); } fprintf(stdout," dec[%3d] pre-selected random packets OK.\n",t); } free(decbak); for(t=0;t<5*2;t++)opus_decoder_destroy(dec[t]); printf(" Decoders stopped.\n"); err=0; for(i=0;i<8*2;i++)err|=outbuf_int[i]!=32749; for(i=MAX_FRAME_SAMP*2;i<(MAX_FRAME_SAMP+8)*2;i++)err|=outbuf[i]!=32749; if(err)test_failed(); free(outbuf_int); free(packet); return 0; }
float Noise::tick() { return (float) ((2.0 * fast_rand() * inv_randmax) - 1.0); }
static void make_ant (int i) { grid[i] = emptyhanded; heading[i] = fast_rand () % 8; }