forked from opdenkamp/xbmc
/
Visualisation.cpp
518 lines (455 loc) · 12.9 KB
/
Visualisation.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* Copyright (C) 2005-2013 Team XBMC
* http://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/>.
*
*/
#include "system.h"
#include "Visualisation.h"
#include "GUIInfoManager.h"
#include "guiinfo/GUIInfoLabels.h"
#include "Application.h"
#include "guilib/GraphicContext.h"
#include "guilib/WindowIDs.h"
#include "music/tags/MusicInfoTag.h"
#include "settings/AdvancedSettings.h"
#include "windowing/WindowingFactory.h"
#include "utils/URIUtils.h"
#include "utils/StringUtils.h"
#include "cores/AudioEngine/AEFactory.h"
#ifdef TARGET_POSIX
#include <dlfcn.h>
#include "filesystem/SpecialProtocol.h"
#endif
using namespace MUSIC_INFO;
using namespace ADDON;
CAudioBuffer::CAudioBuffer(int iSize)
{
m_iLen = iSize;
m_pBuffer = new float[iSize];
}
CAudioBuffer::~CAudioBuffer()
{
delete [] m_pBuffer;
}
const float* CAudioBuffer::Get() const
{
return m_pBuffer;
}
void CAudioBuffer::Set(const float* psBuffer, int iSize)
{
if (iSize<0)
return;
memcpy(m_pBuffer, psBuffer, iSize * sizeof(float));
for (int i = iSize; i < m_iLen; ++i) m_pBuffer[i] = 0;
}
bool CVisualisation::Create()
{
m_pInfo = new VIS_PROPS;
#ifdef HAS_DX
m_pInfo->device = g_Windowing.Get3D11Context();
#else
m_pInfo->device = NULL;
#endif
m_pInfo->name = strdup(Name().c_str());
m_pInfo->presets = strdup(CSpecialProtocol::TranslatePath(Path()).c_str());
m_pInfo->profile = strdup(CSpecialProtocol::TranslatePath(Profile()).c_str());
m_pInfo->submodule = NULL;
if (CAddonDll<DllVisualisation, Visualisation, VIS_PROPS>::Create() == ADDON_STATUS_OK)
{
m_hasPresets = GetPresets();
if (GetSubModules())
m_pInfo->submodule = strdup(CSpecialProtocol::TranslatePath(m_submodules.front()).c_str());
else
m_pInfo->submodule = NULL;
CreateBuffers();
return true;
}
return false;
}
bool CVisualisation::Create(int x, int y, int width, int height, float fPixelRatio)
{
std::string strFile = URIUtils::GetFileName(g_application.CurrentFile());
Start(x, y, width, height, fPixelRatio, m_iChannels,
m_iSamplesPerSec, m_iBitsPerSample, strFile);
}
void CVisualisation::Start(int x, int y, int width, int height, float pixelRatio,
int iChannels, int iSamplesPerSec, int iBitsPerSample, const std::string &strSongName)
{
// notify visz. that new song has been started
// pass it the nr of audio channels, sample rate, bits/sample and offcourse the songname
// and the screen info
// Start the visualisation
if (Initialized())
{
CAEFactory::RegisterAudioCallback(this);
try
{
m_pStruct->Start(x, y, width, height, pixelRatio,
iChannels, iSamplesPerSec, iBitsPerSample, strSongName.c_str());
}
catch (std::exception e)
{
HandleException(e, "m_pStruct->Start (CVisualisation::Start)");
}
}
}
void CVisualisation::AudioData(const float* pAudioData, int iAudioDataLength, float *pFreqData, int iFreqDataLength)
{
// pass audio data to visz.
// audio data: is short audiodata [channel][iAudioDataLength] containing the raw audio data
// iAudioDataLength = length of audiodata array
// pFreqData = fft-ed audio data
// iFreqDataLength = length of pFreqData
if (Initialized())
{
try
{
m_pStruct->AudioData(pAudioData, iAudioDataLength, pFreqData, iFreqDataLength);
}
catch (std::exception e)
{
HandleException(e, "m_pStruct->AudioData (CVisualisation::AudioData)");
}
}
}
void CVisualisation::Render()
{
// ask visz. to render itself
g_graphicsContext.BeginPaint();
if (Initialized())
{
try
{
m_pStruct->Render();
}
catch (std::exception e)
{
HandleException(e, "m_pStruct->Render (CVisualisation::Render)");
}
}
g_graphicsContext.EndPaint();
}
void CVisualisation::Stop()
{
CAEFactory::UnregisterAudioCallback(this);
if (Initialized())
{
CAddonDll<DllVisualisation, Visualisation, VIS_PROPS>::Stop();
}
}
void CVisualisation::GetInfo(VIS_INFO *info)
{
if (Initialized())
{
try
{
m_pStruct->GetInfo(info);
}
catch (std::exception e)
{
HandleException(e, "m_pStruct->GetInfo (CVisualisation::GetInfo)");
}
}
}
bool CVisualisation::OnAction(VIS_ACTION action, void *param)
{
if (!Initialized())
return false;
// see if vis wants to handle the input
// returns false if vis doesnt want the input
// returns true if vis handled the input
try
{
if (action != VIS_ACTION_NONE && m_pStruct->OnAction)
{
// if this is a VIS_ACTION_UPDATE_TRACK action, copy relevant
// tags from CMusicInfoTag to VisTag
if ( action == VIS_ACTION_UPDATE_TRACK && param )
{
const CMusicInfoTag* tag = (const CMusicInfoTag*)param;
std::string artist(StringUtils::Join(tag->GetArtist(), g_advancedSettings.m_musicItemSeparator));
std::string albumArtist(StringUtils::Join(tag->GetAlbumArtist(), g_advancedSettings.m_musicItemSeparator));
std::string genre(StringUtils::Join(tag->GetGenre(), g_advancedSettings.m_musicItemSeparator));
VisTrack track;
track.title = tag->GetTitle().c_str();
track.artist = artist.c_str();
track.album = tag->GetAlbum().c_str();
track.albumArtist = albumArtist.c_str();
track.genre = genre.c_str();
track.comment = tag->GetComment().c_str();
track.lyrics = tag->GetLyrics().c_str();
track.trackNumber = tag->GetTrackNumber();
track.discNumber = tag->GetDiscNumber();
track.duration = tag->GetDuration();
track.year = tag->GetYear();
track.rating = tag->GetRating();
return m_pStruct->OnAction(action, &track);
}
return m_pStruct->OnAction((int)action, param);
}
}
catch (std::exception e)
{
HandleException(e, "m_pStruct->OnAction (CVisualisation::OnAction)");
}
return false;
}
void CVisualisation::OnInitialize(int iChannels, int iSamplesPerSec, int iBitsPerSample)
{
if (!m_pStruct)
return ;
CLog::Log(LOGDEBUG, "OnInitialize() started");
m_iChannels = iChannels;
m_iSamplesPerSec = iSamplesPerSec;
m_iBitsPerSample = iBitsPerSample;
UpdateTrack();
CLog::Log(LOGDEBUG, "OnInitialize() done");
}
void CVisualisation::OnAudioData(const float* pAudioData, int iAudioDataLength)
{
if (!m_pStruct)
return ;
// FIXME: iAudioDataLength should never be less than 0
if (iAudioDataLength<0)
return;
// Save our audio data in the buffers
std::unique_ptr<CAudioBuffer> pBuffer ( new CAudioBuffer(iAudioDataLength) );
pBuffer->Set(pAudioData, iAudioDataLength);
m_vecBuffers.push_back( pBuffer.release() );
if ( (int)m_vecBuffers.size() < m_iNumBuffers) return ;
std::unique_ptr<CAudioBuffer> ptrAudioBuffer ( m_vecBuffers.front() );
m_vecBuffers.pop_front();
// Fourier transform the data if the vis wants it...
if (m_bWantsFreq)
{
const float *psAudioData = ptrAudioBuffer->Get();
if (!m_transform)
m_transform.reset(new RFFT(AUDIO_BUFFER_SIZE/2, false)); // half due to stereo
m_transform->calc(psAudioData, m_fFreq);
// Transfer data to our visualisation
AudioData(psAudioData, iAudioDataLength, m_fFreq, AUDIO_BUFFER_SIZE/2); // half due to complex-conjugate
}
else
{ // Transfer data to our visualisation
AudioData(ptrAudioBuffer->Get(), iAudioDataLength, NULL, 0);
}
return ;
}
void CVisualisation::CreateBuffers()
{
ClearBuffers();
// Get the number of buffers from the current vis
VIS_INFO info;
m_pStruct->GetInfo(&info);
m_iNumBuffers = info.iSyncDelay + 1;
m_bWantsFreq = (info.bWantsFreq != 0);
if (m_iNumBuffers > MAX_AUDIO_BUFFERS)
m_iNumBuffers = MAX_AUDIO_BUFFERS;
if (m_iNumBuffers < 1)
m_iNumBuffers = 1;
}
void CVisualisation::ClearBuffers()
{
m_bWantsFreq = false;
m_iNumBuffers = 0;
while (!m_vecBuffers.empty())
{
CAudioBuffer* pAudioBuffer = m_vecBuffers.front();
delete pAudioBuffer;
m_vecBuffers.pop_front();
}
for (int j = 0; j < AUDIO_BUFFER_SIZE; j++)
{
m_fFreq[j] = 0.0f;
}
}
bool CVisualisation::UpdateTrack()
{
bool handled = false;
if (Initialized())
{
// get the current album art filename
m_AlbumThumb = CSpecialProtocol::TranslatePath(g_infoManager.GetImage(MUSICPLAYER_COVER, WINDOW_INVALID));
// get the current track tag
const CMusicInfoTag* tag = g_infoManager.GetCurrentSongTag();
if (m_AlbumThumb == "DefaultAlbumCover.png")
m_AlbumThumb = "";
else
CLog::Log(LOGDEBUG,"Updating visualisation albumart: %s", m_AlbumThumb.c_str());
// inform the visualisation of the current album art
if (OnAction( VIS_ACTION_UPDATE_ALBUMART, (void*)( m_AlbumThumb.c_str() ) ) )
handled = true;
// inform the visualisation of the current track's tag information
if ( tag && OnAction( VIS_ACTION_UPDATE_TRACK, (void*)tag ) )
handled = true;
}
return handled;
}
bool CVisualisation::GetPresetList(std::vector<std::string> &vecpresets)
{
vecpresets = m_presets;
return !m_presets.empty();
}
bool CVisualisation::GetPresets()
{
m_presets.clear();
char **presets = NULL;
unsigned int entries = 0;
try
{
entries = m_pStruct->GetPresets(&presets);
}
catch (std::exception e)
{
HandleException(e, "m_pStruct->OnAction (CVisualisation::GetPresets)");
return false;
}
if (presets && entries > 0)
{
for (unsigned i=0; i < entries; i++)
{
if (presets[i])
{
m_presets.push_back(presets[i]);
}
}
}
return (!m_presets.empty());
}
bool CVisualisation::GetSubModuleList(std::vector<std::string> &vecmodules)
{
vecmodules = m_submodules;
return !m_submodules.empty();
}
bool CVisualisation::GetSubModules()
{
m_submodules.clear();
char **modules = NULL;
unsigned int entries = 0;
try
{
entries = m_pStruct->GetSubModules(&modules);
}
catch (...)
{
CLog::Log(LOGERROR, "Exception in Visualisation::GetSubModules()");
return false;
}
if (modules && entries > 0)
{
for (unsigned i=0; i < entries; i++)
{
if (modules[i])
{
m_submodules.push_back(modules[i]);
}
}
}
return (!m_submodules.empty());
}
std::string CVisualisation::GetFriendlyName(const std::string& strVisz,
const std::string& strSubModule)
{
// should be of the format "moduleName (visName)"
return strSubModule + " (" + strVisz + ")";
}
bool CVisualisation::IsLocked()
{
if (!m_presets.empty())
{
if (!m_pStruct)
return false;
return m_pStruct->IsLocked();
}
return false;
}
void CVisualisation::Destroy()
{
// Free what was allocated in method CVisualisation::Create
if (m_pInfo)
{
free((void *) m_pInfo->name);
free((void *) m_pInfo->presets);
free((void *) m_pInfo->profile);
free((void *) m_pInfo->submodule);
delete m_pInfo;
m_pInfo = NULL;
}
CAddonDll<DllVisualisation, Visualisation, VIS_PROPS>::Destroy();
}
unsigned CVisualisation::GetPreset()
{
unsigned index = 0;
try
{
index = m_pStruct->GetPreset();
}
catch(...)
{
return 0;
}
return index;
}
std::string CVisualisation::GetPresetName()
{
if (!m_presets.empty())
return m_presets[GetPreset()];
else
return "";
}
CVisualisationManager::CVisualisationManager()
{
}
CVisualisationManager& CVisualisationManager::GetInstance()
{
static CVisualisationManager instance;
return instance;
}
VisualisationPtr CVisualisationManager::GetAddon(const std::string& id, int slot)
{
if (m_addons.find(slot) == m_addons.end() ||
m_addons[slot].first->ID() != id)
{
AddonPtr addon;
CAddonMgr::GetInstance().GetAddon(id, addon, ADDON_VIZ);
if (addon)
m_addons[slot].first = std::static_pointer_cast<CVisualisation>(addon);
else
return VisualisationPtr();
m_addons[slot].first->Create();
}
m_addons[slot].second.Stop();
return m_addons[slot].first;
}
void CVisualisationManager::Release(int slot)
{
m_addons[slot].second.StartZero();
}
void CVisualisationManager::ClearOutIdle()
{
std::vector<int> remove;
for (auto& it : m_addons)
{
if (it.second.second.GetElapsedSeconds() > 30)
remove.push_back(it.first);
}
for (auto& i : remove)
{
CLog::Log(LOGDEBUG, "Visualization %s in slot %i has been IDLE for 30 secs, stopping..", m_addons[i].first->ID().c_str(), i);
m_addons.erase(i);
}
}