Skipping samples

  1. What will happen if we skip samples from wav file? For example: If I have 1000 samples in wave file and if I want to read every 10th sample from table. It means that it will increase the speed of audio. Am I right?

  2. If I want to increase or decrease the speed of audio in code format. how to implement it any guidance?

Below is the general code which I have implemented. I am not able to increase or decrease the speed of audio.

void setSpeed1(float num){

      float speed1 = num/num_samples;
      factor = speed1;
      
      
        }


int16_t output1(){

     if(z == 0){
       z = factor;
      } 

if(factor < 1){

  // Slower playing of samples

   int index = int (z);
  
     dacc_write_conversion_data(DACC_INTERFACE, buf[index]); 
      
   z = speedglobal + float(z);

 if(index > num_samples){
      z = index- num_samples;
  
  }

  
  }


  if(factor > 1){

  // Faster playing of samples

   int index = int (z);
  
     dacc_write_conversion_data(DACC_INTERFACE, buf[index]); 
      
   z = speedglobal + float(z);

 if(index > num_samples){
      z = index- num_samples;
  
  }

  
  }

Google "phase accumulator"

That's very nice answer :slight_smile:

suraj_150890:

  1. What will happen if we skip samples from wav file? For example: If I have 1000 samples in wave file and if I want to read every 10th sample from table. It means that it will increase the speed of audio. Am I right?

You will increase the speed, but you will also cause aliasing.

Perhaps you've heard of the Nyquist sampling theorem? In a nutshell, it says the highest frequency discrete time sampled signals can represent is half the sample frequency. The most widely understood result is if you wish to sample an analog signal, you must do so with a sampling frequency at least twice as fast as the highest frequencies contained in the original signal. Failure to sample fast enough (or filter away the high frequencies before sampling) causes aliasing, where the higher frequencies appear as lower frequencies, which sounds like terrible distortion.

Many other less widely known results of Nyquist also exist. If you decimate (discard samples) without first filtering away the too-high frequenies, the situation is basically the same as sampling an analog signal too slow. Time and time again, novices neglect this. I know, because I'd done it, and I've seen many others go down this path too. Discarding data is such a simple and easy thing to do, and implementing a low-pass filter is tough and CPU intensive. There seems to be a natural human tendency to want to do the simplest, easiest algorithm, even if it means willfully neglecting the unpleasant reality of Nyquist sampling theory (at least before hearing the terrible result).

So the purpose of this long-winded message is simply this: don't fool yourself. Nyquist aliasing is real and it applies to discarding samples just as much as it does to sampling an analog signal too slowly. The too-high frequencies don't just go away because you don't want them. Unless you filter them away before you discard those samples, those high frequencies can and will alias into the new smaller bandwidth, usually leading to terrible sounding distortion.

You are much better off changing the sample rate and outputting all the samples. This will result in the speeding up and the pitch shifting of the sample.

You can pitch shift without changing the speed by re-sampling the sample, that is creating artificial sample points by interpolating between samples. But as Paul says dropping samples will lead to aliasing so that case is best covered by increasing the playback sample rate.

Changing the speed without pitch shifting is a lot more complex but can be done by a combination of resampling and changing the sample rate.

suraj_150890:
if I want to read every 10th sample from table. It means that it will increase the speed of audio. Am I right?

I think it depends on what you do with the samples. I mean..... sure, you can read every 10th sample. But sample systems also need to know something about the sampling period (the time between these bunch of values stored in our computer etc).

So if the original sampling period is T seconds, and if you decide to keep every 10th sample..... and then you assume that the sampling period for your reduced set of samples is still 'T' seconds..... then yeah, sure.... the audio output played back at a rate of 1/T (Hertz) will effectively appear to be 10 times faster (relative to the original audio).

But 9 values are discarded for every 10th sample you keep. So this could really result in not just a loss of quality, but also extra undesirable stuff (like 'aliasing' learned from sampling theory).

Google oversampling and decimation. Generally changing sample rate means doing both,
with steep digital anti-aliasing filters too. As has been mentioned its easier to change the
playback rate and use all the samples than resampling and filtering.

For instance to resample a waveform from 20kSPS to 6kSPS you might over-sample by 3 to 60kSPS,
low-pass digital brick-wall filter to 2.5kHz cut off, then decimate by a factor of 10. If you then
play the result back at 20kSPS rather than 6 you will have multiplied frequencies by 10/3.
(Or playback at 6kSPS to give a low pass filtered signal with lower datarate)

You could also do that by playing the original samples back at 66.7kSPS directly, without
any processing.

[ Note that if you want to scale frequencies in real-time, none of this works but there are techniques
for giving the impression of changing frequencies without changing playback rate ]

MarkT:
low-pass digital brick-wall filter to 2.5kHz cut off,

Is that like a very high roll-off filter that tries to approximate brickwall? Nice recommendations with the resampling method markT.

Southpark:
Is that like a very high roll-off filter that tries to approximate brickwall?

Yes.
However the steeper you make the cut off slope the more the filter will ring.

Grumpy_Mike:
Yes.
However the steeper you make the cut off slope the more the filter will ring.

Thanks Grumpy_Mike!

And the more math it will require, probably more than you can do with 8 bit AVR chips.