Skip to content

Instantly share code, notes, and snippets.

@javajawa
Last active August 29, 2015 14:08
Show Gist options
  • Save javajawa/05567c178daba05415c3 to your computer and use it in GitHub Desktop.
Save javajawa/05567c178daba05415c3 to your computer and use it in GitHub Desktop.
Whitenoise Refiner

Simple WhiteNoise Refiner

  • Takes data from a random source as 16-bit samples
  • Applies bit masks to reduce amplitude

Exmaple Usage

./noise | aplay -N -f S16 -c2 -r 96000 -

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main( void )
{
FILE* ifh = fopen( "/dev/urandom", "rb" );
/* Number of samples to buffer */
const size_t buffer_size = 256;
/* Constant for the maximum amplitude desired */
const int16_t max_amp = 0x07FF;
/* Buffer of samples to process */
int16_t* buffer = calloc( buffer_size, sizeof( int16_t ) );
size_t i;
while ( 1 )
{
fread( buffer, sizeof( int16_t ), buffer_size, ifh );
for ( i = 0; i < buffer_size; ++i )
{
if ( buffer[i] > 0 )
{
buffer[i] = buffer[i] & max_amp;
}
else
{
buffer[i] = buffer[i] | ~max_amp;
}
}
fwrite( buffer, sizeof( int16_t ), buffer_size, stdout );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment