Advertisement
Guest User

Untitled

a guest
Feb 8th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. void Crusher::update(void)
  2. {
  3.     audio_block_t *block;
  4.     block = receiveWritable();
  5.  
  6.     if (!block) return;
  7.  
  8.     uint32_t *data = (uint32_t *)(block->data);
  9.  
  10.     uint16_t res = pow(2,(16-bitDepth)+1);
  11.     uint32_t crushed_sample;
  12.  
  13.     int i = 0;
  14.     while (i < AUDIO_BLOCK_SAMPLES)
  15.     {
  16.        
  17.         //Crushing the sample's resolution
  18.         //This if statement should round the sample to the nearest int wich has the resolution of the desired bit-depth
  19.         if ((data[i] % res) == 0) { //the sample already has the desired resolution
  20.             crushed_sample = data[i];
  21.         } else if (((data[i] % res) > (res/2)) { //sample has to be rounded up to the next int wich has desired resoluton
  22.             crushed_sample = (((data[i] / res) + res) * res);
  23.         } else crushed_sample = ((data[i] / res) * res); //sample has to be rounded down
  24.         }
  25.  
  26.         //Simulating lower sample rate
  27.         for (int j = 0; j < downsampling_factor && i < AUDIO_BLOCK_SAMPLES; j++)
  28.         {
  29.             data[i] = crushed_sample
  30.             i++;
  31.         }
  32.     }
  33.  
  34.     transmit(block);
  35.     release(block);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement