Skip to content

Instantly share code, notes, and snippets.

@wutipong
Created January 11, 2016 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wutipong/bab479593e91c258b8ba to your computer and use it in GitHub Desktop.
Save wutipong/bab479593e91c258b8ba to your computer and use it in GitHub Desktop.
Plays OGG in infinite loop with SDL_mixer
#include <iostream>
#include <SDL.h>
#include <SDL_mixer.h>
#include <vorbis/vorbisfile.h>
using namespace std;
int main(int, char **) {
SDL_Init(SDL_INIT_EVERYTHING);
atexit(SDL_Quit);
Mix_Init(MIX_INIT_FLAC | MIX_INIT_OGG);
atexit(Mix_Quit);
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS,
1024);
atexit(Mix_CloseAudio);
auto window = SDL_CreateWindow(
"Test",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800,
600,
SDL_WINDOW_ALLOW_HIGHDPI);
auto renderer = SDL_CreateRenderer(window, -1, 0);
OggVorbis_File vf;
ov_fopen("./songs/2.04. Heartbeat, Heartbreak.ogg", &vf);
Mix_HookMusic([](void *udata, Uint8 *stream, int len) {
OggVorbis_File *pvf = (OggVorbis_File *) udata;
const long int start_loop = 447151L;
const long int end_loop = 3410688L;
int bitstream;
int read_total = 0;
while (true) {
if (read_total >= len) break;
auto pos = ov_pcm_tell(pvf);
if (pos > end_loop)
ov_pcm_seek(pvf, start_loop);
int read = ov_read(pvf, (char *) stream + read_total,
len - read_total, 0, 2, 1, &bitstream);
read_total += read;
}
}, &vf);
while (true) {
SDL_Event event;
if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
break;
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(1);
}
ov_clear(&vf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment