Example #1
0
// Read samples as floating-point, with values staying
// between 'low' and 'high' (defaults to -1.0 to 1.0).
long read_samples( Blip_Buffer& buf, float* out, long out_size,
		float low = -1.0f, float high = 1.0f )
{
	// Limit number of samples read to those available
	long count = buf.samples_avail();
	if ( count > out_size )
		count = out_size;
	
	// Begin reading samples from Blip_Buffer
	Blip_Reader reader;
	int bass = reader.begin( buf );
	
	float factor = (high - low) * 0.5f;
	float offset = low + 1.0f * factor;
	unsigned long const sample_range = 1UL << blip_sample_bits;
	factor *= 1.0f / (sample_range / 2);
	
	for ( long n = count; n--; )
	{
		// Read sample at full resolution and convert to output format
		long s = reader.read_raw();
		reader.next( bass );
		*out++ = s * factor + offset;
		if ( (unsigned long) (s + sample_range / 2) >= sample_range )
		{
			// clamp
			out [-1] = high;
			if ( s < 0 )
				out [-1] = low;
		}
	}
	
	// End reading and remove the samples
	reader.end( buf );
	buf.remove_samples( count );
	
	return count;
}