virtual void pull(AudioChunk &chunk) { if (!chunk.length()) return; if (!valid || finished) {chunk.silence(); return;} int samples, have = 0, need = chunk.length(); //Create pointers to 16-bit data short *d16[PG_MAX_CHANNELS]; for (Uint32 i = 0; i < chunk.format().channels; ++i) d16[i] = (short*) chunk.start(i); while (true) { samples = stb_vorbis_get_samples_short(ogg, chunk.format().channels, d16, (need-have)); if (samples < 0) { finished = true; //cout << " VORBIS ERROR" << endl; break; } if (samples == 0) { //File's end if (loop) { stb_vorbis_seek_start(ogg); continue; } else { finished = true; break; } } for (Uint32 i=0; i < chunk.format().channels; ++i) d16[i] += samples; have += samples; //if (have > need) cout << "VORBIS OVERDRAW" << endl; //std::cout << "OGG pull: " << have << "/" << need << std::endl; if (have >= need) break; } //Cutoff marker if necessary if (have < need) chunk.cutoff(have); //Upsample data to 24-bit Sint32s for (Uint32 i=0; i < chunk.format().channels; ++i) { Sint32 *start = chunk.start(i), *op = start + have; short *ip = d16[i]; while (op!=start) {*(--op) = 256 * Sint32(*(--ip));} } }
void Splicer::pull(AudioChunk &chunk) { Uint32 left = chunk.length(), chans = chunk.format().channels; Sint32 *data[PG_MAX_CHANNELS]; for (Uint32 i = 0; i < chans; ++i) data[i] = chunk.start(i); //Query exhausted each loop to refresh the value of "current". while (!exhausted()) { //Pull data from next stream AudioChunk sub(chunk.scratch(), output, data, left, chunk.sync); current->pull(sub); //Partial advance if (current->exhausted()) { Uint32 cut = sub.cutoff(); for (Uint32 i = 0; i < chans; ++i) data[i] += cut; left -= cut; current = NULL; if (left) continue; } return; } //The Splicer is exhausted! chunk.cutoff(data[0] - chunk.start(0)); }
void Bandpass::pull(AudioChunk &chunk, const Bandpass_Node &a, const Bandpass_Node &b) { //Pull source data source.pull(chunk); //Calculate RC multipliers float al = RCCONV / ((a.low<=0.0f)?40000.0f:a.low), ah = RCCONV / ((a.high<=0.0f)?10.0f:a.high), bl = RCCONV / ((b.low<=0.0f)?40000.0f:b.low), bh = RCCONV / ((b.high<=0.0f)?10.0f:b.high); float lpRC = al, hpRC = ah, lpM = pow(bl/al, 1.0f / float(chunk.length())), hpM = pow(bh/ah, 1.0f / float(chunk.length())), lpA, hpA, samp, dt = 1.0f / float(chunk.format().rate); //Apply effect! Uint32 chan = source.format().channels; for (Uint32 i = 0; i < chan; ++i) { Sint32 *pos = chunk.start(i), *end = chunk.end(i); float &lpPc = lpP[i], &hpDc = hpD[i]; while (pos < end) { //Interpolate settings lpA = dt / (lpRC + dt); lpRC *= lpM; hpA = hpRC / (hpRC + dt); hpRC *= hpM; //Get samples samp = float(*pos); //Lowpass samp = lpPc + lpA * (samp-lpPc); lpPc = samp; //Highpass (confusing but correct) samp = hpA * (samp+hpDc); hpDc = samp - lpPc; //Set samples *pos = Sint32(samp); ++pos; } } }
virtual void pull(AudioChunk &chunk) { //This is possible at startup; race conditions are bad. if (!back) chunk.silence(); //Shorthand. Uint32 frame = back->mikeFrame, length = back->mikeLength; const Sint32 *data = back->mikeData; //How much information is available? if (readFrame != frame) {readFrame = frame; prog = 0;} Uint32 want = chunk.length(), get = std::min(length-prog, want); //Read what we can from the buffer std::memcpy((void*)chunk.start(0), (const void*)(data+prog), 4*get); prog += get; //Fill your cup too full and it will spill... if (get < want) std::memset((void*)(chunk.start(0)+get), 0, 4*(want-get)); }
void Oscillator::pull(AudioChunk &chunk, const State &a, const State &b) { //Phase goes from -1 to 1. Wierd? Maybe. while (phase >= 1.0f) phase -= 2.0f; while (phase < -1.0f) phase += 2.0f; Sint32 samp; float v, x, sq; float step = (b.freq / output.rate) * 2.0f, amp = b.amp * 16777215.0f, ph = phase; Sint32 *i = chunk.start(0), *e = chunk.end(0); switch (type) { case SINE: while (i != e) { //Simple sine wave samp = amp*sin(PI * ph); ph += step; *i = samp; ++i; ph -= 2.0f*int(ph*.5f); //Restrict to [-1, 1] } break; case SQUARE: while (i != e) { samp = amp * ((ph>=0.0f) ? 1.0f : -1.0f); ph += step; *i = samp; ++i; ph -= 2.0f*int(ph*.5f); //Restrict to [-1, 1] } break; case TRIANGLE: while (i != e) { samp = amp * (2.0f*std::abs(ph) - 1.0f); ph += step; *i = samp; ++i; ph -= 2.0f*int(ph*.5f); //Restrict to [-1, 1] } break; case SAWTOOTH: while (i != e) { samp = amp * ph; ph += step; *i = samp; ++i; ph -= 2.0f*int(ph*.5f); //Restrict to [-1, 1] } break; case KLAXON: default: { const float PISQ = PI*PI; while (i != e) { //Sine wave approximation gone wrong sq = PISQ*ph*ph; x = PI*ph*amp; v = x * 1.02394347; //Makes the endpoints line up x *= sq; v -= x/6.0f; x *= sq; v += x/120.0f; x *= sq; v -= x/5040.0f; samp = x; ph += step; *i = samp; ++i; ph -= 2.0f*int(ph*.5f); //Restrict to [-1, 1] } } break; } phase = ph; //Copy signal into all other channels for (Uint32 c = chunk.channels()-1; c > 0; --c) { std::memcpy(chunk.start(c), chunk.start(0), 4*chunk.length()); } }