aboutsummaryrefslogtreecommitdiff
path: root/test/load ogg.cpp
blob: afa5b60682b7ba75ca783cd22e85c4de6b51bec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "utils.h"
#if UNITTEST

#include <iostream>
#include <stdio.h>
#include <fstream>
#include "../audio/sdl/source.h"
#include "../audio/sdl/audio.h"

using namespace jin::audio;
using namespace std; 

struct OGG
{
    int samples;
    int channel, samplerate;
    short* data;
    int pos;
};

OGG ogg;

static void callbackfunc(void *userdata, Uint8 *stream, int len)
{
    int16_t* buffer = (int16_t*)stream;
    int16_t* oggbuf = (int16_t*)ogg.data;
    oggbuf += ogg.pos;
    for (int i = 0; i < len / 2; ++i)
    {
        buffer[i] = oggbuf[i];
    }
    ogg.pos += len / 2;
}

int main(int argc, char* argv[])
{
    Audio* audio = SDLAudio::get();
    ifstream fs;
    fs.open("a.ogg", ios::binary);
    fs.seekg(0, ios::end);
    int size = fs.tellg();
    fs.seekg(0, ios::beg);
    char* buffer = new char[size];
    memset(buffer, 0, size);
    fs.read(buffer, size);
    ogg.samples = stb_vorbis_decode_memory((unsigned char*)buffer, size, &ogg.channel, &ogg.samplerate, &ogg.data);
    ogg.pos = 0;
    SDLAudioSetting setting;
    SDL_AudioSpec spe;
    setting.callback = callbackfunc;
    audio->init(&setting);
    while (true)
    {
        SDL_Delay(100);
    }
    audio->quit();
    return 0;
}

#endif