// Pressing the mouse stops and starts the sound
 public void mousePressed() {
   if (tone.isPlaying()) {
     tone.stop(); //   The sound can be stopped with the function stop().
   } else {
     tone.repeat();
   }
 }
  public void draw() {
    if (tone.isPlaying()) {
      background(255);
    } else {
      background(100);
    }

    // Set the volume to a range between 0 and 1.0
    float ratio = (float) mouseX / width;
    tone.setVolume(ratio);

    // Set the rate to a range between 0 and 88,200
    // Changing the rate alters the pitch
    ratio = (float) mouseY / height;

    // The rate is set according to the mouseY position.
    tone.setRate(ratio * 88200);

    // Draw some rectangles to show what is going on
    stroke(0);
    fill(175);
    rect(0, 160, mouseX, 20);
    stroke(0);
    fill(175);
    rect(160, 0, 20, mouseY);
  }
  public void setup() {
    size(200, 200);
    // Start Sonia engine.
    Sonia.start(this);

    // Create a new sample object.
    tone = new Sample("tone.wav");

    // Loop the sound forever
    // (well, at least until stop() is called)
    tone.repeat();
    smooth();
  }
  Instrument replace_instrument(byte[] mod_header, int idx, byte[] data_input, int replacetranspose)
      throws IOException {
    int header_offset, sample_data_length;
    int loop_start, loop_length, sample_idx, fine_tune;
    Instrument instrument;
    Sample sample;
    // byte[] raw_sample_data;
    short[] sample_data;
    header_offset = (idx - 1) * 30 + 20;

    instrument = new Instrument();
    sample = new Sample();
    // sample_data_length = unsigned_short_be( mod_header, header_offset + 22 ) << 1;
    sample_data_length = data_input.length;
    System.out.println("Sampledata bytes: " + sample_data_length);
    fine_tune = mod_header[header_offset + 24] & 0x0F;
    if (fine_tune > 7) {
      fine_tune -= 16;
    }
    sample.transpose = (fine_tune + (replacetranspose * 10) << IBXM.FP_SHIFT) / 96;
    sample.volume = mod_header[header_offset + 25] & 0x7F;
    loop_start = unsigned_short_be(mod_header, header_offset + 26) << 1;
    loop_length = unsigned_short_be(mod_header, header_offset + 28) << 1;
    if (loop_length < 4) {
      loop_length = 0;
    }
    // raw_sample_data = new byte[ sample_data_length ];
    sample_data = new short[sample_data_length];

    for (sample_idx = 0; sample_idx < data_input.length; sample_idx++) {
      sample_data[sample_idx] = (short) (data_input[sample_idx] << 8);
    }
    sample.set_sample_data(sample_data, loop_start, loop_length, false);
    instrument.set_num_samples(1);
    instrument.set_sample(0, sample);
    return instrument;
  }