Ejemplo n.º 1
0
void
padsynth_oscillator(unsigned long sample_count, y_sosc_t *sosc,
                    y_voice_t *voice, struct vosc *vosc, int index, float w0)
{
    unsigned long sample;
    float w, w_delta,
          level_a, level_a_delta,
          level_b, level_b_delta;
    float f;
    int   i, ready;

    i = y_voice_mod_index(sosc->pitch_mod_src);
    f = *(sosc->pitch_mod_amt);
    w = 1.0f + f * voice->mod[i].value;
    w_delta = w + f * voice->mod[i].delta * (float)sample_count;
    w_delta *= w0;
    w       *= w0;
    w_delta = (w_delta - w) / (float)sample_count;
    /* -FIX- condition to [0, 0.5)? */

    i = y_voice_mod_index(sosc->amp_mod_src);
    f = *(sosc->amp_mod_amt);
    if (f > 0.0f)
        level_a = 1.0f - f + f * voice->mod[i].value;
    else
        level_a = 1.0f + f * voice->mod[i].value;
    level_a_delta = volume_cv_to_amplitude(level_a + f * voice->mod[i].delta * (float)sample_count);
    level_a       = volume_cv_to_amplitude(level_a);
    level_a       /= 32767.0f;
    level_a_delta /= 32767.0f;
    level_b       = level_a       * *(sosc->level_b);
    level_b_delta = level_a_delta * *(sosc->level_b);
    level_a       *= *(sosc->level_a);
    level_a_delta *= *(sosc->level_a);
    level_a_delta = (level_a_delta - level_a) / (float)sample_count;
    level_b_delta = (level_b_delta - level_b) / (float)sample_count;
    /* -FIX- condition to [0, 1]? */

    if (sosc->sampleset && sosc->sampleset->set_up) {

        i = voice->key + lrintf(*(sosc->pitch));
        if (vosc->mode     != vosc->last_mode ||
            vosc->waveform != vosc->last_waveform ||
            i              != vosc->wave_select_key) {

            /* get sample indices and crossfade from sampleset */
            sample_select(sosc, vosc, i);
            vosc->last_waveform = vosc->waveform;
            /* try to avoid reseting pos0 except when necessary to avoid causing clocks in mono
             * mode with portamento -- but basically it's going to click if it's not the same
             * sample.... */
            if (vosc->mode != vosc->last_mode) {
                vosc->last_mode = vosc->mode;
                if (sosc->sampleset->sample[vosc->i0])
                    vosc->pos0 = vosc->pos1 = (double)random_float(0.0f,
                                                  (float)sosc->sampleset->sample[vosc->i0]->length);
                else
                    vosc->pos0 = vosc->pos1 = 0.0;
            }
        }
        if (sosc->sampleset->sample[vosc->i0] &&
            sosc->sampleset->sample[vosc->i1])
            ready = 1;
        else
            ready = 0;
    } else {
        if (vosc->mode != vosc->last_mode) {
            vosc->last_mode = vosc->mode;
            vosc->pos0 = vosc->pos1 = 0.0;
        }
        vosc->last_waveform = -1;
        ready = 0;
    }

    if (!ready) {
        /* sampleset not ready, render a sine instead */
        float pos = (float)vosc->pos0;

        if (pos > 1.0f) pos = 0.0f;
        level_a       *= 16383.5f; /* scale for sine_wave (float) instead of sample->data (16-bit fixed) */
        level_a_delta *= 16383.5f;
        level_b       *= 16383.5f;
        level_b_delta *= 16383.5f;

        for (sample = 0; sample < sample_count; sample++) {

            pos += w;
            if (pos >= 1.0f) pos -= 1.0f;

            f = pos * (float)SINETABLE_POINTS;
            i = lrintf(f - 0.5f);
            f -= (float)i;
            f = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * f;
            voice->osc_bus_a[index]   += level_a * f;
            voice->osc_bus_b[index++] += level_b * f;

            w       += w_delta;
            level_a += level_a_delta;
            level_b += level_b_delta;
        }

        vosc->pos0 = (double)pos;
        return;
    }

    if (sosc->sampleset->param3 & 1) {  /* mono */
      if (vosc->i0 == vosc->i1) { /* no crossfade */

        /* mono without crossfade */
        y_sample_t *s = (y_sample_t *)sosc->sampleset->sample[vosc->i0];
        signed short *data = s->data;
        double pos = vosc->pos0;
        double length = (double)s->length;
        float period = s->period;

        if (pos >= length) pos = 0.0;

        for (sample = 0; sample < sample_count; sample++) {

            i = lrint(pos - 0.5);
            f = (float)(pos - (double)i);
            f = bspline_interp(f, (float)data[i - 1], (float)data[i],
                                  (float)data[i + 1], (float)data[i + 2]);
            voice->osc_bus_a[index]   += level_a * f;
            voice->osc_bus_b[index++] += level_b * f;

            w       += w_delta;
            level_a += level_a_delta;
            level_b += level_b_delta;

            pos += w * period;
            if (pos >= length) pos -= length;
            /* sampleset oscillators do not export sync */
        }

        vosc->pos0 = pos;

      } else {

        /* mono with crossfade */
        y_sample_t *s0 = (y_sample_t *)sosc->sampleset->sample[vosc->i0],
                   *s1 = (y_sample_t *)sosc->sampleset->sample[vosc->i1];
        signed short *data0 = s0->data,
                     *data1 = s1->data;
        double pos0 = vosc->pos0,
               pos1 = vosc->pos1;
        double length0 = (double)s0->length,
               length1 = (double)s1->length;
        float period0 = s0->period,
              period1 = s1->period;
        float a,
              wavemix0 = vosc->wavemix0,
              wavemix1 = vosc->wavemix1;

        if (pos0 >= length0) pos0 = 0.0;
        if (pos1 >= length1) pos1 = 0.0;

        for (sample = 0; sample < sample_count; sample++) {

            i = lrint(pos0 - 0.5);
            f = (float)(pos0 - (double)i);
            a = bspline_interp(f, (float)data0[i - 1], (float)data0[i],
                                  (float)data0[i + 1], (float)data0[i + 2]) * wavemix0;

            i = lrint(pos1 - 0.5);
            f = (float)(pos1 - (double)i);
            a += bspline_interp(f, (float)data1[i - 1], (float)data1[i],
                                  (float)data1[i + 1], (float)data1[i + 2]) * wavemix1;

            voice->osc_bus_a[index]   += level_a * a;
            voice->osc_bus_b[index++] += level_b * a;

            w       += w_delta;
            level_a += level_a_delta;
            level_b += level_b_delta;

            pos0 += w * period0;
            pos1 += w * period1;
            if (pos0 >= length0) pos0 -= length0;
            if (pos1 >= length1) pos1 -= length1;
            /* sampleset oscillators do not export sync */
        }

        vosc->pos0 = pos0;
        vosc->pos1 = pos1;
      }

    } else {

      if (vosc->i0 == vosc->i1) {

        /* stereo without crossfade */
        y_sample_t *s = (y_sample_t *)sosc->sampleset->sample[vosc->i0];
        signed short *data = s->data;
        double posl = vosc->pos0,
               posr,
               length = (double)s->length;
        float period = s->period;

        if (posl >= length) posl = 0.0;
        /* delay the right channel by about one-half the table length, but make
         * it an multiple of the period length to minimize phase cancellation
         * when summed to mono */
        posr = posl + rint(length / 2.0 / (double)period) * (double)period;
        if (posr >= length) posr -= length;

        for (sample = 0; sample < sample_count; sample++) {

            i = lrint(posl - 0.5);
            f = (float)(posl - (double)i);
            f = bspline_interp(f, (float)data[i - 1], (float)data[i],
                                  (float)data[i + 1], (float)data[i + 2]);
            voice->osc_bus_a[index]   += level_a * f;

            i = lrint(posr - 0.5);
            f = (float)(posr - (double)i);
            f = bspline_interp(f, (float)data[i - 1], (float)data[i],
                                  (float)data[i + 1], (float)data[i + 2]);
            voice->osc_bus_b[index++] += level_b * f;

            w       += w_delta;
            level_a += level_a_delta;
            level_b += level_b_delta;

            posl += w * period;
            if (posl >= length) posl -= length;
            posr += w * period;
            if (posr >= length) posr -= length;
            /* sampleset oscillators do not export sync */
        }

        vosc->pos0 = posl;

      } else {

        /* stereo with crossfade */
        y_sample_t *s0 = (y_sample_t *)sosc->sampleset->sample[vosc->i0],
                   *s1 = (y_sample_t *)sosc->sampleset->sample[vosc->i1];
        signed short *data0 = s0->data,
                     *data1 = s1->data;
        double posl0 = vosc->pos0,
               posl1 = vosc->pos1,
               posr0, posr1;
        double length0 = (double)s0->length,
               length1 = (double)s1->length;
        float period0 = s0->period,
              period1 = s1->period;
        float a,
              wavemix0 = vosc->wavemix0,
              wavemix1 = vosc->wavemix1;

        if (posl0 >= length0) posl0 = 0.0;
        if (posl1 >= length1) posl1 = 0.0;
        posr0 = posl0 + rint(length0 / 2.0 / (double)period0) * (double)period0;
        posr1 = posl1 + rint(length1 / 2.0 / (double)period1) * (double)period1;
        if (posr0 >= length0) posr0 -= length0;
        if (posr1 >= length1) posr1 -= length1;

        for (sample = 0; sample < sample_count; sample++) {

            i = lrint(posl0 - 0.5);
            f = (float)(posl0 - (double)i);
            a = bspline_interp(f, (float)data0[i - 1], (float)data0[i],
                                  (float)data0[i + 1], (float)data0[i + 2]) * wavemix0;
            i = lrint(posl1 - 0.5);
            f = (float)(posl1 - (double)i);
            a += bspline_interp(f, (float)data1[i - 1], (float)data1[i],
                                  (float)data1[i + 1], (float)data1[i + 2]) * wavemix1;
            voice->osc_bus_a[index]   += level_a * a;

            i = lrint(posr0 - 0.5);
            f = (float)(posr0 - (double)i);
            a = bspline_interp(f, (float)data0[i - 1], (float)data0[i],
                                  (float)data0[i + 1], (float)data0[i + 2]) * wavemix0;
            i = lrint(posr1 - 0.5);
            f = (float)(posr1 - (double)i);
            a += bspline_interp(f, (float)data1[i - 1], (float)data1[i],
                                  (float)data1[i + 1], (float)data1[i + 2]) * wavemix1;
            voice->osc_bus_b[index++] += level_b * a;

            w       += w_delta;
            level_a += level_a_delta;
            level_b += level_b_delta;

            posl0 += w * period0;
            posr0 += w * period0;
            posl1 += w * period1;
            posr1 += w * period1;
            if (posl0 >= length0) posl0 -= length0;
            if (posr0 >= length0) posr0 -= length0;
            if (posl1 >= length1) posl1 -= length1;
            if (posr1 >= length1) posr1 -= length1;
            /* sampleset oscillators do not export sync */
        }

        vosc->pos0 = posl0;
        vosc->pos1 = posl1;
      }
    }
}
Ejemplo n.º 2
0
	random_generator(int count) : _count(count), _values(new float [count])
	{
		for (int i = 0; i < count; ++i)
			_values[i] = random_float();
	}
