import java.lang.*;
import java.util.*;
import java.io.*;

import javax.sound.sampled.*;

public class instrumentc {
  sampletablec string_table[];
  int string_table_num_octaves = 8;
  int string_table_psize = 0;
  int string_table_seed = 0;
  float bandwidth = 50.0f;
  Random rand;
  instrumentc() {
    rand = new Random();
    alloc_string_tables();
  }
  void alloc_string_tables645() {
    int num_octaves = string_table_num_octaves;
    int ps = string_table_psize;
    string_table = new sampletablec[num_octaves];
    rand.setSeed(string_table_seed);
    for (int i = 0;i < num_octaves;i++) {
      int s = 1 << (ps+i+20-num_octaves);
      float f = 4000.0f * (1 << i);
      string_table[i] = new sampletablec(s,f);    
      string_table[i].id = i;
    }
    int num_primes = main_app.prime_list.num_primes;
    Vector harmonic_list = new Vector();
    main_app.prime_list.gen_harmonics(harmonic_list,1.0f,num_primes-1,256.0f);
    float harmonic[] = new float[harmonic_list.size()+1];
    for (int i = 1;i <= harmonic_list.size();i++) {
      Number n = (Number) harmonic_list.get(i-1);
      harmonic[i] = n.floatValue();
    }
    for (int i = 0;i < num_octaves;i = i + 1) {      
      sampletablec.wave_data_size = string_table[i].size;
      sampletablec.FTT_data = new float[sampletablec.wave_data_size][2];
      float f = 1 << (ps+18-num_octaves);
      float b = (float)(bandwidth * Math.exp(Math.log(2)*(ps+8-num_octaves)));
      string_table[i].make_instrument_sample(0,f,b,harmonic,rand);
      sampletablec.rearrange();
      sampletablec.perform(false);
      string_table[i].get_instrument_sample(0,40.0f);
    }  
    sampletablec.wave_data_size = 0;
    sampletablec.FTT_data = null;
  }
  void alloc_string_tables() {
    int num_octaves = string_table_num_octaves;
    int ps = string_table_psize;
    string_table = new sampletablec[num_octaves];
    //string_table[0] = new sampletablec(1 << 14,4000.0);
    //string_table[1] = new sampletablec(1 << 14,8000.0);
    //string_table[2] = new sampletablec(1 << 15,16000.0);
    for (int i = 0;i < num_octaves;i++) {
      int s = 1 << (((i >> 1)+18+ps)-((num_octaves-1) >> 1));
      float f = 2000.0f * (1 << i);
      string_table[i] = new sampletablec(s,f);    
      string_table[i].id = i;
    }
    int num_primes = main_app.prime_list.num_primes;
    Vector harmonic_list = new Vector();
    //harmonic_list.add(new Float(1.0));
    main_app.prime_list.gen_harmonics(harmonic_list,1.0f,num_primes-1,256.0f);

    float harmonic[] = new float[harmonic_list.size()+1];
    for (int i = 1;i <= harmonic_list.size();i++) {
      Number n = (Number) harmonic_list.get(i-1);
      harmonic[i] = n.floatValue();
    }
    for (int i = 0;i < num_octaves;i = i + 2) {
      
      sampletablec.wave_data_size = string_table[i].size;
      sampletablec.FTT_data = new float[sampletablec.wave_data_size][2];
      float f = string_table[i].size >> (i+1);
      float b = (string_table[i].size >> (i+1))*(bandwidth*0.001f);
      //float f = (float) ((1 << 12) / (1 << ((i+1) >> 1)));
      //float b =  100.0f / (float) (1 << ((i+1) >> 1));
      float svol = (float) Math.exp(Math.log(1.2)*(i+1-num_octaves));
      string_table[i].make_instrument_sample(0,f,b,harmonic,rand);         
      if ((i+1) < num_octaves) {
        svol = (float)Math.exp(Math.log(1.2)*(i+2-num_octaves));
        string_table[i+1].make_instrument_sample(1,f/2.0f,b/2.0f,harmonic,rand);    
      }
      sampletablec.rearrange();
      sampletablec.perform(false);
      string_table[i].get_instrument_sample(0,40.0f);
      if ((i+1) < num_octaves) {
        string_table[i+1].get_instrument_sample(1,40.0f);
      }
    }  
    sampletablec.wave_data_size = 0;
    sampletablec.FTT_data = null;
  
  }
}

