TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicconsolemodule.cpp
Go to the documentation of this file.
1 #include "musicconsolemodule.h"
2 #include "musicplayer.h"
3 #include "musicplaylist.h"
4 #include "musicformats.h"
6 #include "musichotkeymanager.h"
7 #include "musicfileutils.h"
8 #include "ttkversion.h"
9 
10 static QString shortcutMessage()
11 {
12  return QString(" %1 (%2)\n").arg("Ctrl+B", "Switch to play state") +
13  QString(" %1 (%2)\n").arg("Ctrl+Left", "Play previous") +
14  QString(" %1 (%2)\n").arg("Ctrl+Right", "Play next") +
15  QString(" %1 (%2)\n").arg("Ctrl+Up", "Volume up") +
16  QString(" %1 (%2)\n").arg("Ctrl+Down", "Volume down") +
17  QString(" %1 (%2)\n").arg("Ctrl+1", "Play random") +
18  QString(" %1 (%2)\n").arg("Ctrl+2", "Playlist loop") +
19  QString(" %1 (%2)\n").arg("Ctrl+3", "Play one loop") +
20  QString(" %1 (%2)\n").arg("Ctrl+4", "Play once") +
21  QString(" %1 (%2)\n").arg("Alt+1", "Enhanced off") +
22  QString(" %1 (%2)\n").arg("Alt+2", "Enhanced 3D") +
23  QString(" %1 (%2)\n").arg("Alt+3", "Enhanced NICAM") +
24  QString(" %1 (%2)\n").arg("Alt+4", "Enhanced subwoofer") +
25  QString(" %1 (%2)\n").arg("Alt+5", "Enhanced cocal") +
26  QString(" %1 (%2)\n").arg("Ctrl+Q", "Quit");
27 }
28 
30  : QObject(parent),
31  m_volume(100),
32  m_playbackMode("Order"),
33  m_enhanced("Off")
34 {
35  m_playlist = new MusicPlaylist(this);
37  m_player = new MusicPlayer(this);
39 
40  connect(m_player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
41  connect(m_playlist, SIGNAL(currentIndexChanged(int)), SLOT(currentIndexChanged(int)));
42 
43  G_HOTKEY_PTR->addHotKey(this, "Ctrl+B", SLOT(switchToPlayState()));
44  G_HOTKEY_PTR->addHotKey(this, "Ctrl+Left", SLOT(playPrevious()));
45  G_HOTKEY_PTR->addHotKey(this, "Ctrl+Right", SLOT(playNext()));
46  G_HOTKEY_PTR->addHotKey(this, "Ctrl+Up", SLOT(volumeUp()));
47  G_HOTKEY_PTR->addHotKey(this, "Ctrl+Down", SLOT(volumeDown()));
48  G_HOTKEY_PTR->addHotKey(this, "Ctrl+1", SLOT(playOrder()));
49  G_HOTKEY_PTR->addHotKey(this, "Ctrl+2", SLOT(playRandom()));
50  G_HOTKEY_PTR->addHotKey(this, "Ctrl+3", SLOT(playlistLoop()));
51  G_HOTKEY_PTR->addHotKey(this, "Ctrl+4", SLOT(playOneLoop()));
52  G_HOTKEY_PTR->addHotKey(this, "Ctrl+5", SLOT(playOnce()));
53  G_HOTKEY_PTR->addHotKey(this, "Alt+1", SLOT(setEnhancedOff()));
54  G_HOTKEY_PTR->addHotKey(this, "Alt+2", SLOT(setEnhanced3D()));
55  G_HOTKEY_PTR->addHotKey(this, "Alt+3", SLOT(setEnhancedNICAM()));
56  G_HOTKEY_PTR->addHotKey(this, "Alt+4", SLOT(setEnhancedSubwoofer()));
57  G_HOTKEY_PTR->addHotKey(this, "Alt+5", SLOT(setEnhancedVocal()));
58  G_HOTKEY_PTR->addHotKey(qApp, "Ctrl+Q", SLOT(quit()));
59 }
60 
62 {
63  G_HOTKEY_PTR->unsetShortcut();
64  delete m_player;
65  delete m_playlist;
66 }
67 
69 {
70  QString text = QString("\n%1 Console Module v%2\n").arg(TTK_APP_NAME, TTK_VERSION_STR);
71  text += "Offical web page: https://github.com/Greedysky/TTKMusicPlayer\n";
72  text += "Copyright(C) 2015 - 2025 Greedysky All Rights Reserved\n";
73  text += "TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux\n\n";
74  text += "Global shortcuts:\n" + shortcutMessage() + "\n";
75 
76  TTKCommandLineOption op0("-h", "--help", "Show command line help options");
77  TTKCommandLineOption op1("-u", "--url", "Music play url path");
78  TTKCommandLineOption op2("-d", "--dir", "Music play dir path");
79  TTKCommandLineOption op3("-l", "--playlist", "Music playlist url path");
80 
81  TTKCommandLineParser parser;
82  parser.addOption(op0);
83  parser.addOption(op1);
84  parser.addOption(op2);
85  parser.addOption(op3);
86  parser.setDescription(text);
87  parser.process();
88 
89  if(parser.isEmpty() || parser.isSet(op0))
90  {
91  parser.showHelp();
92  return false;
93  }
94 
95  if(parser.isSet(op1))
96  {
97  const QString &path = parser.value(op1);
98  if(path.isEmpty())
99  {
100  TTK_LOG_STREAM("Music play url path is empty");
101  return false;
102  }
103  else
104  {
105  TTK_LOG_STREAM("Add play url path: " << path);
106  m_playlist->add(0, path);
108  }
109  }
110  else if(parser.isSet(op2))
111  {
112  const QString &path = parser.value(op2);
113  if(path.isEmpty())
114  {
115  TTK_LOG_STREAM("Music play dir path is empty");
116  return false;
117  }
118  else
119  {
121  {
122  TTK_LOG_STREAM("Add play url path: " << fin.absoluteFilePath());
123  m_playlist->append(0, fin.absoluteFilePath());
124  }
125 
126  if(!m_playlist->isEmpty())
127  {
129  }
130  }
131  }
132  else if(parser.isSet(op3))
133  {
134  const QString &path = parser.value(op3);
135  if(path.isEmpty())
136  {
137  TTK_LOG_STREAM("Music playlist path is empty");
138  return false;
139  }
140  else
141  {
142  if(TTK_FILE_SUFFIX(QFileInfo(path)) != TPL_FILE_SUFFIX)
143  {
144  TTK_LOG_STREAM("Music playlist format not support");
145  return false;
146  }
147 
148  MusicTKPLConfigManager manager;
149  if(!manager.fromFile(path))
150  {
151  TTK_LOG_STREAM("Music playlist read error");
152  return false;
153  }
154 
155  MusicSongItemList items;
156  manager.readBuffer(items);
157 
158  for(const MusicSongItem &item : qAsConst(items))
159  {
160  for(const MusicSong &song : qAsConst(item.m_songs))
161  {
162  TTK_LOG_STREAM("Add play url path: " << song.path());
163  m_playlist->append(0, song.path());
164  }
165  }
166 
167  if(!m_playlist->isEmpty())
168  {
170  }
171  }
172  }
173  else
174  {
175  TTK_LOG_STREAM("Options unknown error");
176  return false;
177  }
178 
179  TTK_LOG_STREAM("Music files count: " << m_playlist->count() << TTK_LINEFEED);
180 
181  m_player->play();
183  return QCoreApplication::exec();
184 }
185 
187 {
188  print(position, m_player->duration());
189 }
190 
192 {
193  TTK_LOG_STREAM("Current play index: " << index);
194 // TTK_SIGNLE_SHOT(TTK_DN_S2MS, this, resetVolume, TTK_SLOT);
195 
196  if(index == TTK_NORMAL_LEVEL)
197  {
198  m_player->stop();
199  TTK_SIGNLE_SHOT(TTK_DN_S2MS, qApp, quit, TTK_SLOT);
200  TTK_LOG_STREAM("Music play end and application quit now");
201  }
202 }
203 
205 {
206  if(m_playlist->isEmpty())
207  {
208  return;
209  }
210 
211  if(m_player->isPlaying())
212  {
213  m_player->pause();
214  }
215  else
216  {
217  m_player->play();
219  }
220 }
221 
223 {
224  if(m_playlist->isEmpty())
225  {
226  return;
227  }
228 
230 
231  m_player->play();
233 }
234 
236 {
237  if(m_playlist->isEmpty())
238  {
239  return;
240  }
241 
243 
244  m_player->play();
246 }
247 
248 //void MusicConsoleModule::resetVolume()
249 //{
250 // m_player->setVolume(m_volume);
251 //}
252 
254 {
255  m_volume = m_player->volume();
256  m_volume -= 15;
257  if(m_volume < 0)
258  {
259  m_volume = 0;
260  }
261 
263 }
264 
266 {
267  m_volume = m_player->volume();
268  m_volume += 15;
269  if(m_volume > 100)
270  {
271  m_volume = 100;
272  }
273 
275 }
276 
278 {
280  m_playbackMode = "Order";
281 }
282 
284 {
286  m_playbackMode = "Random";
287 }
288 
290 {
292  m_playbackMode = "ListLoop";
293 }
294 
296 {
298  m_playbackMode = "OneLoop";
299 }
300 
302 {
304  m_playbackMode = "Once";
305 }
306 
308 {
310  m_enhanced = "Off";
311 }
312 
314 {
316  m_enhanced = "3D";
317 }
318 
320 {
322  m_enhanced = "NICAM";
323 }
324 
326 {
328  m_enhanced = "Subwoofer";
329 }
330 
332 {
334  m_enhanced = "Vocal";
335 }
336 
337 void MusicConsoleModule::print(qint64 position, qint64 duration) const
338 {
339  const MusicPlayItem &item = m_playlist->currentItem();
340  TTK_LOG_STREAM(QString("Time:[%1/%2], Volume:%3, PlaybackMode:%4, Enhance:%5, Music Path: %6")
341  .arg(TTKTime::formatDuration(position), TTKTime::formatDuration(duration))
342  .arg(m_player->volume())
343  .arg(m_playbackMode, m_enhanced, item.m_path));
344 }
The class of the music play item.
Definition: musicplaylist.h:27
QString value(const TTKCommandLineOption &option) const
static QStringList supportMusicInputFilterFormats() noexcept
void setDescription(const QString &description) noexcept
#define G_HOTKEY_PTR
MusicPlayItem currentItem() const noexcept
bool fromFile(const QString &name)
QString m_path
Definition: musicplaylist.h:30
#define TTK_APP_NAME
Definition: ttkobject.h:25
bool isEmpty() const noexcept
void setPlaylist(MusicPlaylist *playlist) noexcept
Definition: musicplayer.cpp:45
void setEnhanced(Enhance type)
Definition: musicplayer.cpp:87
MusicPlaylist * m_playlist
The class of the music player.
Definition: musicplayer.h:31
qint64 duration() const
Definition: musicplayer.cpp:50
void add(int playlistRow, const QString &content)
void setVolume(int volume)
Definition: musicplayer.cpp:70
The class of the command line option.
virtual bool readBuffer(MusicSongItemList &items) overridefinal
static constexpr int PLAY_PREVIOUS_LEVEL
Definition: musicplaylist.h:58
#define qAsConst
Definition: ttkqtglobal.h:57
bool isSet(const TTKCommandLineOption &option) const
#define TTK_SIGNLE_SHOT(...)
Definition: ttkqtglobal.h:189
#define TTK_NORMAL_LEVEL
Definition: ttkglobal.h:333
bool addOption(const TTKCommandLineOption &option)
static QString shortcutMessage()
#define TTK_LOG_STREAM(msg)
Definition: ttklogger.h:70
TTK_MODULE_EXPORT QFileInfoList fileInfoListByPath(const QString &dpath, const QStringList &filter={}, bool recursively=true)
bool isPlaying() const noexcept
Definition: musicplayer.cpp:35
#define TTK_SLOT
Definition: ttkqtglobal.h:177
static constexpr int PLAY_NEXT_LEVEL
Definition: musicplaylist.h:57
#define TTK_VERSION_STR
Definition: ttkversion.h:175
void positionChanged(qint64 position)
void setPlaybackMode(TTK::PlayMode mode) noexcept
void setCurrentIndex(int index)
int count() const noexcept
#define TTK_DN_S2MS
Definition: ttkglobal.h:355
The class of the music song item.
Definition: musicsong.h:161
The class of the tkpl config manager.
MusicConsoleModule(QObject *parent=nullptr)
void append(int playlistRow, const QString &content)
bool isEmpty() const noexcept
#define TTK_LINEFEED
Definition: ttkglobal.h:271
#define TPL_FILE_SUFFIX
Definition: musicobject.h:44
#define TTK_FILE_SUFFIX(fin)
Definition: ttkqtglobal.h:185
int volume() const
Definition: musicplayer.cpp:65
The class of the music song info.
Definition: musicsong.h:28
void showHelp() const
The class of the music play list.
Definition: musicplaylist.h:63
The class of the command line parser.
void print(qint64 position, qint64 duration) const
void currentIndexChanged(int index)
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123