/
Main.cpp
306 lines (270 loc) · 8.99 KB
/
Main.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright (C) 2008-2013 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
// Waveform.vis
// A simple visualisation example by MrC
#include "addons/include/xbmc_vis_dll.h"
#include <stdio.h>
#ifdef HAS_SDL_OPENGL
#include <GL/glew.h>
#else
#ifdef _WIN32
#include <D3D9.h>
#endif
#endif
char g_visName[512];
#ifndef HAS_SDL_OPENGL
LPDIRECT3DDEVICE9 g_device;
#else
void* g_device;
#endif
float g_fWaveform[2][512];
#ifdef HAS_SDL_OPENGL
typedef struct {
int X;
int Y;
int Width;
int Height;
int MinZ;
int MaxZ;
} D3DVIEWPORT9;
typedef unsigned long D3DCOLOR;
#endif
D3DVIEWPORT9 g_viewport;
struct Vertex_t
{
float x, y, z, w;
D3DCOLOR col;
};
#ifndef HAS_SDL_OPENGL
#define VERTEX_FORMAT (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
#endif
//-- Create -------------------------------------------------------------------
// Called on load. Addon should fully initalize or return error status
//-----------------------------------------------------------------------------
ADDON_STATUS ADDON_Create(void* hdl, void* props)
{
if (!props)
return ADDON_STATUS_UNKNOWN;
VIS_PROPS* visProps = (VIS_PROPS*)props;
#ifndef HAS_SDL_OPENGL
g_device = (LPDIRECT3DDEVICE9)visProps->device;
#else
g_device = visProps->device;
#endif
g_viewport.X = visProps->x;
g_viewport.Y = visProps->y;
g_viewport.Width = visProps->width;
g_viewport.Height = visProps->height;
g_viewport.MinZ = 0;
g_viewport.MaxZ = 1;
return ADDON_STATUS_OK;
}
//-- Start --------------------------------------------------------------------
// Called when a new soundtrack is played
//-----------------------------------------------------------------------------
extern "C" void Start(int iChannels, int iSamplesPerSec, int iBitsPerSample, const char* szSongName)
{
//printf("Got Start Command\n");
}
//-- Audiodata ----------------------------------------------------------------
// Called by XBMC to pass new audio data to the vis
//-----------------------------------------------------------------------------
extern "C" void AudioData(const float* pAudioData, int iAudioDataLength, float *pFreqData, int iFreqDataLength)
{
int ipos=0;
while (ipos < 512)
{
for (int i=0; i < iAudioDataLength; i+=2)
{
g_fWaveform[0][ipos] = pAudioData[i ]; // left channel
g_fWaveform[1][ipos] = pAudioData[i+1]; // right channel
ipos++;
if (ipos >= 512) break;
}
}
}
//-- Render -------------------------------------------------------------------
// Called once per frame. Do all rendering here.
//-----------------------------------------------------------------------------
extern "C" void Render()
{
Vertex_t verts[512];
#ifndef HAS_SDL_OPENGL
g_device->SetFVF(VERTEX_FORMAT);
g_device->SetPixelShader(NULL);
#endif
// Left channel
#ifdef HAS_SDL_OPENGL
GLenum errcode;
glColor3f(1.0, 1.0, 1.0);
glDisable(GL_BLEND);
glPushMatrix();
glTranslatef(0,0,-1.0);
glBegin(GL_LINE_STRIP);
#endif
for (int i = 0; i < 256; i++)
{
verts[i].col = 0xffffffff;
verts[i].x = g_viewport.X + ((i / 255.0f) * g_viewport.Width);
verts[i].y = g_viewport.Y + g_viewport.Height * 0.33f + (g_fWaveform[0][i] * g_viewport.Height * 0.15f);
verts[i].z = 1.0;
verts[i].w = 1;
#ifdef HAS_SDL_OPENGL
glVertex2f(verts[i].x, verts[i].y);
#endif
}
#ifdef HAS_SDL_OPENGL
glEnd();
if ((errcode=glGetError())!=GL_NO_ERROR) {
printf("Houston, we have a GL problem: %s\n", gluErrorString(errcode));
}
#elif !defined(HAS_SDL_OPENGL)
g_device->DrawPrimitiveUP(D3DPT_LINESTRIP, 255, verts, sizeof(Vertex_t));
#endif
// Right channel
#ifdef HAS_SDL_OPENGL
glBegin(GL_LINE_STRIP);
#endif
for (int i = 0; i < 256; i++)
{
verts[i].col = 0xffffffff;
verts[i].x = g_viewport.X + ((i / 255.0f) * g_viewport.Width);
verts[i].y = g_viewport.Y + g_viewport.Height * 0.66f + (g_fWaveform[1][i] * g_viewport.Height * 0.15f);
verts[i].z = 1.0;
verts[i].w = 1;
#ifdef HAS_SDL_OPENGL
glVertex2f(verts[i].x, verts[i].y);
#endif
}
#ifdef HAS_SDL_OPENGL
glEnd();
glEnable(GL_BLEND);
glPopMatrix();
if ((errcode=glGetError())!=GL_NO_ERROR) {
printf("Houston, we have a GL problem: %s\n", gluErrorString(errcode));
}
#elif !defined(HAS_SDL_OPENGL)
g_device->DrawPrimitiveUP(D3DPT_LINESTRIP, 255, verts, sizeof(Vertex_t));
#endif
}
//-- GetInfo ------------------------------------------------------------------
// Tell XBMC our requirements
//-----------------------------------------------------------------------------
extern "C" void GetInfo(VIS_INFO* pInfo)
{
pInfo->bWantsFreq = false;
pInfo->iSyncDelay = 0;
}
//-- OnAction -----------------------------------------------------------------
// Handle XBMC actions such as next preset, lock preset, album art changed etc
//-----------------------------------------------------------------------------
extern "C" bool OnAction(long flags, const void *param)
{
bool ret = false;
return ret;
}
//-- GetPresets ---------------------------------------------------------------
// Return a list of presets to XBMC for display
//-----------------------------------------------------------------------------
extern "C" unsigned int GetPresets(char ***presets)
{
return 0;
}
//-- GetPreset ----------------------------------------------------------------
// Return the index of the current playing preset
//-----------------------------------------------------------------------------
extern "C" unsigned GetPreset()
{
return 0;
}
//-- IsLocked -----------------------------------------------------------------
// Returns true if this add-on use settings
//-----------------------------------------------------------------------------
extern "C" bool IsLocked()
{
return false;
}
//-- GetSubModules ------------------------------------------------------------
// Return any sub modules supported by this vis
//-----------------------------------------------------------------------------
extern "C" unsigned int GetSubModules(char ***names)
{
return 0; // this vis supports 0 sub modules
}
//-- Stop ---------------------------------------------------------------------
// This dll must stop all runtime activities
// !!! Add-on master function !!!
//-----------------------------------------------------------------------------
extern "C" void ADDON_Stop()
{
}
//-- Detroy -------------------------------------------------------------------
// Do everything before unload of this add-on
// !!! Add-on master function !!!
//-----------------------------------------------------------------------------
extern "C" void ADDON_Destroy()
{
}
//-- HasSettings --------------------------------------------------------------
// Returns true if this add-on use settings
// !!! Add-on master function !!!
//-----------------------------------------------------------------------------
extern "C" bool ADDON_HasSettings()
{
return false;
}
//-- GetStatus ---------------------------------------------------------------
// Returns the current Status of this visualisation
// !!! Add-on master function !!!
//-----------------------------------------------------------------------------
extern "C" ADDON_STATUS ADDON_GetStatus()
{
return ADDON_STATUS_OK;
}
//-- GetSettings --------------------------------------------------------------
// Return the settings for XBMC to display
// !!! Add-on master function !!!
//-----------------------------------------------------------------------------
extern "C" unsigned int ADDON_GetSettings(ADDON_StructSetting ***sSet)
{
return 0;
}
//-- FreeSettings --------------------------------------------------------------
// Free the settings struct passed from XBMC
// !!! Add-on master function !!!
//-----------------------------------------------------------------------------
extern "C" void ADDON_FreeSettings()
{
}
//-- SetSetting ---------------------------------------------------------------
// Set a specific Setting value (called from XBMC)
// !!! Add-on master function !!!
//-----------------------------------------------------------------------------
extern "C" ADDON_STATUS ADDON_SetSetting(const char *strSetting, const void* value)
{
return ADDON_STATUS_OK;
}
//-- Announce -----------------------------------------------------------------
// Receive announcements from XBMC
// !!! Add-on master function !!!
//-----------------------------------------------------------------------------
extern "C" void ADDON_Announce(const char *flag, const char *sender, const char *message, const void *data)
{
}