static A2_handle upload_wave(A2_interface *iface, unsigned len) { A2_errors res; A2_handle wh, sh; int i; int s = 0; int16_t buf[FRAGSIZE]; if((wh = a2_NewWave(iface, A2_WMIPWAVE, 128, 0)) < 0) return wh; if((sh = a2_OpenStream(iface, wh, 0, 0, 0)) < 0) { a2_Release(iface, wh); return sh; } while(s < len) { for(i = 0; (i < FRAGSIZE) && (s < len); ++i, ++s) buf[i] = sin(s * 2.0f * M_PI / 100 + sin(s * .0013) * sin(s * .002) * 10) * 32767.0f; if((res = a2_Write(iface, sh, A2_I16, buf, i * sizeof(int16_t)))) { a2_Release(iface, sh); a2_Release(iface, wh); return -res; } } if((res = a2_Release(iface, sh))) { a2_Release(iface, wh); return -res; } return wh; }
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; }