TTKMusicPlayer  4.3.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  delete m_player;
64  delete m_playlist;
65 }
66 
68 {
69  QString text = TTK_STR_CAT("\n", TTK_APP_NAME, " Console Module v", TTK_VERSION_STR, "\n");
70  text += "Offical web page: https://github.com/Greedysky/TTKMusicPlayer\n";
71  text += "Copyright(C) 2015 - 2026 Greedysky All Rights Reserved\n";
72  text += "TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux\n\n";
73  text += "Global shortcuts:\n" + shortcutMessage() + "\n";
74 
75  TTKCommandLineOption op0("-h", "--help", "Show command line help options");
76  TTKCommandLineOption op1("-u", "--url", "Media play url path");
77  TTKCommandLineOption op2("-d", "--dir", "Media play dir path");
78  TTKCommandLineOption op3("-l", "--playlist", "Media playlist url path");
79 
80  TTKCommandLineParser parser;
81  parser.addOption(op0);
82  parser.addOption(op1);
83  parser.addOption(op2);
84  parser.addOption(op3);
85  parser.setDescription(text);
86  parser.process();
87 
88  if(parser.isEmpty() || parser.isSet(op0))
89  {
90  parser.showHelp();
91  return false;
92  }
93 
94  if(parser.isSet(op1))
95  {
96  const QString &path = parser.value(op1);
97  if(path.isEmpty())
98  {
99  TTK_LOG_STREAM("Media play url path is empty");
100  return false;
101  }
102  else
103  {
104  TTK_LOG_STREAM("Add play url path: " << path);
105  m_playlist->add(0, path);
107  }
108  }
109  else if(parser.isSet(op2))
110  {
111  const QString &path = parser.value(op2);
112  if(path.isEmpty())
113  {
114  TTK_LOG_STREAM("Media play dir path is empty");
115  return false;
116  }
117  else
118  {
120  {
121  TTK_LOG_STREAM("Add play url path: " << fin.absoluteFilePath());
122  m_playlist->append(0, fin.absoluteFilePath());
123  }
124 
125  if(!m_playlist->isEmpty())
126  {
128  }
129  }
130  }
131  else if(parser.isSet(op3))
132  {
133  const QString &path = parser.value(op3);
134  if(path.isEmpty())
135  {
136  TTK_LOG_STREAM("Media playlist path is empty");
137  return false;
138  }
139  else
140  {
141  if(TTK_FILE_SUFFIX(QFileInfo(path)) != TPL_FILE_SUFFIX)
142  {
143  TTK_LOG_STREAM("Media playlist format not support");
144  return false;
145  }
146 
147  MusicTKPLConfigManager manager;
148  if(!manager.fromFile(path))
149  {
150  TTK_LOG_STREAM("Media playlist read error");
151  return false;
152  }
153 
154  MusicSongItemList items;
155  manager.readBuffer(items);
156 
157  for(const MusicSongItem &item : qAsConst(items))
158  {
159  for(const MusicSong &song : qAsConst(item.m_songs))
160  {
161  TTK_LOG_STREAM("Add play url path: " << song.path());
162  m_playlist->append(0, song.path());
163  }
164  }
165 
166  if(!m_playlist->isEmpty())
167  {
169  }
170  }
171  }
172  else
173  {
174  TTK_LOG_STREAM("Options unknown error");
175  return false;
176  }
177 
178  TTK_LOG_STREAM("Media files count: " << m_playlist->count() << TTK_LINEFEED);
179 
180  m_player->play();
182  return QCoreApplication::exec();
183 }
184 
186 {
187  print(position, m_player->duration());
188 }
189 
191 {
192  TTK_LOG_STREAM("Current play index: " << index);
193 // TTK_SIGNLE_SHOT(TTK_DN_S2MS, this, resetVolume, TTK_SLOT);
194 
195  if(index == TTK_NORMAL_LEVEL)
196  {
197  m_player->stop();
198  TTK_SIGNLE_SHOT(TTK_DN_S2MS, qApp, quit, TTK_SLOT);
199  TTK_LOG_STREAM("Media play end and application quit now");
200  }
201 }
202 
204 {
205  if(m_playlist->isEmpty())
206  {
207  return;
208  }
209 
210  if(m_player->isPlaying())
211  {
212  m_player->pause();
213  }
214  else
215  {
216  m_player->play();
218  }
219 }
220 
222 {
223  if(m_playlist->isEmpty())
224  {
225  return;
226  }
227 
229 
230  m_player->play();
232 }
233 
235 {
236  if(m_playlist->isEmpty())
237  {
238  return;
239  }
240 
242 
243  m_player->play();
245 }
246 
247 //void MusicConsoleModule::resetVolume()
248 //{
249 // m_player->setVolume(m_volume);
250 //}
251 
253 {
254  m_volume = m_player->volume();
255  m_volume -= 15;
256  if(m_volume < 0)
257  {
258  m_volume = 0;
259  }
260 
262 }
263 
265 {
266  m_volume = m_player->volume();
267  m_volume += 15;
268  if(m_volume > 100)
269  {
270  m_volume = 100;
271  }
272 
274 }
275 
277 {
279  m_playbackMode = "Order";
280 }
281 
283 {
285  m_playbackMode = "Random";
286 }
287 
289 {
291  m_playbackMode = "ListLoop";
292 }
293 
295 {
297  m_playbackMode = "OneLoop";
298 }
299 
301 {
303  m_playbackMode = "Once";
304 }
305 
307 {
309  m_enhanced = "Off";
310 }
311 
313 {
315  m_enhanced = "3D";
316 }
317 
319 {
321  m_enhanced = "NICAM";
322 }
323 
325 {
327  m_enhanced = "Subwoofer";
328 }
329 
331 {
333  m_enhanced = "Vocal";
334 }
335 
336 void MusicConsoleModule::print(qint64 position, qint64 duration) const
337 {
338  const MusicPlayItem &item = m_playlist->currentItem();
339  TTK_LOG_STREAM(QString("Time:[%1/%2], Volume:%3, PlaybackMode:%4, Enhance:%5, Media Path: %6")
340  .arg(TTKTime::formatDuration(position), TTKTime::formatDuration(duration))
341  .arg(m_player->volume())
342  .arg(m_playbackMode, m_enhanced, item.m_path));
343 }
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:44
MusicPlaylist * m_playlist
The class of the music player.
Definition: musicplayer.h:46
qint64 duration() const
Definition: musicplayer.cpp:49
void add(int playlistRow, const QString &content)
void setVolume(int volume)
Definition: musicplayer.cpp:69
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:34
#define TTK_SLOT
Definition: ttkqtglobal.h:177
static constexpr int PLAY_NEXT_LEVEL
Definition: musicplaylist.h:57
#define TTK_VERSION_STR
Definition: ttkversion.h:179
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:160
The class of the tkpl config manager.
MusicConsoleModule(QObject *parent=nullptr)
void append(int playlistRow, const QString &content)
bool isEmpty() const noexcept
#define TTK_STR_CAT(...)
Definition: ttkglobal.h:240
#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:64
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 setEnhance(TTK::Enhance type)
Definition: musicplayer.cpp:86
void print(qint64 position, qint64 duration) const
void currentIndexChanged(int index)
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123