Exemple #1
0
int main()
{
	setup_demo( buf, synth );
	
	// generate 1000 clocks of square wave
	int length = 1000;
	int amplitude = 1;
	for ( int time = 0; time < length; time += 10 )
	{
		synth.update( time, amplitude );
		amplitude = -amplitude;
	}
	
	// find out how many samples of sine wave to generate
	int count = buf.count_samples( length );
	blip_sample_t temp [4096];
	for ( int i = 0; i < count; i++ )
	{
		double y = sin( i * (3.14159 / 100) );
		temp [i] = y * 0.30 * blip_sample_max; // convert to blip_sample_t's range
	}
	
	// mix sine wave's samples into Blip_Buffer
	buf.mix_samples( temp, count );
	
	// end frame and show samples
	buf.end_frame( length );
	show_buffer( buf );
	wait_button();
	 
	return 0;
}
Exemple #2
0
int main()
{
	long sample_rate = 44100;
	
	// Sample rate sets how many samples are generated per second
	if ( buf.set_sample_rate( sample_rate ) )
		return 1; // out of memory
	buf.bass_freq( 0 ); // keep waveforms perfectly flat
	
	// Setup synth
	synth.output( &buf );
	synth.volume( 0.50 );
	
	while ( !button_pressed() )
	{
		// Mouse sets clock rate, higher to the right. The higher the clock
		// rate, the more packed the waveform becomes.
		long rate = sample_rate * (mouse_x() * 10 + 1);
		
		// Clock rate sets how many time units there are per second
		buf.clock_rate( rate );
		
		// Generate random waveform, with each transition spaced 50 clocks apart.
		srand( 1 );
		buf.clear();
		for ( int time = 0; time < 500; time += 50 )
			synth.update( time, rand() % 20 - 10 );
		
		buf.end_frame( 600 );
		
		show_buffer_unscaled( buf );
	}
	
	return 0;
}
Exemple #3
0
// Read samples as unsigned 8-bit
long read_samples( Blip_Buffer& buf, unsigned char* out, long out_size )
{
	// 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 );
	
	for ( long n = count; n--; )
	{
		// Read 16-bit sample and convert to output format
		long s = reader.read();
		reader.next( bass );
		if ( (short) s != s ) // clamp to 16 bits
			s = 0x7fff - (s >> 24);
		
		*out++ = (s >> 8) + 0x80;
	}
	
	// End reading and remove the samples
	reader.end( buf );
	buf.remove_samples( count );
	
	return count;
}
Exemple #4
0
// Quick test of functions
int main()
{
	Blip_Buffer buf;
	Blip_Synth<blip_low_quality,20> synth;
	
	// Setup buffer
	buf.clock_rate( 44100 );
	if ( buf.set_sample_rate( 44100 ) )
		return 1;
	synth.output( &buf );
	synth.volume( 0.5 );
	
	// Add wave that goes from 0 to 50% to -50%
	synth.update( 4,  10 );
	synth.update( 8, -10 );
	buf.end_frame( 30 );
	
	// Read samples as this type
	typedef float sample_t; // floating-point
	//typedef unsigned short sample_t; // unsigned 16-bit
	//typedef unsigned char sample_t; // unsigned 8-bit
	
	// Read and display samples
	const int max_samples = 30;
	sample_t samples [max_samples];
	int count = read_samples( buf, samples, max_samples );
	
	for ( int i = buf.output_latency() + 1; i < count; i++ )
		printf( "%.2f,", (double) samples [i] );
	printf( "\n" );
	
	return 0;
}
Exemple #5
0
void Nes_Vrc6_Apu::run_square( Vrc6_Osc& osc, blip_time_t end_time )
{
	Blip_Buffer* output = osc.output;
	if ( !output )
		return;
	
	int volume = osc.regs [0] & 15;
	if ( !(osc.regs [2] & 0x80) )
		volume = 0;
	
	int gate = osc.regs [0] & 0x80;
	int duty = ((osc.regs [0] >> 4) & 7) + 1;
	int delta = ((gate || osc.phase < duty) ? volume : 0) - osc.last_amp;
	blip_time_t time = last_time;
	if ( delta )
	{
		osc.last_amp += delta;
		output->set_modified();
		square_synth.offset( time, delta, output );
	}
	
	time += osc.delay;
	osc.delay = 0;
	int period = osc.period();
	if ( volume && !gate && period > 4 )
	{
		if ( time < end_time )
		{
			int phase = osc.phase;
			output->set_modified();
			
			do
			{
				phase++;
				if ( phase == 16 )
				{
					phase = 0;
					osc.last_amp = volume;
					square_synth.offset( time, volume, output );
				}
				if ( phase == duty )
				{
					osc.last_amp = 0;
					square_synth.offset( time, -volume, output );
				}
				time += period;
			}
			while ( time < end_time );
			
			osc.phase = phase;
		}
		osc.delay = time - end_time;
	}
}
Exemple #6
0
void show_buffer_unscaled( Blip_Buffer& in )
{
	blip_sample_t buf [scope_width];
	long count = in.read_samples( buf, scope_width );
	while ( count < scope_width )
		buf [count++] = 0;
	
	scope.draw( buf, scope_width );
	
	// remove remaining samples
	while ( in.read_samples( buf, scope_width ) ) { }
}
void Nes_Vrc7_Apu::end_frame( blip_time_t time )
{
    if ( time > next_time )
        run_until( time );

    next_time -= time;
    assert( next_time >= 0 );

    for ( int i = osc_count; --i >= 0; )
    {
        Blip_Buffer* output = oscs [i].output;
        if ( output )
            output->set_modified();
    }
}
Exemple #8
0
long Nes_Nonlinearizer::make_nonlinear( Blip_Buffer& buf, long count )
{
	require( apu );
	long avail = buf.samples_avail();
	if ( count > avail )
		count = avail;
	if ( count && enabled )
	{
		
		Blip_Buffer::buf_t_* p = buf.buffer_;
		long accum = this->accum;
		long prev = this->prev;
		for ( unsigned n = count; n; --n )
		{
			long entry = ENTRY( accum );
			check( (entry >= 0) == (accum >= 0) );
			accum += *p;
			*p++ = (entry - prev) << (blip_sample_bits - 16);
			prev = entry;
		}
		
		this->prev = prev;
		this->accum = accum;
	}
	
	return count;
}
Exemple #9
0
int main()
{
	while ( !button_pressed() )
	{
		setup_demo( buf, synth );
		
		// base frequency and amplitude on mouse position
		int period = mouse_x() * 100 + 10;
		int amplitude = mouse_y() * 9 + 1;
		
		// generate alternating signs of square wave, spaced by period
		int time = 0;
		while ( time < 1000 )
		{
			amplitude = -amplitude;
			synth.update( time, amplitude );
			time += period;
		}
		buf.end_frame( 1000 );
		
		show_buffer( buf );
	}
	
	return 0;
}
Exemple #10
0
// Callback called when Alure needs more data to ffed a buffer
ALuint StreamCB (void* userdata, ALubyte *data, ALuint bytes) {

  double period = SR / (2* freq);  // How many samples need to do a half cycle.
  size_t lenght = bytes / 2;          // Lenght in "samples"

  unsigned const amplitude = 9;
  while (offset < lenght) {
      sign = -sign;
      synth.update(offset, amplitude * sign);
      offset += period;
  }
  blipbuf.end_frame(lenght);
  offset -= lenght; // adjust time to new frame

  return 2* blipbuf.read_samples ((blip_sample_t*) data, lenght ); // return bytes!

}
Exemple #11
0
void Nes_Vrc6_Apu::run_saw( blip_time_t end_time )
{
	Vrc6_Osc& osc = oscs [2];
	Blip_Buffer* output = osc.output;
	if ( !output )
		return;
	output->set_modified();
	
	int amp = osc.amp;
	int amp_step = osc.regs [0] & 0x3F;
	blip_time_t time = last_time;
	int last_amp = osc.last_amp;
	if ( !(osc.regs [2] & 0x80) || !(amp_step | amp) )
	{
		osc.delay = 0;
		int delta = (amp >> 3) - last_amp;
		last_amp = amp >> 3;
		saw_synth.offset( time, delta, output );
	}
Exemple #12
0
int main()
{
	// Setup left buffer and wave
	int left_time = 0;
	int left_amp = 0;
	setup_demo( left, left_synth );
	left.clock_rate( left.sample_rate() * 100 );
	
	// Setup right buffer and wave
	int right_time = 0;
	int right_amp = 0;
	setup_demo( right, right_synth );
	right.clock_rate( right.sample_rate() * 100 );
	
	while ( !button_pressed() )
	{
		blip_time_t length = 100000;
		
		// mouse sets frequency of left wave
		int period = 1000 + 6 * mouse_x();
		
		// Add saw wave to left buffer
		do {
			left_synth.update( left_time, left_amp = (left_amp + 1) % 10 );
		} while ( (left_time += period) < length );
		left.end_frame( length );
		left_time -= length;
		
		// Add saw wave of slightly lower pitch to right buffer
		do {
			right_synth.update( right_time, right_amp = (right_amp + 1) % 10 );
		} while ( (right_time += 1000) < length );
		right.end_frame( length );
		right_time -= length;
		
		// buffer to read samples into
		int const buf_size = 2048;
		static blip_sample_t samples [buf_size];
		
		// Read left channel into even samples, right channel into odd samples:
		// LRLRLRLRLR...
		long count = left.read_samples( samples, buf_size / 2, 1 );
		right.read_samples( samples + 1, count, 1 );
		
		play_stereo_samples( samples, count * 2 );
	}
	
	return 0;
}
Exemple #13
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;
}
Exemple #14
0
int main () {
  if (!alureInitDevice(NULL, NULL)) {
    std::fprintf(stderr, "Failed to open OpenAL device: %s\n", alureGetErrorString());
    return 1;
  }

  alGenSources(1, &src);
  if (alGetError() != AL_NO_ERROR) {
    std::fprintf(stderr, "Failed to create OpenAL source!\n");
    alureShutdownDevice();
    return 1;
  }

  // Seeting Blip Buffer

  synth.treble_eq( -18.0f );

  synth.volume (0.80);
  synth.output (&blipbuf);

  // Init Blip Buffer with a buffer of 250ms (second paramter is time in ms)
  if ( blipbuf.set_sample_rate( SR, 1000 / 4 ) ) {
    std::fprintf(stderr, "Failed to create Blip Buffer! Our of Memory\n");
    alureShutdownDevice();
    return 1;
  }
  blipbuf.clock_rate( blipbuf.sample_rate() );
  blipbuf.bass_freq(300); // Equalization like a TV speaker

  stream = alureCreateStreamFromCallback (StreamCB, nullptr, AL_FORMAT_MONO16, SR, SR/2, 0, nullptr);

  if(!stream) {
    std::fprintf(stderr, "Error creating stream! %s\n",  alureGetErrorString());
    alDeleteSources(1, &src);

    alureShutdownDevice();
    return 1;
  }

  if (!alurePlaySourceStream(src, stream, 4, 0, eos_callback, NULL)) {
    std::fprintf(stderr, "Failed to play stream: %s\n", alureGetErrorString());
    isdone = 1;
  }

  alureUpdateInterval(0.005f); // Should be a independint thread  playing the stream

  while(!isdone) {
    freq -= 1;
    if (freq < 1) {
      freq = 600;
    }

    alureSleep(0.02f);
  }
  alureStopSource(src, AL_FALSE);

  alDeleteSources(1, &src);
  alureDestroyStream(stream, 0, NULL);

  alureShutdownDevice();
  return 0;
}
Exemple #15
0
void show_buffer( Blip_Buffer& in )
{
	long count = in.read_samples( sample_buf, buf_size );
	scope.draw( sample_buf, scope_width, (double) count / scope_width );
}