pointers help for beginner

Status
Not open for further replies.

emmanuel63

Well-known member
Hello,

I am sorry to post this problem that might look basic for the most of you but i am stuck.

I am working with a simple wavetable synth. I try to find a generic way to change the instrument with pointers. Here is my faulty code :

Code:
int last_index;
const char *instrument[] = {
  "bassoon",
  "clarinet",
  "distortiongt",
  "epiano",
  "flute",
  "frenchhorn"
};

void wavetable_patch() {
  int index = analogRead(A0) / 6;

  if (index != last_index) { 
    wavetable.setInstrument(instrument[instrument_index]);
    last_patch = patch;
  }
}

I would like to be able to generalize this method for various objects. Thank you for helping.
Emmanuel
 
Two problems here.

1: wavetable.setInstrument() wants the name of a big block of data generated by that Python program which converts soundfont files. It doesn't know how to use text names of instruments.

If you have something like this in a file:

Code:
#include "Flute_100kbyte_samples.h"

PROGMEM
static const uint32_t sample_0_Flute_100kbyte_FluteD4[7936] = {
0xfbfffc4b,0xfba4fbac,0xfbbffbb2,0xfc00fbdc,0xfc3afc16,0xfcb8fc57,0xfcf9fcf3,0xfc87fcad,
0xfcabfc77,0xfd8cfd0c,0xfe7dfe0c,0xff24fee0,0xffc4ff61,0x006a002d,0x002d004c,0xffe90000,
0x002effe9,0x00f60099,0x014b0134,0x0192014c,0x01f001d8,0x01ab01d9,0x011f0165,0x00e900e9,
0x013800f2,0x017f0150,0x01f601a7,0x02ac024d,0x02dc02e3,0x02ae02cd,0x02390260,0x026a0242,

Then in you program, you would include Flute_100kbyte_samples.h, and create your instrument array like this:

Code:
const uint32_t *instrument[] = {
  sample_0_Flute_100kbyte_FluteD4,
  // more instruments...
};

2: you probably want to use the map() function rather than dividing by 6. For example:

Code:
  int index = map(analogRead(A0), 0, 1023, 0, 5);

Because analogRead gives 0 to 1023, dividing by 6 will give you a range of 0 to 170. You could just divide by 170, but that wouldn't be quite right either, since 1023 would give 6, which is 7 possible outputs since array indexing starts with zero. You could divide by 171. But the map() function is simpler to use and internally it does the proper scaling to handle these numerical round-off issues so you get the input range very nicely mapped onto the output range.
 
Status
Not open for further replies.
Back
Top