int main(int argc, char **argv) { StreamPlayer *player; int i; /* Print out usage if no file was specified */ if(argc < 2) { fprintf(stderr, "Usage: %s <filenames...>\n", argv[0]); return 1; } if(InitAL() != 0) return 1; if(alIsExtensionPresent("AL_SOFT_buffer_samples")) { printf("AL_SOFT_buffer_samples supported!\n"); alBufferSamplesSOFT = alGetProcAddress("alBufferSamplesSOFT"); alIsBufferFormatSupportedSOFT = alGetProcAddress("alIsBufferFormatSupportedSOFT"); } else printf("AL_SOFT_buffer_samples not supported\n"); player = NewPlayer(); /* Play each file listed on the command line */ for(i = 1;i < argc;i++) { if(!OpenPlayerFile(player, argv[i])) continue; printf("Playing %s (%s, %s, %dhz)\n", argv[i], TypeName(player->type), ChannelsName(player->channels), player->rate); fflush(stdout); if(!StartPlayer(player)) { ClosePlayerFile(player); continue; } while(UpdatePlayer(player)) Sleep(10); /* All done with this file. Close it and go to the next */ ClosePlayerFile(player); } printf("Done.\n"); /* All files done. Delete the player, and close OpenAL */ DeletePlayer(player); player = NULL; CloseAL(); return 0; }
int main(int argc, char *argv[]) { enum WaveType wavetype = WT_Sine; ALuint source, buffer; ALint last_pos, num_loops; ALint max_loops = 4; ALint srate = -1; ALint tone_freq = 1000; ALCint dev_rate; ALenum state; int i; for(i = 1;i < argc;i++) { if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { fprintf(stderr, "OpenAL Tone Generator\n" "\n" "Usage: %s <options>\n" "\n" "Available options:\n" " --help/-h This help text\n" " -t <seconds> Time to play a tone (default 5 seconds)\n" " --waveform/-w <type> Waveform type: sine (default), square, sawtooth,\n" " triangle, impulse\n" " --freq/-f <hz> Tone frequency (default 1000 hz)\n" " --srate/-s <sample rate> Sampling rate (default output rate)\n", argv[0] ); return 1; } else if(i+1 < argc && strcmp(argv[i], "-t") == 0) { i++; max_loops = atoi(argv[i]) - 1; } else if(i+1 < argc && (strcmp(argv[i], "--waveform") == 0 || strcmp(argv[i], "-w") == 0)) { i++; if(strcmp(argv[i], "sine") == 0) wavetype = WT_Sine; else if(strcmp(argv[i], "square") == 0) wavetype = WT_Square; else if(strcmp(argv[i], "sawtooth") == 0) wavetype = WT_Sawtooth; else if(strcmp(argv[i], "triangle") == 0) wavetype = WT_Triangle; else if(strcmp(argv[i], "impulse") == 0) wavetype = WT_Impulse; else fprintf(stderr, "Unhandled waveform: %s\n", argv[i]); } else if(i+1 < argc && (strcmp(argv[i], "--freq") == 0 || strcmp(argv[i], "-f") == 0)) { i++; tone_freq = atoi(argv[i]); if(tone_freq < 1) { fprintf(stderr, "Invalid tone frequency: %s (min: 1hz)\n", argv[i]); tone_freq = 1; } } else if(i+1 < argc && (strcmp(argv[i], "--srate") == 0 || strcmp(argv[i], "-s") == 0)) { i++; srate = atoi(argv[i]); if(srate < 40) { fprintf(stderr, "Invalid sample rate: %s (min: 40hz)\n", argv[i]); srate = 40; } } } InitAL(); if(!alIsExtensionPresent("AL_EXT_FLOAT32")) { fprintf(stderr, "Required AL_EXT_FLOAT32 extension not supported on this device!\n"); CloseAL(); return 1; } { ALCdevice *device = alcGetContextsDevice(alcGetCurrentContext()); alcGetIntegerv(device, ALC_FREQUENCY, 1, &dev_rate); assert(alcGetError(device)==ALC_NO_ERROR && "Failed to get device sample rate"); } if(srate < 0) srate = dev_rate; /* Load the sound into a buffer. */ buffer = CreateWave(wavetype, tone_freq, srate); if(!buffer) { CloseAL(); return 1; } printf("Playing %dhz %s-wave tone with %dhz sample rate and %dhz output, for %d second%s...\n", tone_freq, GetWaveTypeName(wavetype), srate, dev_rate, max_loops+1, max_loops?"s":""); fflush(stdout); /* Create the source to play the sound with. */ source = 0; alGenSources(1, &source); alSourcei(source, AL_BUFFER, buffer); assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source"); /* Play the sound for a while. */ num_loops = 0; last_pos = 0; alSourcei(source, AL_LOOPING, (max_loops > 0) ? AL_TRUE : AL_FALSE); alSourcePlay(source); do { ALint pos; al_nssleep(10000000); alGetSourcei(source, AL_SAMPLE_OFFSET, &pos); alGetSourcei(source, AL_SOURCE_STATE, &state); if(pos < last_pos && state == AL_PLAYING) { ++num_loops; if(num_loops >= max_loops) alSourcei(source, AL_LOOPING, AL_FALSE); printf("%d...\n", max_loops - num_loops + 1); fflush(stdout); } last_pos = pos; } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING); /* All done. Delete resources, and close OpenAL. */ alDeleteSources(1, &source); alDeleteBuffers(1, &buffer); /* Close up OpenAL. */ CloseAL(); return 0; }
int main(int argc, char **argv) { ALCdevice *device; ALuint source, buffer; const char *soundname; const char *hrtfname; ALCint hrtf_state; ALCint num_hrtf; ALdouble angle; ALenum state; /* Print out usage if no file was specified */ if(argc < 2 || (strcmp(argv[1], "-hrtf") == 0 && argc < 4)) { fprintf(stderr, "Usage: %s [-hrtf <name>] <soundfile>\n", argv[0]); return 1; } /* Initialize OpenAL with the default device, and check for HRTF support. */ if(InitAL() != 0) return 1; if(strcmp(argv[1], "-hrtf") == 0) { hrtfname = argv[2]; soundname = argv[3]; } else { hrtfname = NULL; soundname = argv[1]; } device = alcGetContextsDevice(alcGetCurrentContext()); if(!alcIsExtensionPresent(device, "ALC_SOFT_HRTF")) { fprintf(stderr, "Error: ALC_SOFT_HRTF not supported\n"); CloseAL(); return 1; } /* Define a macro to help load the function pointers. */ #define LOAD_PROC(d, x) ((x) = alcGetProcAddress((d), #x)) LOAD_PROC(device, alcGetStringiSOFT); LOAD_PROC(device, alcResetDeviceSOFT); #undef LOAD_PROC /* Enumerate available HRTFs, and reset the device using one. */ alcGetIntegerv(device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtf); if(!num_hrtf) printf("No HRTFs found\n"); else { ALCint attr[5]; ALCint index = -1; ALCint i; printf("Available HRTFs:\n"); for(i = 0; i < num_hrtf; i++) { const ALCchar *name = alcGetStringiSOFT(device, ALC_HRTF_SPECIFIER_SOFT, i); printf(" %d: %s\n", i, name); /* Check if this is the HRTF the user requested. */ if(hrtfname && strcmp(name, hrtfname) == 0) index = i; } i = 0; attr[i++] = ALC_HRTF_SOFT; attr[i++] = ALC_TRUE; if(index == -1) { if(hrtfname) printf("HRTF \"%s\" not found\n", hrtfname); printf("Using default HRTF...\n"); } else { printf("Selecting HRTF %d...\n", index); attr[i++] = ALC_HRTF_ID_SOFT; attr[i++] = index; } attr[i] = 0; if(!alcResetDeviceSOFT(device, attr)) printf("Failed to reset device: %s\n", alcGetString(device, alcGetError(device))); } /* Check if HRTF is enabled, and show which is being used. */ alcGetIntegerv(device, ALC_HRTF_SOFT, 1, &hrtf_state); if(!hrtf_state) printf("HRTF not enabled!\n"); else { const ALchar *name = alcGetString(device, ALC_HRTF_SPECIFIER_SOFT); printf("HRTF enabled, using %s\n", name); } fflush(stdout); /* Load the sound into a buffer. */ buffer = LoadSound(soundname); if(!buffer) { CloseAL(); return 1; } /* Create the source to play the sound with. */ source = 0; alGenSources(1, &source); alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE); alSource3f(source, AL_POSITION, 0.0f, 0.0f, -1.0f); alSourcei(source, AL_BUFFER, buffer); assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source"); /* Play the sound until it finishes. */ angle = 0.0; alSourcePlay(source); do { al_nssleep(10000000); /* Rotate the source around the listener by about 1/4 cycle per second. * Only affects mono sounds. */ angle += 0.01 * M_PI * 0.5; alSource3f(source, AL_POSITION, (ALfloat)sin(angle), 0.0f, -(ALfloat)cos(angle)); alGetSourcei(source, AL_SOURCE_STATE, &state); } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING); /* All done. Delete resources, and close OpenAL. */ alDeleteSources(1, &source); alDeleteBuffers(1, &buffer); CloseAL(); return 0; }
int main(int argc, char **argv) { ALuint source, buffer; ALdouble offsets[2]; ALenum state; /* Print out usage if no arguments were specified */ if(argc < 2) { fprintf(stderr, "Usage: %s [-device <name>] <filename>\n", argv[0]); return 1; } /* Initialize OpenAL, and check for source_latency support. */ argv++; argc--; if(InitAL(&argv, &argc) != 0) return 1; if(!alIsExtensionPresent("AL_SOFT_source_latency")) { fprintf(stderr, "Error: AL_SOFT_source_latency not supported\n"); CloseAL(); return 1; } /* Define a macro to help load the function pointers. */ #define LOAD_PROC(x) ((x) = alGetProcAddress(#x)) LOAD_PROC(alSourcedSOFT); LOAD_PROC(alSource3dSOFT); LOAD_PROC(alSourcedvSOFT); LOAD_PROC(alGetSourcedSOFT); LOAD_PROC(alGetSource3dSOFT); LOAD_PROC(alGetSourcedvSOFT); LOAD_PROC(alSourcei64SOFT); LOAD_PROC(alSource3i64SOFT); LOAD_PROC(alSourcei64vSOFT); LOAD_PROC(alGetSourcei64SOFT); LOAD_PROC(alGetSource3i64SOFT); LOAD_PROC(alGetSourcei64vSOFT); if(alIsExtensionPresent("AL_SOFT_buffer_samples")) { LOAD_PROC(alBufferSamplesSOFT); LOAD_PROC(alIsBufferFormatSupportedSOFT); } #undef LOAD_PROC /* Load the sound into a buffer. */ buffer = LoadSound(argv[0]); if(!buffer) { CloseAL(); return 1; } /* Create the source to play the sound with. */ source = 0; alGenSources(1, &source); alSourcei(source, AL_BUFFER, buffer); assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source"); /* Play the sound until it finishes. */ alSourcePlay(source); do { al_nssleep(10000000); alGetSourcei(source, AL_SOURCE_STATE, &state); /* Get the source offset and latency. AL_SEC_OFFSET_LATENCY_SOFT will * place the offset (in seconds) in offsets[0], and the time until that * offset will be heard (in seconds) in offsets[1]. */ alGetSourcedvSOFT(source, AL_SEC_OFFSET_LATENCY_SOFT, offsets); printf("\rOffset: %f - Latency:%3u ms ", offsets[0], (ALuint)(offsets[1]*1000)); fflush(stdout); } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING); printf("\n"); /* All done. Delete resources, and close OpenAL. */ alDeleteSources(1, &source); alDeleteBuffers(1, &buffer); CloseAL(); return 0; }