Пример #1
0
int main(int argc, const char *argv[])
#endif
{
	int s, whi, t;
	A2_handle h, ph, wh[WAVES];
	A2_driver *drv;
	A2_config *cfg;
	A2_state *state;

	signal(SIGTERM, breakhandler);
	signal(SIGINT, breakhandler);

	/* Command line switches */
	parse_args(argc, argv);

	/* Configure and open master state */
	if(!(drv = a2_NewDriver(A2_AUDIODRIVER, audiodriver)))
		fail(1, a2_LastError());
	if(!(cfg = a2_OpenConfig(samplerate, audiobuf, channels,
			A2_TIMESTAMP | A2_REALTIME | A2_STATECLOSE)))
		fail(2, a2_LastError());
	if(drv && a2_AddDriver(cfg, drv))
		fail(3, a2_LastError());
	if(!(state = a2_Open(cfg)))
		fail(4, a2_LastError());
	if(samplerate != cfg->samplerate)
		printf("Actual master state sample rate: %d (requested %d)\n",
				cfg->samplerate, samplerate);

	/* Load wave player program */
	if((h = a2_Load(state, "data/testprograms.a2s", 0)) < 0)
		fail(5, -h);
	if((ph = a2_Get(state, h, "PlayTestWave2")) < 0)
		fail(6, -ph);

	/* Allocate wave render buffer */
	if(!(wbuf = malloc(sizeof(int16_t) * WAVELEN)))
		fail(7, A2_OOMEMORY);

	/* Abuse! */
	memset(wh, 0, sizeof(wh));
	whi = 0;
	t = a2_GetTicks();
	a2_Now(state);
	fprintf(stderr, "Starting!\n");
	while(!do_exit)
	{
		A2_handle vh;
		float a, fmd;

		/* Unload! */
		if(wh[whi])
			a2_Release(state, wh[whi]);

		/* Render! */
		a = 32767.0f;
		fmd = a2_Rand(state, FMDEPTH);
		for(s = 0; s < WAVELEN; ++s)
		{
			float phase = s * 2.0f * M_PI / WAVEPER;
			float poffs = sin(phase) * fmd;
			wbuf[s] = sin(phase + poffs) * a;
			a *= DECAY;
			fmd *= FMDECAY;
		}

		wh[whi] = a2_UploadWave(state, A2_WWAVE, WAVEPER, 0,
				A2_I16, wbuf, sizeof(int16_t) * WAVELEN);
		if(wh[whi] < 0)
			fail(8, -wh[whi]);

		/* Play! */
		vh = a2_Start(state, a2_RootVoice(state), ph,
				a2_Rand(state, 1.0f), 0.5f, wh[whi]);
		if(vh < 0)
			fail(10, -vh);

		a2_Wait(state, DELAY);

		/* Stop and release! */
		a2_Send(state, vh, 1);
		a2_Release(state, vh);

		whi = (whi + 1) % WAVES;

		/* Timing... */
		if(whi == 0)
		{
			t += DELAY * WAVES;
			while((t - (int)a2_GetTicks() > 0) && !do_exit)
				a2_Sleep(1);
			fprintf(stderr, "(batch)\n");
			a2_Now(state);
		}
	}

	a2_Close(state);
	free(wbuf);
	return 0;
}
Пример #2
0
int main(int argc, const char *argv[])
{
	A2_handle h, ph, vh;
	A2_driver *drv;
	A2_config *cfg;
	A2_interface *iface;
	signal(SIGTERM, breakhandler);
	signal(SIGINT, breakhandler);

	/* Command line switches */
	parse_args(argc, argv);

	/* Configure and open master state */
	if(!(drv = a2_NewDriver(A2_AUDIODRIVER, audiodriver)))
		fail(a2_LastError());
	if(!(cfg = a2_OpenConfig(samplerate, audiobuf, channels,
			A2_TIMESTAMP | A2_AUTOCLOSE)))
		fail(a2_LastError());
	if(drv && a2_AddDriver(cfg, drv))
		fail(a2_LastError());
	if(!(iface = a2_Open(cfg)))
		fail(a2_LastError());
	if(samplerate != cfg->samplerate)
		printf("Actual master iface sample rate: %d (requested %d)\n",
				cfg->samplerate, samplerate);

	/* Load wave player program */
	if((h = a2_Load(iface, "data/testprograms.a2s", 0)) < 0)
		fail(-h);
	if((ph = a2_Get(iface, h, "PlayTestWave")) < 0)
		fail(-ph);

	/* Generate wave */
	fprintf(stderr, "Generating wave...\n");
	if((h = upload_wave(iface, 100000)) < 0)
		fail(-h);

	/* Start playing! */
	fprintf(stderr, "Playing...\n");
	a2_TimestampReset(iface);
	vh = a2_Start(iface, a2_RootVoice(iface), ph, 0.0f, 1.0f, h);
	if(vh < 0)
		fail(-vh);

	/* Wait for completion or abort */
	while(!do_exit)
	{
		a2_Sleep(100);
		a2_PumpMessages(iface);
	}

	a2_TimestampReset(iface);
	a2_Send(iface, vh, 1);
	a2_Sleep(1000);

	/*
	 * Not very nice at all - just butcher everything! But this is supposed
	 * to work without memory leaks or anything, so we may as well test it.
	 */
	a2_Close(iface);
	return 0;
}