Kodi Development 22.0
for Binary and Script based Add-Ons
 
Loading...
Searching...
No Matches
Visualization.h
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#pragma once
10
11#include "../AddonBase.h"
12#include "../c-api/addon-instance/visualization.h"
13#include "../gui/renderHelper.h"
14
15#ifdef __cplusplus
16namespace kodi
17{
18namespace addon
19{
20
21//==============================================================================
39{
41 friend class CInstanceVisualization;
44public:
46 VisualizationTrack() = default;
47 VisualizationTrack(const VisualizationTrack& tag) { *this = tag; }
48
49 VisualizationTrack& operator=(const VisualizationTrack& right)
50 {
51 if (&right == this)
52 return *this;
53
54 m_title = right.m_title;
55 m_artist = right.m_artist;
56 m_album = right.m_album;
57 m_albumArtist = right.m_albumArtist;
58 m_genre = right.m_genre;
59 m_comment = right.m_comment;
60 m_lyrics = right.m_lyrics;
61
62 m_trackNumber = right.m_trackNumber;
63 m_discNumber = right.m_discNumber;
64 m_duration = right.m_duration;
65 m_year = right.m_year;
66 m_rating = right.m_rating;
67 return *this;
68 }
90
93
95 void SetTitle(const std::string& title) { m_title = title; }
96
98 const std::string& GetTitle() const { return m_title; }
99
101 void SetArtist(const std::string& artist) { m_artist = artist; }
102
104 const std::string& GetArtist() const { return m_artist; }
105
107 void SetAlbum(const std::string& album) { m_album = album; }
108
110 const std::string& GetAlbum() const { return m_album; }
111
113 void SetAlbumArtist(const std::string& albumArtist) { m_albumArtist = albumArtist; }
114
116 const std::string& GetAlbumArtist() const { return m_albumArtist; }
117
119 void SetGenre(const std::string& genre) { m_genre = genre; }
120
122 const std::string& GetGenre() const { return m_genre; }
123
125 void SetDuration(int duration) { m_duration = duration; }
126
128 int GetDuration() const { return m_duration; }
129
131 void SetTrack(int trackNumber) { m_trackNumber = trackNumber; }
132
134 int GetTrack() const { return m_trackNumber; }
135
137 void SetDisc(int discNumber) { m_discNumber = discNumber; }
138
140 int GetDisc() const { return m_discNumber; }
141
143 void SetYear(int year) { m_year = year; }
144
146 int GetYear() const { return m_year; }
147
149 void SetLyrics(const std::string& lyrics) { m_lyrics = lyrics; }
150
152 const std::string& GetLyrics() const { return m_lyrics; }
153
155 void SetRating(int rating) { m_rating = rating; }
156
158 int GetRating() const { return m_rating; }
159
161 void SetComment(const std::string& comment) { m_comment = comment; }
162
164 const std::string& GetComment() const { return m_comment; }
165
167
168private:
170 {
171 if (!tag)
172 return;
173
174 m_title = tag->title ? tag->title : "";
175 m_artist = tag->artist ? tag->artist : "";
176 m_album = tag->album ? tag->album : "";
177 m_albumArtist = tag->albumArtist ? tag->albumArtist : "";
178 m_genre = tag->genre ? tag->genre : "";
179 m_comment = tag->comment ? tag->comment : "";
180 m_lyrics = tag->lyrics ? tag->lyrics : "";
181
182 m_trackNumber = tag->trackNumber;
183 m_discNumber = tag->discNumber;
184 m_duration = tag->duration;
185 m_year = tag->year;
186 m_rating = tag->rating;
187 }
188
189 std::string m_title;
190 std::string m_artist;
191 std::string m_album;
192 std::string m_albumArtist;
193 std::string m_genre;
194 std::string m_comment;
195 std::string m_lyrics;
196
197 int m_trackNumber = 0;
198 int m_discNumber = 0;
199 int m_duration = 0;
200 int m_year = 0;
201 int m_rating = 0;
202};
204//------------------------------------------------------------------------------
205
206//==============================================================================
214
215//==============================================================================
385class ATTR_DLL_LOCAL CInstanceVisualization : public IAddonInstance
386{
387public:
388 //============================================================================
396 : IAddonInstance(IInstanceInfo(CPrivateBase::m_interface->firstKodiInstance))
397 {
398 if (CPrivateBase::m_interface->globalSingleInstance != nullptr)
399 throw std::logic_error(
400 "kodi::addon::CInstanceVisualization: Cannot create multiple instances of add-on.");
401
402 SetAddonStruct(CPrivateBase::m_interface->firstKodiInstance);
403 CPrivateBase::m_interface->globalSingleInstance = this;
404 }
405 //----------------------------------------------------------------------------
406
407 //==========================================================================
445 explicit CInstanceVisualization(const IInstanceInfo& instance) : IAddonInstance(instance)
446 {
447 if (CPrivateBase::m_interface->globalSingleInstance != nullptr)
448 throw std::logic_error("kodi::addon::CInstanceVisualization: Creation of multiple together "
449 "with single instance way is not allowed!");
450
451 SetAddonStruct(instance);
452 }
453 //----------------------------------------------------------------------------
454
455 //============================================================================
459 ~CInstanceVisualization() override = default;
460 //----------------------------------------------------------------------------
461
462 //============================================================================
472 virtual bool Start(int channels,
473 int samplesPerSec,
474 int bitsPerSample,
475 const std::string& songName)
476 {
477 return true;
478 }
479 //----------------------------------------------------------------------------
480
481 //============================================================================
486 virtual void Stop() {}
487 //----------------------------------------------------------------------------
488
489 //============================================================================
496 virtual void AudioData(const float* audioData, size_t audioDataLength) {}
497 //----------------------------------------------------------------------------
498
499 //============================================================================
506 virtual bool IsDirty() { return true; }
507 //----------------------------------------------------------------------------
508
509 //============================================================================
513 virtual void Render() {}
514 //----------------------------------------------------------------------------
515
516 //============================================================================
524 virtual int GetSyncDelay() { return 0; }
525 //----------------------------------------------------------------------------
526
527 //============================================================================
537 virtual bool GetPresets(std::vector<std::string>& presets) { return false; }
538 //----------------------------------------------------------------------------
539
540 //============================================================================
546 virtual int GetActivePreset() { return -1; }
547 //----------------------------------------------------------------------------
548
549 //============================================================================
555 virtual bool IsLocked() { return false; }
556 //----------------------------------------------------------------------------
557
558 //============================================================================
564 virtual bool PrevPreset() { return false; }
565 //----------------------------------------------------------------------------
566
567 //============================================================================
573 virtual bool NextPreset() { return false; }
574 //----------------------------------------------------------------------------
575
576 //============================================================================
585 virtual bool LoadPreset(int select) { return false; }
586 //----------------------------------------------------------------------------
587
588 //============================================================================
594 virtual bool RandomPreset() { return false; }
595 //----------------------------------------------------------------------------
596
597 //============================================================================
604 virtual bool LockPreset(bool lockUnlock) { return false; }
605 //----------------------------------------------------------------------------
606
607 //============================================================================
615 virtual bool RatePreset(bool plusMinus) { return false; }
616 //----------------------------------------------------------------------------
617
618 //============================================================================
625 virtual bool UpdateAlbumart(const std::string& albumart) { return false; }
626 //----------------------------------------------------------------------------
627
628 //============================================================================
665 virtual bool UpdateTrack(const kodi::addon::VisualizationTrack& track) { return false; }
666 //----------------------------------------------------------------------------
667
668 //============================================================================
675
676 //============================================================================
687 inline void TransferPresets(const std::vector<std::string>& presets)
688 {
689 m_instanceData->visualization->toKodi->clear_presets(m_instanceData->info->kodi);
690 for (const auto& it : presets)
691 m_instanceData->visualization->toKodi->transfer_preset(m_instanceData->info->kodi,
692 it.c_str());
693 }
694 //----------------------------------------------------------------------------
695
696 //============================================================================
717 inline kodi::HardwareContext Device() { return m_props.device; }
718 //----------------------------------------------------------------------------
719
720 //============================================================================
726 inline int X() { return m_props.x; }
727 //----------------------------------------------------------------------------
728
729 //============================================================================
735 inline int Y() { return m_props.y; }
736 //----------------------------------------------------------------------------
737
738 //============================================================================
744 inline int Width() { return m_props.width; }
745 //----------------------------------------------------------------------------
746
747 //============================================================================
753 inline int Height() { return m_props.height; }
754 //----------------------------------------------------------------------------
755
756 //============================================================================
763 inline float PixelRatio() { return m_props.pixelRatio; }
764 //----------------------------------------------------------------------------
765
767
768private:
769 void SetAddonStruct(KODI_ADDON_INSTANCE_STRUCT* instance)
770 {
771 m_instanceData = instance;
772 m_instanceData->hdl = this;
773 m_instanceData->visualization->toAddon->start = ADDON_start;
774 m_instanceData->visualization->toAddon->stop = ADDON_stop;
775 m_instanceData->visualization->toAddon->audio_data = ADDON_audio_data;
776 m_instanceData->visualization->toAddon->is_dirty = ADDON_is_dirty;
777 m_instanceData->visualization->toAddon->render = ADDON_render;
778 m_instanceData->visualization->toAddon->get_sync_delay = ADDON_get_sync_delay;
779 m_instanceData->visualization->toAddon->prev_preset = ADDON_prev_preset;
780 m_instanceData->visualization->toAddon->next_preset = ADDON_next_preset;
781 m_instanceData->visualization->toAddon->load_preset = ADDON_load_preset;
782 m_instanceData->visualization->toAddon->random_preset = ADDON_random_preset;
783 m_instanceData->visualization->toAddon->lock_preset = ADDON_lock_preset;
784 m_instanceData->visualization->toAddon->rate_preset = ADDON_rate_preset;
785 m_instanceData->visualization->toAddon->update_albumart = ADDON_update_albumart;
786 m_instanceData->visualization->toAddon->update_track = ADDON_update_track;
787 m_instanceData->visualization->toAddon->get_presets = ADDON_get_presets;
788 m_instanceData->visualization->toAddon->get_active_preset = ADDON_get_active_preset;
789 m_instanceData->visualization->toAddon->is_locked = ADDON_is_locked;
790
791 m_instanceData->visualization->toKodi->get_properties(instance->info->kodi, &m_props);
792 }
793
794 inline static bool ADDON_start(const KODI_ADDON_VISUALIZATION_HDL hdl,
795 int channels,
796 int samplesPerSec,
797 int bitsPerSample,
798 const char* songName)
799 {
800 CInstanceVisualization* thisClass = static_cast<CInstanceVisualization*>(hdl);
801 thisClass->m_renderHelper = kodi::gui::GetRenderHelper();
802 return thisClass->Start(channels, samplesPerSec, bitsPerSample, songName);
803 }
804
805 inline static void ADDON_stop(const KODI_ADDON_VISUALIZATION_HDL hdl)
806 {
807 CInstanceVisualization* thisClass = static_cast<CInstanceVisualization*>(hdl);
808 thisClass->Stop();
809 thisClass->m_renderHelper = nullptr;
810 }
811
812 inline static void ADDON_audio_data(const KODI_ADDON_VISUALIZATION_HDL hdl,
813 const float* audioData,
814 size_t audioDataLength)
815 {
816 static_cast<CInstanceVisualization*>(hdl)->AudioData(audioData, audioDataLength);
817 }
818
819 inline static bool ADDON_is_dirty(const KODI_ADDON_VISUALIZATION_HDL hdl)
820 {
821 return static_cast<CInstanceVisualization*>(hdl)->IsDirty();
822 }
823
824 inline static void ADDON_render(const KODI_ADDON_VISUALIZATION_HDL hdl)
825 {
826 CInstanceVisualization* thisClass = static_cast<CInstanceVisualization*>(hdl);
827 if (!thisClass->m_renderHelper)
828 return;
829 thisClass->m_renderHelper->Begin();
830 thisClass->Render();
831 thisClass->m_renderHelper->End();
832 }
833
834 inline static int ADDON_get_sync_delay(const KODI_ADDON_VISUALIZATION_HDL hdl)
835 {
836 return static_cast<CInstanceVisualization*>(hdl)->GetSyncDelay();
837 }
838
839 inline static unsigned int ADDON_get_presets(const KODI_ADDON_VISUALIZATION_HDL hdl)
840 {
841 CInstanceVisualization* thisClass = static_cast<CInstanceVisualization*>(hdl);
842 std::vector<std::string> presets;
843 if (thisClass->GetPresets(presets))
844 {
845 for (const auto& it : presets)
846 thisClass->m_instanceData->visualization->toKodi->transfer_preset(
847 thisClass->m_instanceData->info->kodi, it.c_str());
848 }
849
850 return static_cast<unsigned int>(presets.size());
851 }
852
853 inline static int ADDON_get_active_preset(const KODI_ADDON_VISUALIZATION_HDL hdl)
854 {
855 return static_cast<CInstanceVisualization*>(hdl)->GetActivePreset();
856 }
857
858 inline static bool ADDON_prev_preset(const KODI_ADDON_VISUALIZATION_HDL hdl)
859 {
860 return static_cast<CInstanceVisualization*>(hdl)->PrevPreset();
861 }
862
863 inline static bool ADDON_next_preset(const KODI_ADDON_VISUALIZATION_HDL hdl)
864 {
865 return static_cast<CInstanceVisualization*>(hdl)->NextPreset();
866 }
867
868 inline static bool ADDON_load_preset(const KODI_ADDON_VISUALIZATION_HDL hdl, int select)
869
870 {
871 return static_cast<CInstanceVisualization*>(hdl)->LoadPreset(select);
872 }
873
874 inline static bool ADDON_random_preset(const KODI_ADDON_VISUALIZATION_HDL hdl)
875 {
876 return static_cast<CInstanceVisualization*>(hdl)->RandomPreset();
877 }
878
879 inline static bool ADDON_lock_preset(const KODI_ADDON_VISUALIZATION_HDL hdl)
880 {
881 CInstanceVisualization* thisClass = static_cast<CInstanceVisualization*>(hdl);
882 thisClass->m_presetLockedByUser = !thisClass->m_presetLockedByUser;
883 return thisClass->LockPreset(thisClass->m_presetLockedByUser);
884 }
885
886 inline static bool ADDON_rate_preset(const KODI_ADDON_VISUALIZATION_HDL hdl, bool plus_minus)
887 {
888 return static_cast<CInstanceVisualization*>(hdl)->RatePreset(plus_minus);
889 }
890
891 inline static bool ADDON_is_locked(const KODI_ADDON_VISUALIZATION_HDL hdl)
892 {
893 return static_cast<CInstanceVisualization*>(hdl)->IsLocked();
894 }
895
896 inline static bool ADDON_update_albumart(const KODI_ADDON_VISUALIZATION_HDL hdl,
897 const char* albumart)
898 {
899 return static_cast<CInstanceVisualization*>(hdl)->UpdateAlbumart(albumart);
900 }
901
902 inline static bool ADDON_update_track(const KODI_ADDON_VISUALIZATION_HDL hdl,
904 {
905 VisualizationTrack cppTrack(track);
906 return static_cast<CInstanceVisualization*>(hdl)->UpdateTrack(cppTrack);
907 }
908
909 std::shared_ptr<kodi::gui::IRenderHelper> m_renderHelper;
910 bool m_presetLockedByUser = false;
911 KODI_ADDON_INSTANCE_STRUCT* m_instanceData{nullptr};
913};
914
915} /* namespace addon */
916} /* namespace kodi */
917#endif /* __cplusplus */
Definition Visualization.h:386
Definition AddonBase.h:565
Definition AddonBase.h:498
Definition Visualization.h:39
int Height()
Returns the height of the rendering window.
Definition Visualization.h:753
kodi::HardwareContext Device()
Device that represents the display adapter.
Definition Visualization.h:717
void TransferPresets(const std::vector< std::string > &presets)
To transfer available presets on addon.
Definition Visualization.h:687
int X()
Returns the X position of the rendering window.
Definition Visualization.h:726
int Width()
Returns the width of the rendering window.
Definition Visualization.h:744
int Y()
Returns the Y position of the rendering window.
Definition Visualization.h:735
float PixelRatio()
Pixel aspect ratio (often abbreviated PAR) is a ratio that describes how the width of a pixel compare...
Definition Visualization.h:763
int GetDisc() const
Get disk number (if present)
Definition Visualization.h:140
const std::string & GetAlbumArtist() const
Get album artist names, as a single string-.
Definition Visualization.h:116
void SetAlbum(const std::string &album)
Set Album that the current song is from.
Definition Visualization.h:107
void SetTrack(int trackNumber)
Set track number (if present) from music info as integer.
Definition Visualization.h:131
void SetComment(const std::string &comment)
Set additional information comment (if present).
Definition Visualization.h:161
void SetDuration(int duration)
Set the duration of music as integer from info.
Definition Visualization.h:125
int GetYear() const
Get year that the current song was released.
Definition Visualization.h:146
void SetLyrics(const std::string &lyrics)
Set string from lyrics.
Definition Visualization.h:149
int GetRating() const
Get the user-defined rating of the current song.
Definition Visualization.h:158
const std::string & GetAlbum() const
Get Album that the current song is from.
Definition Visualization.h:110
void SetTitle(const std::string &title)
Set title of the current song.
Definition Visualization.h:95
void SetArtist(const std::string &artist)
Set artist names, as a single string-.
Definition Visualization.h:101
void SetAlbumArtist(const std::string &albumArtist)
Set album artist names, as a single stringalbum artist name.
Definition Visualization.h:113
const std::string & GetComment() const
Get additional information comment (if present).
Definition Visualization.h:164
int GetDuration() const
Get the duration of music as integer from info.
Definition Visualization.h:128
const std::string & GetGenre() const
Get genre name from music as string if present.
Definition Visualization.h:122
void SetDisc(int discNumber)
Set disk number (if present) from music info as integer.
Definition Visualization.h:137
const std::string & GetLyrics() const
Get string from lyrics.
Definition Visualization.h:152
void SetRating(int rating)
Set the user-defined rating of the current song.
Definition Visualization.h:155
void SetYear(int year)
Set year that the current song was released.
Definition Visualization.h:143
const std::string & GetTitle() const
Get title of the current song.
Definition Visualization.h:98
int GetTrack() const
Get track number (if present).
Definition Visualization.h:134
void SetGenre(const std::string &genre)
Set genre name from music as string if present.
Definition Visualization.h:119
const std::string & GetArtist() const
Get artist names, as a single string-.
Definition Visualization.h:104
virtual bool LockPreset(bool lockUnlock)
Lock the current visualization preset, preventing it from changing.
Definition Visualization.h:604
virtual void Render()
Used to indicate when the add-on should render.
Definition Visualization.h:513
virtual bool RandomPreset()
Switch to a new random preset.
Definition Visualization.h:594
virtual bool LoadPreset(int select)
Load a visualization preset.
Definition Visualization.h:585
virtual bool IsDirty()
Used to inform Kodi that the rendered region is dirty and need an update.
Definition Visualization.h:506
virtual bool UpdateTrack(const kodi::addon::VisualizationTrack &track)
Inform the visualization of the current track's tag information.
Definition Visualization.h:665
virtual bool PrevPreset()
Load the previous visualization preset.
Definition Visualization.h:564
virtual bool GetPresets(std::vector< std::string > &presets)
Used to get a list of visualization presets the user can select. from.
Definition Visualization.h:537
virtual void AudioData(const float *audioData, size_t audioDataLength)
Pass audio data to the visualization.
Definition Visualization.h:496
virtual int GetActivePreset()
Get the index of the current preset.
Definition Visualization.h:546
~CInstanceVisualization() override=default
Destructor.
CInstanceVisualization()
Visualization class constructor.
Definition Visualization.h:395
virtual bool NextPreset()
Load the next visualization preset.
Definition Visualization.h:573
virtual bool Start(int channels, int samplesPerSec, int bitsPerSample, const std::string &songName)
Used to notify the visualization that a new song has been started.
Definition Visualization.h:472
virtual bool UpdateAlbumart(const std::string &albumart)
Inform the visualization of the current album art image.
Definition Visualization.h:625
virtual bool IsLocked()
Check if the add-on is locked to the current preset.
Definition Visualization.h:555
CInstanceVisualization(const IInstanceInfo &instance)
Visualization class constructor used to support multiple instance types.
Definition Visualization.h:445
virtual bool RatePreset(bool plusMinus)
Used to increase/decrease the visualization preset rating.
Definition Visualization.h:615
virtual void Stop()
Used to inform the visualization that the rendering control was stopped.
Definition Visualization.h:486
virtual int GetSyncDelay()
Used to get the number of buffers from the current visualization.
Definition Visualization.h:524
Definition addon_base.h:268
Definition visualization.h:45
Definition visualization.h:22
Internal used structure to have stored C API data above and available for everything below.
Definition AddonBase.h:73