aboutsummaryrefslogtreecommitdiff
path: root/test/load wav.cpp
blob: 4cd27567f4183edb49fa77dc6f908ecd8dc66a4e (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
61
62
63
64
65
#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 WAV
{
    int samples;
    int channel, samplerate;
    short* data;
    int pos;
};

WAV wav;
static void callbackfunc(void *userdata, Uint8 *stream, int len)
{
    if (wav.pos > wav.samples)
        wav.pos = 0;
    int16_t* buffer = (int16_t*)stream;
    int l = len / 2;
    for (int i = 0; i < l; ++i)
    {
        buffer[i] = wav.data[wav.pos + i];
    }
    wav.pos += l;
}
int length = 2226052;
char buf[2226052];

int main(int argc, char* argv[])
{
    Audio* audio = SDLAudio::get();
    ifstream fs;
    fs.open("a.wav", ios::binary);
    fs.read(buf, length);
    SDLAudioSetting setting;
    SDL_AudioSpec spe;
    setting.callback = callbackfunc;
    wav_t wavconfig;
    if (wav_read(&wavconfig, buf, length) != 0)
    {
        cout << "load wav failed\n";
    }
    wav.channel = wavconfig.channels;
    wav.data = (short*)wavconfig.data;
    wav.pos = 0;
    wav.samplerate = wavconfig.samplerate;
    wav.samples = wavconfig.length;
    audio->init(&setting);
    while (true)
    {
        SDL_Delay(100);
    }
    audio->quit();
    return 0;
}

#endif