File indexing completed on 2024-04-28 16:08:28
0001 // SPDX-FileCopyrightText: 2023 Mathis BrĂ¼chert <mbb@kaidan.im> 0002 // 0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 0005 #include "metronome.h" 0006 0007 #include <QDebug> 0008 0009 Metronome::Metronome(QObject *parent) 0010 : QObject(parent) 0011 { 0012 m_hitTimer.setSingleShot(false); 0013 setBpm(120); 0014 0015 connect(&m_hitTimer, &QTimer::timeout, this, &Metronome::hit); 0016 connect(&m_hitTimer, &QTimer::timeout, this, &Metronome::playHitSound); 0017 connect(&m_hitTimer, &QTimer::timeout, this, [=] { 0018 m_hitCount++; 0019 }); 0020 addNote(Sounds::F); 0021 } 0022 0023 int Metronome::bpm() const 0024 { 0025 return m_bpm; 0026 } 0027 0028 void Metronome::setBpm(int bpm) 0029 { 0030 m_bpm = bpm; 0031 Q_EMIT bpmChanged(); 0032 0033 const int intervalMsecs = (60 / double(m_bpm)) * 1000; 0034 m_hitTimer.setInterval(int(intervalMsecs)); 0035 } 0036 0037 void Metronome::playHitSound() 0038 { 0039 const int i = m_hitCount % m_notes.size(); 0040 0041 // To display it in the gui 0042 setCurrentIndex(i); 0043 0044 qDebug() << "Sound index" << i << m_notes.at(i)->soundFile(); 0045 m_mediaPlayer.setMedia(m_notes.at(i)->soundFile()); 0046 m_mediaPlayer.setVolume(m_notes.at(i)->volume()); 0047 0048 m_mediaPlayer.setPosition(0); 0049 m_mediaPlayer.play(); 0050 } 0051 0052 int Metronome::currentIndex() const 0053 { 0054 return m_currentIndex; 0055 } 0056 0057 void Metronome::setCurrentIndex(int curentIndex) 0058 { 0059 qDebug() << "Current index" << currentIndex(); 0060 m_currentIndex = curentIndex; 0061 Q_EMIT currentIndexChanged(); 0062 } 0063 0064 QVector<Note*> Metronome::notes() const 0065 { 0066 return m_notes; 0067 } 0068 0069 void Metronome::addNote(const Sounds sound) 0070 { 0071 auto *note = new Note(this); 0072 note->setSound(sound); 0073 m_notes.push_back(note); 0074 Q_EMIT notesChanged(); 0075 } 0076 0077 void Metronome::removeNote(const int index) 0078 { 0079 if (index < 1) { 0080 return; 0081 } 0082 m_notes.removeAt(index); 0083 Q_EMIT notesChanged(); 0084 } 0085 0086 void Metronome::start() 0087 { 0088 m_hitTimer.start(); 0089 Q_EMIT runningChanged(); 0090 } 0091 0092 void Metronome::stop() 0093 { 0094 m_hitTimer.stop(); 0095 Q_EMIT runningChanged(); 0096 } 0097 0098 bool Metronome::running() const { 0099 return m_hitTimer.isActive(); 0100 }