title: play a waveform at a certain frequency in SDL callback function
body: <p>I have a waveform 64 samples long. If the sampling rate is 44100 hz, how can I play(loop) this waveform so that it plays arbitrary frequencies?</p>
  
  <p>frequency = samplerate / waveform duration in samples</p>
  
  <p>Therefore the frequency should be 689hz(44100/64). If I wanted it to be say, 65.41hz(C-2), I would have to do this:</p>
  
  <p>65.41 = 44100 / x</p>
  
  <p>Solving for x yields aprox. 674.208. So I need to figure out what speed to play the waveform at to get this frequency. So we can solve this equation:</p>
  
  <p>64 * x = 674.208</p>
  
  <p>and get about 10.5. So the waveform needs to be played at 10.5% of its original speed.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>double smp_index = 0;
  double freq = .105;
  
  void callback(void *data, Uint8 *buf, int len){
      int i;
      s8 *out;
      out = (s8*) buf;
      if(smp_index &lt; waveform_length){
          for(i = 0; i &lt; len; i ++){
              out[i] = smpdata[(int)smp_index];
              smp_index +=freq;
              if(smp_index &gt;= waveform_length)
                  smp_index = 0;
          }
      }
  }
  </code></pre>
  
  <p>So the resulting audio should be about the note C-2, but its more of a D-2. Is the cast </p>
  
  <pre><code>(int)smp_index
  </code></pre>
  
  <p>causing the problem? I couldn't see any other way to accomplish this...</p>