Float StyleScore(int consider_style, int expected_style) { /* todoSCORE */ return(0.5+ (0.5* (1.0-((IntAbs(StyleToInt(consider_style) - StyleToInt(expected_style)))/3.0)))); }
void RatStd(Rational &num) { Integer tmp; if (IntSmaller(num.denom, IntZero)) { //num.numer.nonnegative = !num.numer.nonnegative; //num.denom.nonnegative = !num.denom.nonnegative; num.numer = IntMult(num.numer, IntMinusOne); num.denom = IntMult(num.denom, IntMinusOne); } tmp = IntGcd(IntAbs(num.numer), IntAbs(num.denom)); if (!IntEqual(tmp, IntOne)) { num.numer = IntDiv(num.numer, tmp); num.denom = IntDiv(num.denom, tmp); } //return num; }
Integer IntPlus(const Integer &a, const Integer &b) { Integer exv; if (a.nonnegative && b.nonnegative) { exv = _IntPlus(a, b); } // a>=0 && b>=0 if (!a.nonnegative && !b.nonnegative) { exv = _IntPlus(IntAbs(a), IntAbs(b)); exv.nonnegative = false; // a < 0 && b < 0 } if (!a.nonnegative && b.nonnegative) { if (IntBigger(IntAbs(a), b)) { exv = _IntMinus(IntAbs(a), b); exv.nonnegative = false; } else { exv = _IntMinus(b, IntAbs(a)); exv.nonnegative = true; } } if (a.nonnegative && !b.nonnegative) { if (!IntSmaller(a, IntAbs(b))) { exv = _IntMinus(a, IntAbs(b)); exv.nonnegative = true; } else { exv = _IntMinus(IntAbs(b), a); exv.nonnegative = false; } } return exv; }
static void Mix(short *pFinalOut, unsigned Frames) { int MasterVol; mem_zero(m_pMixBuffer, m_MaxFrames*2*sizeof(int)); Frames = min(Frames, m_MaxFrames); // aquire lock while we are mixing lock_wait(m_SoundLock); MasterVol = m_SoundVolume; for(unsigned i = 0; i < NUM_VOICES; i++) { if(m_aVoices[i].m_pSample) { // mix voice CVoice *v = &m_aVoices[i]; int *pOut = m_pMixBuffer; int Step = v->m_pSample->m_Channels; // setup input sources short *pInL = &v->m_pSample->m_pData[v->m_Tick*Step]; short *pInR = &v->m_pSample->m_pData[v->m_Tick*Step+1]; unsigned End = v->m_pSample->m_NumFrames-v->m_Tick; int Rvol = (int)(v->m_pChannel->m_Vol*(v->m_Vol/255.0f)); int Lvol = (int)(v->m_pChannel->m_Vol*(v->m_Vol/255.0f)); // make sure that we don't go outside the sound data if(Frames < End) End = Frames; // check if we have a mono sound if(v->m_pSample->m_Channels == 1) pInR = pInL; // volume calculation if(v->m_Flags&ISound::FLAG_POS && v->m_pChannel->m_Pan) { // TODO: we should respect the channel panning value int dx = v->m_X - m_CenterX; int dy = v->m_Y - m_CenterY; // int p = IntAbs(dx); float FalloffX = 0.0f; float FalloffY = 0.0f; int RangeX = 0; // for panning bool InVoiceField = false; switch(v->m_Shape) { case ISound::SHAPE_CIRCLE: { float r = v->m_Circle.m_Radius; RangeX = r; int Dist = (int)sqrtf((float)dx*dx+dy*dy); // nasty float if(Dist < r) { InVoiceField = true; // falloff int FalloffDistance = r*v->m_Falloff; if(Dist > FalloffDistance) FalloffX = FalloffY = (r-Dist)/(r-FalloffDistance); else FalloffX = FalloffY = 1.0f; } else InVoiceField = false; break; } case ISound::SHAPE_RECTANGLE: { RangeX = v->m_Rectangle.m_Width/2.0f; int abs_dx = abs(dx); int abs_dy = abs(dy); int w = v->m_Rectangle.m_Width/2.0f; int h = v->m_Rectangle.m_Height/2.0f; if(abs_dx < w && abs_dy < h) { InVoiceField = true; // falloff int fx = v->m_Falloff * w; int fy = v->m_Falloff * h; FalloffX = abs_dx > fx ? (float)(w-abs_dx)/(w-fx) : 1.0f; FalloffY = abs_dy > fy ? (float)(h-abs_dy)/(h-fy) : 1.0f; } else InVoiceField = false; break; } }; if(InVoiceField) { // panning if(!(v->m_Flags&ISound::FLAG_NO_PANNING)) { if(dx > 0) Lvol = ((RangeX-p)*Lvol)/RangeX; else Rvol = ((RangeX-p)*Rvol)/RangeX; } { Lvol *= FalloffX; Rvol *= FalloffY; } } else { Lvol = 0; Rvol = 0; } } // process all frames for(unsigned s = 0; s < End; s++) { *pOut++ += (*pInL)*Lvol; *pOut++ += (*pInR)*Rvol; pInL += Step; pInR += Step; v->m_Tick++; } // free voice if not used any more if(v->m_Tick == v->m_pSample->m_NumFrames) { if(v->m_Flags&ISound::FLAG_LOOP) v->m_Tick = 0; else { v->m_pSample = 0; v->m_Age++; } } } } // release the lock lock_unlock(m_SoundLock); { // clamp accumulated values // TODO: this seams slow for(unsigned i = 0; i < Frames; i++) { int j = i<<1; int vl = ((m_pMixBuffer[j]*MasterVol)/101)>>8; int vr = ((m_pMixBuffer[j+1]*MasterVol)/101)>>8; pFinalOut[j] = Int2Short(vl); pFinalOut[j+1] = Int2Short(vr); } } #if defined(CONF_ARCH_ENDIAN_BIG) swap_endian(pFinalOut, sizeof(short), Frames * 2); #endif }
static void Mix(short *pFinalOut, unsigned Frames) { int MasterVol; mem_zero(m_pMixBuffer, m_MaxFrames*2*sizeof(int)); Frames = min(Frames, m_MaxFrames); // aquire lock while we are mixing lock_wait(m_SoundLock); MasterVol = m_SoundVolume; for(unsigned i = 0; i < NUM_VOICES; i++) { if(m_aVoices[i].m_pSample) { // mix voice CVoice *v = &m_aVoices[i]; int *pOut = m_pMixBuffer; int Step = v->m_pSample->m_Channels; // setup input sources short *pInL = &v->m_pSample->m_pData[v->m_Tick*Step]; short *pInR = &v->m_pSample->m_pData[v->m_Tick*Step+1]; unsigned End = v->m_pSample->m_NumFrames-v->m_Tick; int Rvol = v->m_pChannel->m_Vol; int Lvol = v->m_pChannel->m_Vol; // make sure that we don't go outside the sound data if(Frames < End) End = Frames; // check if we have a mono sound if(v->m_pSample->m_Channels == 1) pInR = pInL; // volume calculation if(v->m_Flags&ISound::FLAG_POS && v->m_pChannel->m_Pan) { // TODO: we should respect the channel panning value const int Range = 1500; // magic value, remove int dx = v->m_X - m_CenterX; int dy = v->m_Y - m_CenterY; int Dist = (int)sqrtf((float)dx*dx+dy*dy); // float here. nasty int p = IntAbs(dx); if(Dist >= 0 && Dist < Range) { // panning if(dx > 0) Lvol = ((Range-p)*Lvol)/Range; else Rvol = ((Range-p)*Rvol)/Range; // falloff Lvol = (Lvol*(Range-Dist))/Range; Rvol = (Rvol*(Range-Dist))/Range; } else { Lvol = 0; Rvol = 0; } } // process all frames for(unsigned s = 0; s < End; s++) { *pOut++ += (*pInL)*Lvol; *pOut++ += (*pInR)*Rvol; pInL += Step; pInR += Step; v->m_Tick++; } // free voice if not used any more if(v->m_Tick == v->m_pSample->m_NumFrames) { if(v->m_Flags&ISound::FLAG_LOOP) v->m_Tick = 0; else v->m_pSample = 0; } } } // release the lock lock_unlock(m_SoundLock); { // clamp accumulated values // TODO: this seams slow for(unsigned i = 0; i < Frames; i++) { int j = i<<1; int vl = ((m_pMixBuffer[j]*MasterVol)/101)>>8; int vr = ((m_pMixBuffer[j+1]*MasterVol)/101)>>8; pFinalOut[j] = Int2Short(vl); pFinalOut[j+1] = Int2Short(vr); } } #if defined(CONF_ARCH_ENDIAN_BIG) swap_endian(pFinalOut, sizeof(short), Frames * 2); #endif }
int brrencode(short *input_data, unsigned char *output_data, long inputframes, bool isLoop, long loop_point, int pad_frames) { int frm; short *input; unsigned char *output; int out_1, out_2; int frames_remain,frame_offset,outbytes=0; int *filter, *range, half; int numblocks; int current_block; int loopstart_block = (pad_frames+loop_point)/16; int loopstart_sample; int blockout[16]; int blockdec[16]; bool use_filter0; bool redo_loopf; int adv_frame; //frame_offset = (16-(inputframes % 16))%16; frame_offset = pad_frames; numblocks = (inputframes+frame_offset) / 16; filter = new int[numblocks]; range = new int[numblocks]; use_filter0 = false; redo_loopf = true; frames_remain = inputframes; out_1 = out_2 = 0; input = input_data; output = output_data; current_block = 0; while ( redo_loopf ) { redo_loopf=false; //int err_sum = 0; while (frames_remain > 0) { // printf("current_block=%d\n",current_block); int err; int filter_fix = -1; int range_fix = -1; if ( current_block == 0 || use_filter0 ) { filter_fix = 0; } adv_frame = encodeBlock( input, frame_offset, &out_1, &out_2, filter_fix, range_fix, true, &filter[current_block], &range[current_block], blockout, blockdec, &err ); use_filter0 = false; if ( current_block == loopstart_block ) { loopstart_sample = blockdec[0]; } // err_sum += err; input += adv_frame; frames_remain -= adv_frame; frame_offset -= 16-adv_frame; //Headerバイトの設定 // printf("filter=%d\n",filter[current_block]); *output = range[current_block]<<4; *output |= filter[current_block]<<2; if (frames_remain <= 0) { *output |= 1; //ENDbitの付加 } if (isLoop) { *output |= 2; //ループフラグの付加 } output++; outbytes++; //データバイトの書き込み half = 0; for (frm=0; frm<16; frm++) { if (half == 0) { *output = (blockout[frm] << 4) & 0xf0; half = 1; } else { *output |= blockout[frm] & 0xf; half = 0; output++; outbytes++; } } current_block++; } if ( isLoop ) { int lc_range; int lc_filter; int lc_value; int loop_w = loopstart_block*9; lc_filter = (output_data[loop_w] & 0x0c) >> 2; if ( lc_filter != 0 ) { lc_range = (output_data[loop_w] & 0xf0) >> 4; lc_value = output_data[loop_w+1] >> 4; lc_value <<= lc_range; // printf("lc_value=%d\n",lc_value); input -= adv_frame; lc_value += ComputeFilter( input[13], input[12], lc_filter ); // printf("out_2=%d\n",out_2); // printf("out_1=%d\n",out_1); // printf("lc_value=%d\n",lc_value); // printf("loopstart_sample=%d\n",loopstart_sample); if( IntAbs( lc_value - loopstart_sample ) > looploss_tolerance ) { // ループ地点から再エンコード frame_offset = pad_frames; if ( 16*loopstart_block < frame_offset ) { frame_offset -= 16*loopstart_block; frames_remain = inputframes; input = input_data; } else { frames_remain = inputframes+frame_offset - 16*loopstart_block; input = input_data + (16*loopstart_block - frame_offset); frame_offset = 0; } output = output_data + loopstart_block*9; current_block = loopstart_block; outbytes = loopstart_block*9; redo_loopf = true; use_filter0 = true; } } } }