Subversion Repositories DIN Is Noise

Rev

Rev 416 | Rev 690 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
* sine_mixer.cc
* DIN Is Noise is copyright (c) 2006-2018 Jagannathan Sampath
* For more information, please visit http://dinisnoise.org/
*/



#include "dingl.h"
#include "input.h"
#include "viewwin.h"
#include "sine_mixer.h"
#include "ui_list.h"
#include "viewwin.h"
#include "vector2d.h"
#include "circler.h"
#include <fstream>
#include <math.h>
using namespace std;

extern ui_list uis;

extern viewport view;

extern const float PI;
extern const float TWO_PI;

extern ofstream dlog;

extern int MILLION;

extern curve_library sin_lib;

sine_mixer::sine_mixer () : cp_sin ("sine_mixer_sin.crv"), sin_ed ("sine_mixer_sin.ed"), sine_levels ("sine_levels") {
  name = "Sine_Mixer";
  shapeform = 0;
  type = 0;
  sin_ed.add (&cp_sin.crv, this);
  sin_ed.attach_library (&sin_lib);
}

void sine_mixer::load_params () {
  ifstream f (make_fname().c_str(), ios::in);
  string ignore;
  f >> ignore >> num_points >> ignore >> make_shapeform >> ignore >> type;
}

void sine_mixer::save_params () {
  ofstream f (make_fname().c_str(), ios::out);
  string ignore;
  f << "num_points " << int (sp_points.f_value) << " make_shapeform " << cb_make_shapeform.state << " type " << type << endl;
}

sine_mixer::~sine_mixer () {
  widget_save ("d_sine_mixer", ctrls);
  save_params ();
  dlog << "--- destroyed sine mixer ---" << endl;
}

void sine_mixer::num_harmonics (int h) {
  nharmonics = h;
  if (nharmonics < 1) nharmonics = 1;
  prep_harmonics ();
}

void sine_mixer::prep_harmonics () {
  harmonics.resize (nharmonics);
  float df = TWO_PI / (NUM_SINE_SAMPLES - 1);
  funktion& f_sin = *pf_sin;
  for (int i = 0, j = 1; i < nharmonics; ++i, ++j) {
    vector< point<float> >& harmonic = harmonics[i];
    harmonic.resize (NUM_SINE_SAMPLES);
    float dx = j * df;
    float x = 0;
    for (int p = 0; p < NUM_SINE_SAMPLES; ++p) {
      point<float>& hp = harmonic[p];
      hp.x = cos(x);
      hp.y = f_sin (x, TWO_PI);
      x += dx;
      if (x > TWO_PI) x -= TWO_PI;
    }
  }
  norm.resize (NUM_SINE_SAMPLES);
  mix ();
}

void sine_mixer::mix () {
  for (int p = 0; p < NUM_SINE_SAMPLES; ++p) { point<float>& pi = norm[p]; pi.x = pi.y = 0;}
  ss.str(""); ss << name;
  for (int i = 0; i < nharmonics; ++i) {
    float lev = sine_levels.values[i];
    if (lev > 0) {
      vector< point<float> >& harmonic = harmonics[i];
      for (int p = 0; p < NUM_SINE_SAMPLES; ++p) {
        point<float>& np = norm[p];
        point<float>& hp = harmonic[p];
        np.x += (lev * hp.x);
        np.y += (lev * hp.y);
      }
      ss << '_' << (i+1);
    }
  }
  normalise ();
}

void sine_mixer::normalise () {
  int n = NUM_SINE_SAMPLES;
  if (n) {
    point<float>& np0 = norm[0];
    float maxy = fabs (np0.y), maxx = fabs (np0.x);
    for (int i = 0; i < n; ++i) {
      point<float>& pi = norm[i];
      float vx = fabs (pi.x), vy = fabs (pi.y);
      if (vx > maxx) maxx = vx;
      if (vy > maxy) maxy = vy;
    }
    if (maxx != 0) for (int p = 0; p < n; ++p) norm[p].x /= maxx;
    if (maxy != 0) for (int p = 0; p < n; ++p) norm[p].y /= maxy;
  }
}