Ejemplo n.º 3
0
GDoubleTerminal::GDoubleTerminal( const GDoubleTerminalDef& termdef ) :
    GTerminal( termdef )
{
    _value = termdef._minimum + (termdef._maximum - termdef._minimum) * random_float(1.0);
}
Ejemplo n.º 4
0
int main() {
    srand(time(0));

    for (int i = 0; i < 3; i++)
        gColor[i] = random_float();

    signal(SIGINT, signal_handler);

    SDL_Init(SDL_INIT_VIDEO);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    /* Create our window centered at 512x512 resolution */
    gWindow = SDL_CreateWindow("Hello Triangle",
                               SDL_WINDOWPOS_CENTERED,
                               SDL_WINDOWPOS_CENTERED,
                               800,
                               600,
                               SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    SDL_GL_CreateContext(gWindow);
    SDL_GL_SetSwapInterval(1);

    /* create a black box context */
    if (!bbgl_init(&bbgl)) {
        fprintf(stderr, "[bbgl] (client) failed to connect to black box\n");
        return -1;
    }

    glViewport(0, 0, 800, 600);
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

    //const char *vendor = (const char *)glGetString(GL_VENDOR);
    //const char *renderer = (const char *)glGetString(GL_RENDERER);
    //const char *version = (const char *)glGetString(GL_VERSION);
    //const char *shading = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);

    //printf("vendor:     %s\n", vendor);
    //printf("renderer:   %s\n", renderer);
    //printf("version:    %s\n", version);
    //printf("shading:    %s\n", shading);

    create_vertex_array();
    create_vertex_buffer();
    compile_shaders();

    int frames = 0;
    Uint32 start = SDL_GetTicks();
    int dirs[3] = { +1, +1, +1 };
    while (gRunning) {
        ++frames;
        Uint32 elapsed = SDL_GetTicks() - start;
        if (elapsed) {
            double seconds = elapsed / 1000.0;
            double fps = frames / seconds;
            printf("\r%g FPS\n", fps);
        }
        SDL_Event e;
        while (SDL_PollEvent(&e)) {
            switch (e.type) {
            case SDL_QUIT:
                gRunning = 0;
                break;
            }
        }
        render();
        for (int i = 0; i < 3; i++) {
            int value = ((int)(gColor[i] * 255.0f)+dirs[i]);
            if (value > 255 || value <= 0)
                dirs[i] = -dirs[i];
            gColor[i] = value/255.0f;
        }
    }

    bbgl_destroy(&bbgl);
    return 0;
}
Ejemplo n.º 5
0
void
piglit_init(int argc, char **argv)
{
	unsigned r;
	unsigned c;
	unsigned i;


	(void) argc;
	(void) argv;

	piglit_require_vertex_program();
	piglit_require_fragment_program();
	piglit_require_extension("GL_NV_fragment_program_option");
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	vert_prog = piglit_compile_program(GL_VERTEX_PROGRAM_ARB,
					   vert_shader_source);
	frag_prog = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB,
					   frag_shader_source);

	glClearColor(0.5, 0.5, 0.5, 1.0);

	i = 0;
	for (r = 0; r < TEST_ROWS; r++) {
		for (c = 0; c < TEST_COLS; c++) {
			position[i + 0] = (float)((BOX_SIZE / 2) + c *
						  (BOX_SIZE + 1) + 1);
			position[i + 1] = (float)((BOX_SIZE / 2) + r *
						  (BOX_SIZE + 1) + 1);
			position[i + 2] = 0.0f;
			position[i + 3] = 1.0f;
			i += 4;
		}
	}


	/* Generate a bunch of random direction vectors.  Based on the random
	 * direction vector, generate an axis such that the reflection of the
	 * random vector across the axis is { 0, 1, 0 }.
	 */
	srand(time(NULL));
	for (i = 0; i < (ARRAY_SIZE(direction) / 4); i++) {
		const double d[3] = {
			random_float(),
			random_float(),
			random_float()
		};
		const double inv_mag_d = 1.0 /
			sqrt((d[0] * d[0]) + (d[1] * d[1]) + (d[2] * d[2]));
		double a[3];
		double mag_a;


		direction[(i * 4) + 0] = d[0] * inv_mag_d;
		direction[(i * 4) + 1] = d[1] * inv_mag_d;
		direction[(i * 4) + 2] = d[2] * inv_mag_d;
		direction[(i * 4) + 3] = 0.0;

		a[0] = direction[(i * 4) + 0] + 0.0;
		a[1] = direction[(i * 4) + 1] + 1.0;
		a[2] = direction[(i * 4) + 2] + 0.0;
		mag_a = sqrt((a[0] * a[0]) + (a[1] * a[1]) + (a[2] * a[2]));

		axis[(i * 4) + 0] = a[0] / mag_a;
		axis[(i * 4) + 1] = a[1] / mag_a;
		axis[(i * 4) + 2] = a[2] / mag_a;
		axis[(i * 4) + 3] = 0.0;
	}
}
Ejemplo n.º 6
0
int main()
{
    sf::RenderWindow window(sf::VideoMode(2000, 1200), "Do not pop the balloons!");
    std::vector<MyCircle> shapes;

    int active_circles = NUMCIRCLES;
    auto size = window.getSize();

    for (int i=0; i < NUMCIRCLES; ++i) {
        MyCircle shape(&window);
        shape.setRadius(random_float(0.66*CIRCLERADIUS, 1.33*CIRCLERADIUS));
        shape.setPosition(random_float(0, shape.x_max), random_float(0, shape.y_max));
        shape.setFillColor(colours[random_int(0, colours.size()-1)]);
        //shape.setFillColor(sf::Color::Black);
        shape.setDirection(random_direction(), random_direction());
        shape.setSpeed(random_float(0, XSPEED), random_float(0, YSPEED));
        shapes.push_back(shape);
    }

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::Yellow);
        for (int i=0; i < NUMCIRCLES; ++i) {
            auto pos = shapes[i].getPosition();
            auto rad = shapes[i].getRadius();
            auto x = pos.x;
            auto y = pos.y;

            //Get mouse click
            if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
                auto mouse_pos = sf::Mouse::getPosition(window);
                if (mouse_pos.x > x && mouse_pos.x < x + 2*rad &&
                    mouse_pos.y > y && mouse_pos.y < y + 2*rad) {
                    shapes[i].setFillColor(sf::Color::Transparent);
                    shapes[i].setRadius(0.f);
                    shapes[i].setSpeed(0.f, 0.f);
                    active_circles -= 1;

                    if (active_circles == 0) window.close();
                }
            }

            // From here is the code to make the ball bounce left to right
            if (x >= shapes[i].x_max) {
                shapes[i].x_direction = -1;
            }

            if (x <= 0) {
                shapes[i].x_direction = 1;
            }

            x += shapes[i].x_speed * shapes[i].x_direction;
            // End

            // Copy the above code, but swap Y for X to make the ball bounce up and down
            if (y >= shapes[i].y_max) {
                shapes[i].y_direction = -1;
            }

            if (y <= 0) {
                shapes[i].y_direction = 1;
            }

            y += shapes[i].y_speed * shapes[i].y_direction;

            shapes[i].setPosition(x, y);
            window.draw(shapes[i]);
        }
        window.display();
    }

    return 0;
}
Ejemplo n.º 7
0
Bird::Bird(World world, float maxV, float minsize, float maxsize) {
    Vector pos = Vector();
    Vector vel = Vector();

    id = -1;
    species = -1;
    predator = false;
    this->mass = random_float(minsize, maxsize);

//	float px = (float)(rrand((500<<4)) -(500<<3));
//	float py = (float)(rrand((450<<4)) -(450<<3));
//	float pz = (float)(rrand(200) + 500);
//
//	while(px < 0) px += world.x;
//	while(py < 0) py += world.y;
//	while(pz < 0) pz += world.z;
//
//	while(px > world.x) px -= world.x;
//	while(py > world.y) py -= world.y;
//	while(pz > world.z) pz -= world.z;
//
//	pos.setX(px);
//	pos.setY(py);
//	pos.setZ(pz);

    pos.setX((float)random_int(50, world.x - 50));
    pos.setY((float)random_int( world.y/3, world.y/2));
    //pos.setY(world.y / 2);
    pos.setZ((float)random_int(50, world.z - 50));

    this->pos = pos;

//	float vx = (float)(rrand(51) - 25);
//	float vy = (float)(rrand(51) - 25);
//	float vz = (float)(rrand(51) - 25);
//
//	vel.setX(vx);
//	vel.setY(vy);
//	vel.setZ(vz);

    vel.setX(random_int(-maxV, maxV));
    vel.setY(0);
    vel.setZ(random_int(-maxV, maxV));

    this->vel = vel;
    this->maxV = maxV;
    maxA = 0;

    sight_distance = 0;
    min_separation = 0;
    alignment_radius = 0;
    species_avoidance_radius = 0;
    predator_avoidance_radius = 0;

    coef_cohesion = 0;
    coef_separation = 0;
    coef_alignment = 0;
    coef_avoidance = 0;

    wander_radius = 0;
    wander_distance = 0;

    vertical_sight_angle = 0;
    horizontal_sight_angle = 0;
    max_turn = 0;
}
Ejemplo n.º 8
0
// Copyright 2015 Adobe Systems Incorporated
// All Rights Reserved.

Var x, y, c;
ImageParam ip(type_of<uint8_t>(), 3, "image 1");

Param<float> red("red channel multiplier", 1.0f, 0.0f, 2.0f);
Param<float> green("green channel multiplier", 1.0f, 0.0f, 2.0f);
Param<float> blue("blue channel multiplier", 1.0f, 0.0f, 2.0f);
Param<float> noise("noise amount", 0.0f, 0.0f, 1.0f);
Param<bool> invert("invert", false);

Expr noiseVal = noise * (random_float() - 0.5) * 255;
Expr input = select(invert, 255 - ip(x, y, c), ip(x, y, c));

result(x, y, c) = cast<int8_t>(
    clamp( select(
        c == 0, input * red,
        c == 1, input * green,
        c == 2, input * blue,
        0) + noiseVal, 0, 255)
);

result.vectorize(x, 8).parallel(y, 4);