Kodi Development  20.0
for Binary and Script based Add-Ons
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
16 namespace kodi
17 {
18 namespace addon
19 {
20 
21 
22 //==============================================================================
40 {
42  friend class CInstanceVisualization;
45 public:
47  VisualizationTrack() = default;
49  {
50  *this = tag;
51  }
52 
53  VisualizationTrack& operator=(const VisualizationTrack& right)
54  {
55  if (&right == this)
56  return *this;
57 
58  m_title = right.m_title;
59  m_artist = right.m_artist;
60  m_album = right.m_album;
61  m_albumArtist = right.m_albumArtist;
62  m_genre = right.m_genre;
63  m_comment = right.m_comment;
64  m_lyrics = right.m_lyrics;
65 
66  m_trackNumber = right.m_trackNumber;
67  m_discNumber = right.m_discNumber;
68  m_duration = right.m_duration;
69  m_year = right.m_year;
70  m_rating = right.m_rating;
71  return *this;
72  }
94 
97 
99  void SetTitle(const std::string& title) { m_title = title; }
100 
102  const std::string& GetTitle() const { return m_title; }
103 
105  void SetArtist(const std::string& artist) { m_artist = artist; }
106 
108  const std::string& GetArtist() const { return m_artist; }
109 
111  void SetAlbum(const std::string& album) { m_album = album; }
112 
114  const std::string& GetAlbum() const { return m_album; }
115 
117  void SetAlbumArtist(const std::string& albumArtist) { m_albumArtist = albumArtist; }
118 
120  const std::string& GetAlbumArtist() const { return m_albumArtist; }
121 
123  void SetGenre(const std::string& genre) { m_genre = genre; }
124 
126  const std::string& GetGenre() const { return m_genre; }
127 
129  void SetDuration(int duration) { m_duration = duration; }
130 
132  int GetDuration() const { return m_duration; }
133 
135  void SetTrack(int trackNumber) { m_trackNumber = trackNumber; }
136 
138  int GetTrack() const { return m_trackNumber; }
139 
141  void SetDisc(int discNumber) { m_discNumber = discNumber; }
142 
144  int GetDisc() const { return m_discNumber; }
145 
147  void SetYear(int year) { m_year = year; }
148 
150  int GetYear() const { return m_year; }
151 
153  void SetLyrics(const std::string& lyrics) { m_lyrics = lyrics; }
154 
156  const std::string& GetLyrics() const { return m_lyrics; }
157 
159  void SetRating(int rating) { m_rating = rating; }
160 
162  int GetRating() const { return m_rating; }
163 
165  void SetComment(const std::string& comment) { m_comment = comment; }
166 
168  const std::string& GetComment() const { return m_comment; }
169 
171 
172 private:
173  VisualizationTrack(const VIS_TRACK* tag)
174  {
175  if (!tag)
176  return;
177 
178  m_title = tag->title ? tag->title : "";
179  m_artist = tag->artist ? tag->artist : "";
180  m_album = tag->album ? tag->album : "";
181  m_albumArtist = tag->albumArtist ? tag->albumArtist : "";
182  m_genre = tag->genre ? tag->genre : "";
183  m_comment = tag->comment ? tag->comment : "";
184  m_lyrics = tag->lyrics ? tag->lyrics : "";
185 
186  m_trackNumber = tag->trackNumber;
187  m_discNumber = tag->discNumber;
188  m_duration = tag->duration;
189  m_year = tag->year;
190  m_rating = tag->rating;
191  }
192 
193  std::string m_title;
194  std::string m_artist;
195  std::string m_album;
196  std::string m_albumArtist;
197  std::string m_genre;
198  std::string m_comment;
199  std::string m_lyrics;
200 
201  int m_trackNumber = 0;
202  int m_discNumber = 0;
203  int m_duration = 0;
204  int m_year = 0;
205  int m_rating = 0;
206 };
208 //------------------------------------------------------------------------------
209 
210 //==============================================================================
218 
219 //==============================================================================
395 class ATTRIBUTE_HIDDEN CInstanceVisualization : public IAddonInstance
396 {
397 public:
398  //============================================================================
407  {
408  if (CAddonBase::m_interface->globalSingleInstance != nullptr)
409  throw std::logic_error(
410  "kodi::addon::CInstanceVisualization: Cannot create multiple instances of add-on.");
411 
412  SetAddonStruct(CAddonBase::m_interface->firstKodiInstance);
413  CAddonBase::m_interface->globalSingleInstance = this;
414  }
415  //----------------------------------------------------------------------------
416 
417  //==========================================================================
458  explicit CInstanceVisualization(KODI_HANDLE instance, const std::string& kodiVersion = "")
460  !kodiVersion.empty() ? kodiVersion
462  {
463  if (CAddonBase::m_interface->globalSingleInstance != nullptr)
464  throw std::logic_error("kodi::addon::CInstanceVisualization: Creation of multiple together "
465  "with single instance way is not allowed!");
466 
467  SetAddonStruct(instance);
468  }
469  //----------------------------------------------------------------------------
470 
471  //============================================================================
475  ~CInstanceVisualization() override = default;
476  //----------------------------------------------------------------------------
477 
478  //============================================================================
488  virtual bool Start(int channels, int samplesPerSec, int bitsPerSample, std::string songName)
489  {
490  return true;
491  }
492  //----------------------------------------------------------------------------
493 
494  //============================================================================
499  virtual void Stop() {}
500  //----------------------------------------------------------------------------
501 
502  //============================================================================
516  virtual void AudioData(const float* audioData,
517  int audioDataLength,
518  float* freqData,
519  int freqDataLength)
520  {
521  }
522  //----------------------------------------------------------------------------
523 
524  //============================================================================
531  virtual bool IsDirty() { return true; }
532  //----------------------------------------------------------------------------
533 
534  //============================================================================
538  virtual void Render() {}
539  //----------------------------------------------------------------------------
540 
541  //============================================================================
554  virtual void GetInfo(bool& wantsFreq, int& syncDelay)
555  {
556  wantsFreq = false;
557  syncDelay = 0;
558  }
559  //----------------------------------------------------------------------------
560 
561  //============================================================================
571  virtual bool GetPresets(std::vector<std::string>& presets) { return false; }
572  //----------------------------------------------------------------------------
573 
574  //============================================================================
580  virtual int GetActivePreset() { return -1; }
581  //----------------------------------------------------------------------------
582 
583  //============================================================================
589  virtual bool IsLocked() { return false; }
590  //----------------------------------------------------------------------------
591 
592  //============================================================================
598  virtual bool PrevPreset() { return false; }
599  //----------------------------------------------------------------------------
600 
601  //============================================================================
607  virtual bool NextPreset() { return false; }
608  //----------------------------------------------------------------------------
609 
610  //============================================================================
619  virtual bool LoadPreset(int select) { return false; }
620  //----------------------------------------------------------------------------
621 
622  //============================================================================
628  virtual bool RandomPreset() { return false; }
629  //----------------------------------------------------------------------------
630 
631  //============================================================================
638  virtual bool LockPreset(bool lockUnlock) { return false; }
639  //----------------------------------------------------------------------------
640 
641  //============================================================================
649  virtual bool RatePreset(bool plusMinus) { return false; }
650  //----------------------------------------------------------------------------
651 
652  //============================================================================
659  virtual bool UpdateAlbumart(std::string albumart) { return false; }
660  //----------------------------------------------------------------------------
661 
662  //============================================================================
699  virtual bool UpdateTrack(const kodi::addon::VisualizationTrack& track) { return false; }
700  //----------------------------------------------------------------------------
701 
702  //============================================================================
709 
710  //============================================================================
721  inline void TransferPresets(const std::vector<std::string>& presets)
722  {
723  m_instanceData->toKodi->clear_presets(m_instanceData->toKodi->kodiInstance);
724  for (const auto& it : presets)
725  m_instanceData->toKodi->transfer_preset(m_instanceData->toKodi->kodiInstance, it.c_str());
726  }
727  //----------------------------------------------------------------------------
728 
729  //============================================================================
750  inline kodi::HardwareContext Device() { return m_instanceData->props->device; }
751  //----------------------------------------------------------------------------
752 
753  //============================================================================
759  inline int X() { return m_instanceData->props->x; }
760  //----------------------------------------------------------------------------
761 
762  //============================================================================
768  inline int Y() { return m_instanceData->props->y; }
769  //----------------------------------------------------------------------------
770 
771  //============================================================================
777  inline int Width() { return m_instanceData->props->width; }
778  //----------------------------------------------------------------------------
779 
780  //============================================================================
786  inline int Height() { return m_instanceData->props->height; }
787  //----------------------------------------------------------------------------
788 
789  //============================================================================
796  inline float PixelRatio() { return m_instanceData->props->pixelRatio; }
797  //----------------------------------------------------------------------------
798 
799  //============================================================================
805  inline std::string Name() { return m_instanceData->props->name; }
806  //----------------------------------------------------------------------------
807 
808  //============================================================================
814  inline std::string Presets() { return m_instanceData->props->presets; }
815  //----------------------------------------------------------------------------
816 
817  //============================================================================
827  inline std::string Profile() { return m_instanceData->props->profile; }
828  //----------------------------------------------------------------------------
829 
831 
832 private:
833  void SetAddonStruct(KODI_HANDLE instance)
834  {
835  if (instance == nullptr)
836  throw std::logic_error("kodi::addon::CInstanceVisualization: Null pointer instance passed.");
837 
838  m_instanceData = static_cast<AddonInstance_Visualization*>(instance);
839  m_instanceData->toAddon->addonInstance = this;
840  m_instanceData->toAddon->start = ADDON_Start;
841  m_instanceData->toAddon->stop = ADDON_Stop;
842  m_instanceData->toAddon->audio_data = ADDON_AudioData;
843  m_instanceData->toAddon->is_dirty = ADDON_IsDirty;
844  m_instanceData->toAddon->render = ADDON_Render;
845  m_instanceData->toAddon->get_info = ADDON_GetInfo;
846  m_instanceData->toAddon->prev_preset = ADDON_PrevPreset;
847  m_instanceData->toAddon->next_preset = ADDON_NextPreset;
848  m_instanceData->toAddon->load_preset = ADDON_LoadPreset;
849  m_instanceData->toAddon->random_preset = ADDON_RandomPreset;
850  m_instanceData->toAddon->lock_preset = ADDON_LockPreset;
851  m_instanceData->toAddon->rate_preset = ADDON_RatePreset;
852  m_instanceData->toAddon->update_albumart = ADDON_UpdateAlbumart;
853  m_instanceData->toAddon->update_track = ADDON_UpdateTrack;
854  m_instanceData->toAddon->get_presets = ADDON_GetPresets;
855  m_instanceData->toAddon->get_active_preset = ADDON_GetActivePreset;
856  m_instanceData->toAddon->is_locked = ADDON_IsLocked;
857  }
858 
859  inline static bool ADDON_Start(const AddonInstance_Visualization* addon,
860  int channels,
861  int samplesPerSec,
862  int bitsPerSample,
863  const char* songName)
864  {
865  CInstanceVisualization* thisClass =
866  static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance);
867  thisClass->m_renderHelper = kodi::gui::GetRenderHelper();
868  return thisClass->Start(channels, samplesPerSec, bitsPerSample, songName);
869  }
870 
871  inline static void ADDON_Stop(const AddonInstance_Visualization* addon)
872  {
873  CInstanceVisualization* thisClass =
874  static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance);
875  thisClass->Stop();
876  thisClass->m_renderHelper = nullptr;
877  }
878 
879  inline static void ADDON_AudioData(const AddonInstance_Visualization* addon,
880  const float* audioData,
881  int audioDataLength,
882  float* freqData,
883  int freqDataLength)
884  {
885  static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)
886  ->AudioData(audioData, audioDataLength, freqData, freqDataLength);
887  }
888 
889  inline static bool ADDON_IsDirty(const AddonInstance_Visualization* addon)
890  {
891  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)->IsDirty();
892  }
893 
894  inline static void ADDON_Render(const AddonInstance_Visualization* addon)
895  {
896  CInstanceVisualization* thisClass =
897  static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance);
898  if (!thisClass->m_renderHelper)
899  return;
900  thisClass->m_renderHelper->Begin();
901  thisClass->Render();
902  thisClass->m_renderHelper->End();
903  }
904 
905  inline static void ADDON_GetInfo(const AddonInstance_Visualization* addon, VIS_INFO* info)
906  {
907  static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)
908  ->GetInfo(info->bWantsFreq, info->iSyncDelay);
909  }
910 
911  inline static unsigned int ADDON_GetPresets(const AddonInstance_Visualization* addon)
912  {
913  CInstanceVisualization* thisClass =
914  static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance);
915  std::vector<std::string> presets;
916  if (thisClass->GetPresets(presets))
917  {
918  for (const auto& it : presets)
919  thisClass->m_instanceData->toKodi->transfer_preset(addon->toKodi->kodiInstance, it.c_str());
920  }
921 
922  return static_cast<unsigned int>(presets.size());
923  }
924 
925  inline static int ADDON_GetActivePreset(const AddonInstance_Visualization* addon)
926  {
927  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)->GetActivePreset();
928  }
929 
930  inline static bool ADDON_PrevPreset(const AddonInstance_Visualization* addon)
931  {
932  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)->PrevPreset();
933  }
934 
935  inline static bool ADDON_NextPreset(const AddonInstance_Visualization* addon)
936  {
937  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)->NextPreset();
938  }
939 
940  inline static bool ADDON_LoadPreset(const AddonInstance_Visualization* addon, int select)
941 
942  {
943  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)->LoadPreset(select);
944  }
945 
946  inline static bool ADDON_RandomPreset(const AddonInstance_Visualization* addon)
947  {
948  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)->RandomPreset();
949  }
950 
951  inline static bool ADDON_LockPreset(const AddonInstance_Visualization* addon)
952  {
953  CInstanceVisualization* thisClass =
954  static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance);
955  thisClass->m_presetLockedByUser = !thisClass->m_presetLockedByUser;
956  return thisClass->LockPreset(thisClass->m_presetLockedByUser);
957  }
958 
959  inline static bool ADDON_RatePreset(const AddonInstance_Visualization* addon, bool plus_minus)
960  {
961  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)
962  ->RatePreset(plus_minus);
963  }
964 
965  inline static bool ADDON_IsLocked(const AddonInstance_Visualization* addon)
966  {
967  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)->IsLocked();
968  }
969 
970  inline static bool ADDON_UpdateAlbumart(const AddonInstance_Visualization* addon,
971  const char* albumart)
972  {
973  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)
974  ->UpdateAlbumart(albumart);
975  }
976 
977  inline static bool ADDON_UpdateTrack(const AddonInstance_Visualization* addon,
978  const VIS_TRACK* track)
979  {
980  VisualizationTrack cppTrack(track);
981  return static_cast<CInstanceVisualization*>(addon->toAddon->addonInstance)
982  ->UpdateTrack(cppTrack);
983  }
984 
985  std::shared_ptr<kodi::gui::IRenderHelper> m_renderHelper;
986  bool m_presetLockedByUser = false;
987  AddonInstance_Visualization* m_instanceData;
988 };
989 
990 } /* namespace addon */
991 } /* namespace kodi */
992 #endif /* __cplusplus */
Definition: Visualization.h:396
Definition: AddonBase.h:186
Definition: Visualization.h:40
@ ADDON_INSTANCE_VISUALIZATION
Music visualization instance, see kodi::addon::CInstanceVisualization.
Definition: versions.h:238
std::string Presets()
Used to get the full path where the add-on is installed.
Definition: Visualization.h:814
int Height()
Returns the height of the rendering window.
Definition: Visualization.h:786
kodi::HardwareContext Device()
Device that represents the display adapter.
Definition: Visualization.h:750
std::string Profile()
Used to get the full path to the add-on's user profile.
Definition: Visualization.h:827
void TransferPresets(const std::vector< std::string > &presets)
To transfer available presets on addon.
Definition: Visualization.h:721
int X()
Returns the X position of the rendering window.
Definition: Visualization.h:759
int Width()
Returns the width of the rendering window.
Definition: Visualization.h:777
int Y()
Returns the Y position of the rendering window.
Definition: Visualization.h:768
std::string Name()
Used to get the name of the add-on defined in addon.xml.
Definition: Visualization.h:805
float PixelRatio()
Pixel aspect ratio (often abbreviated PAR) is a ratio that describes how the width of a pixel compare...
Definition: Visualization.h:796
int GetDisc() const
Get disk number (if present)
Definition: Visualization.h:144
void SetAlbum(const std::string &album)
Set Album that the current song is from.
Definition: Visualization.h:111
void SetTrack(int trackNumber)
Set track number (if present) from music info as integer.
Definition: Visualization.h:135
void SetComment(const std::string &comment)
Set additional information comment (if present).
Definition: Visualization.h:165
void SetDuration(int duration)
Set the duration of music as integer from info.
Definition: Visualization.h:129
int GetYear() const
Get year that the current song was released.
Definition: Visualization.h:150
void SetLyrics(const std::string &lyrics)
Set string from lyrics.
Definition: Visualization.h:153
int GetRating() const
Get the user-defined rating of the current song.
Definition: Visualization.h:162
const std::string & GetArtist() const
Get artist names, as a single string-.
Definition: Visualization.h:108
void SetTitle(const std::string &title)
Set title of the current song.
Definition: Visualization.h:99
const std::string & GetAlbum() const
Get Album that the current song is from.
Definition: Visualization.h:114
void SetArtist(const std::string &artist)
Set artist names, as a single string-.
Definition: Visualization.h:105
void SetAlbumArtist(const std::string &albumArtist)
Set album artist names, as a single stringalbum artist name.
Definition: Visualization.h:117
const std::string & GetLyrics() const
Get string from lyrics.
Definition: Visualization.h:156
int GetDuration() const
Get the duration of music as integer from info.
Definition: Visualization.h:132
void SetDisc(int discNumber)
Set disk number (if present) from music info as integer.
Definition: Visualization.h:141
void SetRating(int rating)
Set the user-defined rating of the current song.
Definition: Visualization.h:159
void SetYear(int year)
Set year that the current song was released.
Definition: Visualization.h:147
const std::string & GetGenre() const
Get genre name from music as string if present.
Definition: Visualization.h:126
int GetTrack() const
Get track number (if present).
Definition: Visualization.h:138
const std::string & GetTitle() const
Get title of the current song.
Definition: Visualization.h:102
void SetGenre(const std::string &genre)
Set genre name from music as string if present.
Definition: Visualization.h:123
const std::string & GetAlbumArtist() const
Get album artist names, as a single string-.
Definition: Visualization.h:120
const std::string & GetComment() const
Get additional information comment (if present).
Definition: Visualization.h:168
virtual bool LockPreset(bool lockUnlock)
Lock the current visualization preset, preventing it from changing.
Definition: Visualization.h:638
virtual void Render()
Used to indicate when the add-on should render.
Definition: Visualization.h:538
virtual bool RandomPreset()
Switch to a new random preset.
Definition: Visualization.h:628
virtual bool LoadPreset(int select)
Load a visualization preset.
Definition: Visualization.h:619
virtual bool IsDirty()
Used to inform Kodi that the rendered region is dirty and need an update.
Definition: Visualization.h:531
virtual bool UpdateTrack(const kodi::addon::VisualizationTrack &track)
Inform the visualization of the current track's tag information.
Definition: Visualization.h:699
virtual bool PrevPreset()
Load the previous visualization preset.
Definition: Visualization.h:598
virtual bool GetPresets(std::vector< std::string > &presets)
Used to get a list of visualization presets the user can select. from.
Definition: Visualization.h:571
virtual void GetInfo(bool &wantsFreq, int &syncDelay)
Used to get the number of buffers from the current visualization.
Definition: Visualization.h:554
virtual void AudioData(const float *audioData, int audioDataLength, float *freqData, int freqDataLength)
Pass audio data to the visualization.
Definition: Visualization.h:516
virtual int GetActivePreset()
Get the index of the current preset.
Definition: Visualization.h:580
virtual bool Start(int channels, int samplesPerSec, int bitsPerSample, std::string songName)
Used to notify the visualization that a new song has been started.
Definition: Visualization.h:488
~CInstanceVisualization() override=default
Destructor.
virtual bool UpdateAlbumart(std::string albumart)
Inform the visualization of the current album art image.
Definition: Visualization.h:659
CInstanceVisualization()
Visualization class constructor.
Definition: Visualization.h:405
virtual bool NextPreset()
Load the next visualization preset.
Definition: Visualization.h:607
virtual bool IsLocked()
Check if the add-on is locked to the current preset.
Definition: Visualization.h:589
virtual bool RatePreset(bool plusMinus)
Used to increase/decrease the visualization preset rating.
Definition: Visualization.h:649
virtual void Stop()
Used to inform the visualization that the rendering control was stopped.
Definition: Visualization.h:499
CInstanceVisualization(KODI_HANDLE instance, const std::string &kodiVersion="")
Visualization class constructor used to support multiple instance types.
Definition: Visualization.h:458
std::string ATTRIBUTE_HIDDEN GetKodiTypeVersion(int type)
To get used version inside Kodi itself about asked type.
Definition: AddonBase.h:630
Definition: visualization.h:110
Definition: visualization.h:22
Definition: visualization.h:28