Shop OBEX P1 Docs P2 Docs Learn Events
floating point array — Parallax Forums

floating point array

bob800bob800 Posts: 2
edited 2014-02-01 21:43 in Propeller 1
I'd like to create a floating-point array, mapping various MIDI note values to their respective frequencies. I found the following C code which should accomplish this:
[COLOR=#000000]float midi[127];[/COLOR]
int a = 440;
for (int x = 0; x < 127; ++x)
{   midi[x] = (a / 32) * (2 ^ ((x - 9) / 12)); [COLOR=#000000]}[/COLOR]

I know how to convert most this to SPIN language, using one of the available floating-point libraries, but where I am lost is how to create a floating point array for midi[]. E.g., the F32 float library contains floating-point arithmetic functions, but how do I store the results in a variable? When I try to define anything as "float" in the VAR block, I get the following error: "Expected BYTE, WORD, or LONG".

Any suggestions would be greatly appreciated!

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-02-01 20:54
    Floats in Spin are stored as LONGs.

    -Phil
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-01 21:07
    There are no floating point variables in Spin -- they're stored as a long (as Phil points out), but can't be dealt with outside an FP library. It would probably be simpler and use less memory to create a table of MIDI notes; this would take 128 longs that you can look-up with an index versus the 680 longs consumed by F32.
  • lonesocklonesock Posts: 917
    edited 2014-02-01 21:43
    I do something similar in the Karplus-Strong synthesis. Here's the relevant code:
    DAT
    {{
      Matched to the MIDI spec, freq = 440 * 2 ^ ( (N - 69) / 12 )
      MIDI note ratios for 0..11 (every 12 notes goes up an octave...just divide the period by 2)
      fixed point period = sample_rate << fraction_bits / frequency
      period = sample_rate << fraction_bits * 2^32 / 2^32 / frequency
      using the ** operator (x ** y) means (x * y / 2^32)
      so if ratio = 2^32 / frequency
      period = sample_rate << fraction_bits ** ratio
    }}
    ratio0  long $1F4FDA4C  ' 2^32 / 8.175799 Hz
    ratio1  long $1D8DF51D  ' 2^32 / 8.661957 Hz
    ratio2  long $1BE5501B  ' 2^32 / 9.177024 Hz
    ratio3  long $1A548077  ' 2^32 / 9.722718 Hz
    ratio4  long $18DA2FC0  ' 2^32 / 10.300861 Hz
    ratio5  long $17751ABC  ' 2^32 / 10.913382 Hz
    ratio6  long $16241056  ' 2^32 / 11.562326 Hz
    ratio7  long $14E5F099  ' 2^32 / 12.249857 Hz
    ratio8  long $13B9ABB9  ' 2^32 / 12.978272 Hz
    ratio9  long $129E412A  ' 2^32 / 13.750000 Hz
    ratio10 long $1192BEC8  ' 2^32 / 14.567618 Hz
    ratio11 long $10964004  ' 2^32 / 15.433853 Hz
    
    PUB set_frequency( Hz )
      ' clamped to max_delay internally
      return (sample_rate / Hz)
    
    PUB set_note( MIDI_note )
      ' clamped to max_delay internally
      return ((sample_rate ** ratio0[MIDI_note//12]) >> (MIDI_note / 12))
    
    Jonathan
Sign In or Register to comment.