TTKMusicPlayer  3.7.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 
11  : QObject(parent),
12  m_volume(100),
13  m_playbackMode("Order"),
14  m_enhanced("Off")
15 {
16  m_playlist = new MusicPlaylist(this);
18  m_player = new MusicPlayer(this);
20 
21  connect(m_player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
22  connect(m_playlist, SIGNAL(currentIndexChanged(int)), SLOT(currentIndexChanged(int)));
23 
24  G_HOTKEY_PTR->addHotKey(this, "Ctrl+B", SLOT(switchToPlayState()));
25  G_HOTKEY_PTR->addHotKey(this, "Ctrl+Left", SLOT(playPrevious()));
26  G_HOTKEY_PTR->addHotKey(this, "Ctrl+Right", SLOT(playNext()));
27  G_HOTKEY_PTR->addHotKey(this, "Ctrl+Up", SLOT(volumeUp()));
28  G_HOTKEY_PTR->addHotKey(this, "Ctrl+Down", SLOT(volumeDown()));
29  G_HOTKEY_PTR->addHotKey(this, "Ctrl+1", SLOT(playOrder()));
30  G_HOTKEY_PTR->addHotKey(this, "Ctrl+2", SLOT(playRandom()));
31  G_HOTKEY_PTR->addHotKey(this, "Ctrl+3", SLOT(playlistLoop()));
32  G_HOTKEY_PTR->addHotKey(this, "Ctrl+4", SLOT(playOneLoop()));
33  G_HOTKEY_PTR->addHotKey(this, "Ctrl+5", SLOT(playOnce()));
34  G_HOTKEY_PTR->addHotKey(this, "Alt+1", SLOT(setEnhancedOff()));
35  G_HOTKEY_PTR->addHotKey(this, "Alt+2", SLOT(setEnhanced3D()));
36  G_HOTKEY_PTR->addHotKey(this, "Alt+3", SLOT(setEnhancedNICAM()));
37  G_HOTKEY_PTR->addHotKey(this, "Alt+4", SLOT(setEnhancedSubwoofer()));
38  G_HOTKEY_PTR->addHotKey(this, "Alt+5", SLOT(setEnhancedVocal()));
39  G_HOTKEY_PTR->addHotKey(qApp, "Ctrl+Q", SLOT(quit()));
40 }
41 
43 {
44  delete m_player;
45  delete m_playlist;
46 }
47 
49 {
50  QString text = "\n" TTK_APP_NAME "Console Module" "v" TTK_VERSION_STR "\n";
51  text += "Offical web page: https://github.com/Greedysky/TTKMusicPlayer\n";
52  text += "Copyright(C) 2015 - 2024 Greedysky All Rights Reserved\n";
53  text += "TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux\n\n";
54 
55  TTKCommandLineOption op0("-h", "--help", "Show command line help options");
56  TTKCommandLineOption op1("-u", "--url", "Music play url path");
57  TTKCommandLineOption op2("-d", "--dir", "Music play dir path");
58  TTKCommandLineOption op3("-l", "--playlist", "Music playlist url path");
59 
60  TTKCommandLineParser parser;
61  parser.addOption(op0);
62  parser.addOption(op1);
63  parser.addOption(op2);
64  parser.addOption(op3);
65  parser.setDescription(text);
66  parser.process();
67 
68  if(parser.isEmpty() || parser.isSet(op0))
69  {
70  parser.showHelp();
71  return false;
72  }
73 
74  if(parser.isSet(op1))
75  {
76  const QString &path = parser.value(op1);
77  if(path.isEmpty())
78  {
79  TTK_LOG_STREAM("Music play url path is empty");
80  return false;
81  }
82  else
83  {
84  TTK_LOG_STREAM("Add play url path: " << path);
85  m_playlist->add(0, path);
87  }
88  }
89  else if(parser.isSet(op2))
90  {
91  const QString &path = parser.value(op2);
92  if(path.isEmpty())
93  {
94  TTK_LOG_STREAM("Music play dir path is empty");
95  return false;
96  }
97  else
98  {
100  {
101  TTK_LOG_STREAM("Add play url path: " << fin.absoluteFilePath());
102  m_playlist->append(0, fin.absoluteFilePath());
103  }
104 
105  if(!m_playlist->isEmpty())
106  {
108  }
109  }
110  }
111  else if(parser.isSet(op3))
112  {
113  const QString &path = parser.value(op3);
114  if(path.isEmpty())
115  {
116  TTK_LOG_STREAM("Music playlist path is empty");
117  return false;
118  }
119  else
120  {
121  if(TTK_FILE_SUFFIX(QFileInfo(path)) != TPL_FILE_SUFFIX)
122  {
123  TTK_LOG_STREAM("Music playlist format not support");
124  return false;
125  }
126 
127  MusicTKPLConfigManager manager;
128  if(!manager.fromFile(path))
129  {
130  TTK_LOG_STREAM("Music playlist read error");
131  return false;
132  }
133 
134  MusicSongItemList items;
135  manager.readBuffer(items);
136 
137  for(const MusicSongItem &item : qAsConst(items))
138  {
139  for(const MusicSong &song : qAsConst(item.m_songs))
140  {
141  TTK_LOG_STREAM("Add play url path: " << song.path());
142  m_playlist->append(0, song.path());
143  }
144  }
145 
146  if(!m_playlist->isEmpty())
147  {
149  }
150  }
151  }
152  else
153  {
154  TTK_LOG_STREAM("Options unknown error");
155  return false;
156  }
157 
158  TTK_LOG_STREAM("Music Files count: " << m_playlist->count() << "\n");
159 
160  m_player->play();
162  return QCoreApplication::exec();
163 }
164 
166 {
167  print(position, m_player->duration());
168 }
169 
171 {
172  TTK_LOG_STREAM("Current Play Indedx: " << index);
174 
175  if(index == TTK_NORMAL_LEVEL)
176  {
177  m_player->stop();
178  TTK_SIGNLE_SHOT(TTK_DN_S2MS, qApp, quit, TTK_SLOT);
179  TTK_LOG_STREAM("Music play end and application quit now");
180  }
181 }
182 
184 {
185  if(m_playlist->isEmpty())
186  {
187  return;
188  }
189 
190  if(m_player->isPlaying())
191  {
192  m_player->pause();
193  }
194  else
195  {
196  m_player->play();
198  }
199 }
200 
202 {
203  if(m_playlist->isEmpty())
204  {
205  return;
206  }
207 
209 
210  m_player->play();
212 }
213 
215 {
216  if(m_playlist->isEmpty())
217  {
218  return;
219  }
220 
222 
223  m_player->play();
225 }
226 
228 {
230 }
231 
233 {
234  m_volume = m_player->volume();
235  m_volume -= 15;
236  if(m_volume < 0)
237  {
238  m_volume = 0;
239  }
240 
242 }
243 
245 {
246  m_volume = m_player->volume();
247  m_volume += 15;
248  if(m_volume > 100)
249  {
250  m_volume = 100;
251  }
252 
254 }
255 
257 {
259  m_playbackMode = "Order";
260 }
261 
263 {
265  m_playbackMode = "Random";
266 }
267 
269 {
271  m_playbackMode = "ListLoop";
272 }
273 
275 {
277  m_playbackMode = "OneLoop";
278 }
279 
281 {
283  m_playbackMode = "Once";
284 }
285 
287 {
289  m_enhanced = "Off";
290 }
291 
293 {
295  m_enhanced = "3D";
296 }
297 
299 {
301  m_enhanced = "NICAM";
302 }
303 
305 {
307  m_enhanced = "Subwoofer";
308 }
309 
311 {
313  m_enhanced = "Vocal";
314 }
315 
316 void MusicConsoleModule::print(qint64 position, qint64 duration) const
317 {
318  const MusicPlayItem &item = m_playlist->currentItem();
319  TTK_LOG_STREAM(QString("Music Name: %1, Time:[%2/%3], Volume:%4, PlaybackMode:%5, Enhance:%6")
320  .arg(item.m_path, TTKTime::formatDuration(position), TTKTime::formatDuration(duration))
321  .arg(m_player->volume())
322  .arg(m_playbackMode, m_enhanced));
323 }
The class of the music play item.
Definition: musicplaylist.h:27
QString value(const TTKCommandLineOption &option) const
int count() const
void setDescription(const QString &description) noexcept
#define G_HOTKEY_PTR
bool fromFile(const QString &name)
static QStringList supportMusicInputFilterFormats()
QString m_path
Definition: musicplaylist.h:30
#define TTK_APP_NAME
Definition: ttkobject.h:25
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
MusicPlayItem currentItem() const
static constexpr int PLAY_PREVIOUS_LEVEL
Definition: musicplaylist.h:58
#define qAsConst
Definition: ttkqtglobal.h:53
bool isEmpty() const
bool isSet(const TTKCommandLineOption &option) const
#define TTK_SIGNLE_SHOT(...)
Definition: ttkqtglobal.h:177
#define TTK_NORMAL_LEVEL
Definition: ttkglobal.h:254
void setPlaybackMode(TTK::PlayMode mode)
bool addOption(const TTKCommandLineOption &option)
#define TTK_LOG_STREAM(msg)
Definition: ttklogger.h:63
TTK_MODULE_EXPORT QFileInfoList fileInfoListByPath(const QString &dpath, const QStringList &filter={}, bool recursively=true)
#define TTK_SLOT
Definition: ttkqtglobal.h:165
static constexpr int PLAY_NEXT_LEVEL
Definition: musicplaylist.h:57
#define TTK_VERSION_STR
Definition: ttkversion.h:171
void positionChanged(qint64 position)
void setCurrentIndex(int index)
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
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 TPL_FILE_SUFFIX
Definition: musicobject.h:37
#define TTK_FILE_SUFFIX(fin)
Definition: ttkqtglobal.h:173
int volume() const
Definition: musicplayer.cpp:65
The class of the music song info.
Definition: musicsong.h:28
void setPlaylist(MusicPlaylist *playlist)
Definition: musicplayer.cpp:45
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
bool isPlaying() const
Definition: musicplayer.cpp:35
void currentIndexChanged(int index)
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123