File indexing completed on 2024-04-28 15:08:06

0001 /* GCompris - GSynth.h
0002  *
0003  * SPDX-FileCopyrightText: 2018 Timothée Giet <animtim@gmail.com>
0004  *
0005  * Authors:
0006  *   Johnny Jazeix <jazeix@gmail.com>
0007  *   Timothée Giet <animtim@gmail.com>
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 #ifndef GSYNTH_H
0012 #define GSYNTH_H
0013 
0014 #include <QAudioDeviceInfo>
0015 #include <QAudioOutput>
0016 #include <QTimer>
0017 #include <QMap>
0018 #include "preset.h"
0019 
0020 class QQmlEngine;
0021 class QJSEngine;
0022 
0023 class Generator;
0024 
0025 class GSynth : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     explicit GSynth(QObject *parent = nullptr);
0031     virtual ~GSynth();
0032 
0033     /**
0034      * Generate a note and start the corresponding timer
0035      *  to stop it at "duration" ms.
0036      *
0037      * @param note note to play
0038      * @param duration how much time the note needs to be played
0039      */
0040     Q_INVOKABLE void generate(int note, int duration);
0041 
0042     static GSynth *getInstance() {
0043         if(!m_instance) {
0044             m_instance = new GSynth();
0045         }
0046         return m_instance;
0047     }
0048     static QObject *synthProvider(QQmlEngine *engine,
0049                                   QJSEngine *scriptEngine);
0050 
0051 protected:
0052     static GSynth *m_instance;
0053 
0054 private Q_SLOTS:
0055     void stopAudio(int note);
0056     
0057 private:
0058     Generator        *m_generator;
0059     QAudioDeviceInfo  m_device;
0060     QAudioFormat      m_format;
0061     QByteArray        m_buffer;
0062     QAudioOutput     *m_audioOutput;
0063     QMap<int, QTimer *> m_timers;
0064     
0065     Preset PresetCustom;
0066 };
0067 
0068 #endif // GSYNTH_H
0069