void sine_mixer::render () {
  undo = !cb_auto_apply.state;
  shapeform = cb_make_shapeform.state;
  points.clear ();
  if (shapeform) {
    for (int i = 0, j = norm.size(); i < j; ++i) {
      point<float>& ni = norm[i];
      points.push_back (point<float>(ni.x, ni.y));
    }
  } else {
    float x = 0;
    float dx = 1.0f / (NUM_SINE_SAMPLES - 1);
    for (int i = 0, j = norm.size(); i < j; ++i) {
      point<float>& ni = norm[i];
      points.push_back (point<float>(x, ni.y));
      x += dx;
    }
  }
}

void sine_mixer::setup () {
  plugin::setup ();
  widget* _ctrls [] = {&sine_levels, &sp_points, &cb_make_shapeform, &ol_sin, &b_edit, &cb_paint_harmonics};
  for (int i = 0; i < 6; ++i) ctrls.push_back (_ctrls[i]);
  num_ctrls = ctrls.size ();
  load_params ();
  sp_points.set_label ("Points");
  sp_points.set_limits (1, MILLION);
  sp_points.set_value (num_points);
  sp_points.set_delta (1);
  sp_points.set_listener (this);
  sine_levels.set_listener (this);
  cb_make_shapeform.set_label ("Make shapeform");
  cb_make_shapeform.set_state (make_shapeform);
  cb_make_shapeform.set_listener (this);
  cb_paint_harmonics.set_label ("Paint harmonics");
  cb_paint_harmonics.set_listener (this);

  ol_sin.set_listener (this);
  b_edit.set_listener (this);
  widget_load ("d_sine_mixer", ctrls);
  ol_sin.set_pos (cb_paint_harmonics.extents.left, cb_paint_harmonics.extents.bottom - get_line_height ());
  ol_sin.add_child (&b_edit);
  b_edit.set_label ("Edit");
  set_type ();
  num_harmonics (sine_levels.nlev);
  render ();
}

void sine_mixer::set_type () {
  if (type) {
    pf_sin = &cp_sin;
    ol_sin.option.set_text (" Custom sin");
    b_edit.set_color (clr.r, clr.g, clr.b);
    b_edit.set_listener (this);
  } else {
    pf_sin = &st_sin;
    ol_sin.option.set_text (" Standard sin");
    b_edit.set_color (0.25f, 0.25f, 0.25f);
    b_edit.set_listener (0);
  }
  int spacer = 10; b_edit.set_pos (ol_sin.option.extents.right + spacer, ol_sin.option.extents.bottom);
}

void sine_mixer::changed (field& f) {
  if (&f == &sp_points.f_value) {
    int s = f;
    set_samples (s);
    do_render ();
  }
}

void sine_mixer::changed (levels& l) {
  if (l.paint != cb_paint_harmonics.state) cb_paint_harmonics.set_state (l.paint);
  mix ();
  do_render ();
}

void sine_mixer::set_samples (int s) {
  NUM_SINE_SAMPLES = s;
  if (NUM_SINE_SAMPLES < MIN_SINE_SAMPLES) NUM_SINE_SAMPLES = MIN_SINE_SAMPLES;
  sp_points.set_value (NUM_SINE_SAMPLES);
  prep_harmonics ();
}

void sine_mixer::changed (checkbutton& cb) {
  if (&cb == &cb_make_shapeform) {
    render ();
    if (cb_auto_apply.state) {
      plugin::mix = undo = 1;
      uis.crved->apply_plugin (this);
      plugin::mix = undo = 0;
    }
  } else if (&cb == &cb_paint_harmonics) {
    sine_levels.paint = cb_paint_harmonics.state;
  } else plugin::changed (cb);
}

void sine_mixer::picked (label& lbl, int dir) {
  type = !type;
  set_type ();
  prep_harmonics ();
  render ();
}

void sine_mixer::clicked (button& b) {
  if (&b == &b_edit) {
    uis.set_current (&sin_ed);
  } else plugin::clicked (b);
}

void sine_mixer::edited (curve_editor* e, int i) {
  cp_sin.sol.update ();
  curve_listener::edited (e, i);
  prep_harmonics ();
  render ();
}