Merge pull request #4324 from FernetMenta/wasapi
[vuplus_xbmc] / lib / vgmstream / XBMCVGM.cpp
1 /*
2  *      Copyright (C) 2008-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #ifndef _LINUX
22 #include <windows.h>
23 #else
24 #define __cdecl
25 #define __declspec(x)
26 #endif
27
28 extern "C"
29 {
30   #include "src/vgmstream.h"
31   
32   long __declspec(dllexport) DLL_Init()
33   {
34     return 1;
35   }
36
37   long __declspec(dllexport) DLL_LoadVGM(const char* szFileName, int* sampleRate, int* sampleSize, int* channels)
38   {
39     VGMSTREAM* result;
40
41     if ((result = init_vgmstream(szFileName)) == NULL)
42       return 0;
43
44     *sampleRate = result->sample_rate;
45     *sampleSize = 16;
46     *channels = result->channels;
47     return (long)result;
48   }
49
50   void __declspec(dllexport) DLL_FreeVGM(long vgm)
51   {
52     close_vgmstream((VGMSTREAM*)vgm);
53   }
54
55   int __declspec(dllexport) DLL_FillBuffer(long vgm, char* szBuffer, int iSize)
56   {
57     VGMSTREAM* song = (VGMSTREAM*)vgm;
58     render_vgmstream((sample*)szBuffer,iSize/(2*song->channels),(VGMSTREAM*)vgm);
59     
60     return iSize;
61   }
62
63   unsigned long __declspec(dllexport) DLL_Seek(long vgm, unsigned long timepos)
64   {
65     VGMSTREAM* song = (VGMSTREAM*)vgm;
66     int16_t* buffer = new int16_t[576*song->channels];
67     long samples_to_do = (long)timepos * song->sample_rate / 1000L;
68     if (samples_to_do < song->current_sample )
69        reset_vgmstream(song);
70     else
71       samples_to_do -= song->current_sample;
72       
73     while (samples_to_do > 0)
74     {
75       long l = samples_to_do>576?576:samples_to_do;
76       render_vgmstream(buffer,l,song);
77       samples_to_do -= l;
78     }
79     delete[] buffer;
80
81     return timepos;
82   }
83
84   unsigned long __declspec(dllexport) DLL_GetLength(long vgm)
85   {
86     VGMSTREAM* song = (VGMSTREAM*)vgm;
87
88     return song->num_samples/song->sample_rate*1000;
89   }
90 }