diff --git a/src/core/synth/GSynth.cpp b/src/core/synth/GSynth.cpp index 6c8b32c2c..e5fc79abb 100644 --- a/src/core/synth/GSynth.cpp +++ b/src/core/synth/GSynth.cpp @@ -1,97 +1,97 @@ /* GCompris - GSynth.cpp * * Copyright (C) 2018 Timothée Giet * * Authors: * Johnny Jazeix * Timothée Giet * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ #include "GSynth.h" #include "generator.h" #include GSynth *GSynth::m_instance = nullptr; GSynth::GSynth(QObject *parent) : QObject(parent) { - bufferSize = 8192; + static const int bufferSize = 8192; m_format.setSampleRate(22050); m_format.setChannelCount(1); m_format.setSampleSize(16); m_format.setCodec("audio/pcm"); m_format.setByteOrder(QAudioFormat::LittleEndian); m_format.setSampleType(QAudioFormat::SignedInt); QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); if (!info.isFormatSupported(m_format)) { qWarning() << "Default format not supported - trying to use nearest"; m_format = info.nearestFormat(m_format); } m_device = QAudioDeviceInfo::defaultOutputDevice(); m_buffer = QByteArray(bufferSize, 0); m_audioOutput = new QAudioOutput(m_device, m_format, this); m_audioOutput->setBufferSize(bufferSize); m_generator = new Generator(m_format, this); // todo Only start generator if musical activity, and stop it on exit (in main.qml, activity.isMusicalActivity) m_generator->setPreset(PresetCustom); m_generator->start(); m_audioOutput->start(m_generator); m_audioOutput->setVolume(1); } GSynth::~GSynth() { m_audioOutput->stop(); m_generator->stop(); delete m_audioOutput; delete m_generator; auto i = m_timers.constBegin(); while (i != m_timers.constEnd()) { delete i.value(); ++i; } } void GSynth::generate(int note, int duration) { //test part... m_generator->noteOn(1, note, 255); if(!m_timers.contains(note)) { m_timers[note] = new QTimer(); connect(m_timers[note], &QTimer::timeout, this, [this, note]() { stopAudio(note); }); } m_timers[note]->start(duration); } void GSynth::stopAudio(int note) { m_generator->noteOff(1, note); } QObject *GSynth::synthProvider(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) GSynth* synth = getInstance(); return synth; } diff --git a/src/core/synth/GSynth.h b/src/core/synth/GSynth.h index 962e975ed..09fce75ba 100644 --- a/src/core/synth/GSynth.h +++ b/src/core/synth/GSynth.h @@ -1,79 +1,77 @@ /* GCompris - GSynth.h * * Copyright (C) 2018 Timothée Giet * * Authors: * Johnny Jazeix * Timothée Giet * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ #ifndef GSYNTH_H #define GSYNTH_H #include #include #include #include #include "preset.h" class Generator; class GSynth : public QObject { Q_OBJECT public: explicit GSynth(QObject *parent = 0); virtual ~GSynth(); /** * Generate a note and start the corresponding timer * to stop it at "duration" ms. * * @param note note to play * @param duration how much time the note needs to be played */ Q_INVOKABLE void generate(int note, int duration); static GSynth *getInstance() { if(!m_instance) { m_instance = new GSynth(); } return m_instance; } static QObject *synthProvider(QQmlEngine *engine, QJSEngine *scriptEngine); protected: static GSynth *m_instance; private slots: void stopAudio(int note); private: - unsigned int bufferSize; - Generator *m_generator; QAudioDeviceInfo m_device; QAudioFormat m_format; QByteArray m_buffer; QAudioOutput *m_audioOutput; QMap m_timers; Preset PresetCustom; }; #endif // GSYNTH_H