Commit 66e3c6f4 authored by Divyam Madaan's avatar Divyam Madaan Committed by Johnny Jazeix
Browse files

background music: add first version of the feature

parent c631ea62
......@@ -31,4 +31,10 @@ ExploreLevels {
numberOfLevels: 1
url: "qrc:/gcompris/src/activities/explore_world_music/resource/"
hasAudioQuestions: true
property bool backgroundMusicStatus
onStart: {
backgroundMusicStatus = ApplicationSettings.isBackgroundMusicEnabled
ApplicationSettings.isBackgroundMusicEnabled = false
}
onStop: ApplicationSettings.isBackgroundMusicEnabled = backgroundMusicStatus
}
......@@ -29,4 +29,10 @@ FindIt {
mode: "Instruments"
dataset: Dataset
backgroundImg: "qrc:/gcompris/src/activities/instruments/resource/background.svg"
property bool backgroundMusicStatus
onStart: {
backgroundMusicStatus = ApplicationSettings.isBackgroundMusicEnabled
ApplicationSettings.isBackgroundMusicEnabled = false
}
onStop: ApplicationSettings.isBackgroundMusicEnabled = backgroundMusicStatus
}
......@@ -59,6 +59,8 @@ ActivityBase {
}
onStart: {
items.backgroundMusicStatus = ApplicationSettings.isBackgroundMusicEnabled
ApplicationSettings.isBackgroundMusicEnabled = false
bar.level = 1
score.numberOfSubLevels = 5
score.currentSubLevel = 1
......
......@@ -27,5 +27,12 @@ import "../memory-sound/memorysounddataset.js" as Dataset
MemoryCommon {
dataset: Dataset.get()
backgroundImg: "qrc:/gcompris/src/activities/memory-sound/resource/gcompris_band.svg"
property bool backgroundMusicStatus
onStart: {
backgroundMusicStatus = ApplicationSettings.isBackgroundMusicEnabled
ApplicationSettings.isBackgroundMusicEnabled = false
}
onStop: ApplicationSettings.isBackgroundMusicEnabled = backgroundMusicStatus
withTux: true
}
......@@ -28,4 +28,10 @@ import "memorysounddataset.js" as Dataset
MemoryCommon {
dataset: Dataset.get()
backgroundImg: "qrc:/gcompris/src/activities/memory-sound/resource/gcompris_band.svg"
property bool backgroundMusicStatus
onStart: {
backgroundMusicStatus = ApplicationSettings.isBackgroundMusicEnabled
ApplicationSettings.isBackgroundMusicEnabled = false
}
onStop: ApplicationSettings.isBackgroundMusicEnabled = backgroundMusicStatus
}
......@@ -212,6 +212,15 @@ Item {
}
}
GCDialogCheckBox {
id: enableBackgroundMusicBox
text: qsTr("Enable background music")
checked: isBackgroundMusicEnabled
onCheckedChanged: {
isBackgroundMusicEnabled = checked;
}
}
GCDialogCheckBox {
id: enableFullscreenBox
text: qsTr("Fullscreen")
......@@ -520,6 +529,7 @@ Item {
property bool showLockedActivities: ApplicationSettings.showLockedActivities
property bool isAudioVoicesEnabled: ApplicationSettings.isAudioVoicesEnabled
property bool isAudioEffectsEnabled: ApplicationSettings.isAudioEffectsEnabled
property bool isBackgroundMusicEnabled: ApplicationSettings.isBackgroundMusicEnabled
property bool isFullscreen: ApplicationSettings.isFullscreen
property bool isVirtualKeyboard: ApplicationSettings.isVirtualKeyboard
property bool isAutomaticDownloadsEnabled: ApplicationSettings.isAutomaticDownloadsEnabled
......@@ -538,6 +548,9 @@ Item {
isAudioEffectsEnabled = ApplicationSettings.isAudioEffectsEnabled
enableAudioEffectsBox.checked = isAudioEffectsEnabled
isBackgroundMusicEnabled = ApplicationSettings.isBackgroundMusicEnabled
enableBackgroundMusicBox.checked = isBackgroundMusicEnabled
isFullscreen = ApplicationSettings.isFullscreen
enableFullscreenBox.checked = isFullscreen
......@@ -585,12 +598,12 @@ Item {
ApplicationSettings.showLockedActivities = showLockedActivities
ApplicationSettings.isAudioVoicesEnabled = isAudioVoicesEnabled
ApplicationSettings.isAudioEffectsEnabled = isAudioEffectsEnabled
ApplicationSettings.isBackgroundMusicEnabled = isBackgroundMusicEnabled
ApplicationSettings.isFullscreen = isFullscreen
ApplicationSettings.isVirtualKeyboard = isVirtualKeyboard
ApplicationSettings.isAutomaticDownloadsEnabled = isAutomaticDownloadsEnabled
ApplicationSettings.sectionVisible = sectionVisible
ApplicationSettings.wordset = wordset
ApplicationSettings.isEmbeddedFont = fonts.get(fontBox.currentIndex).isLocalResource;
ApplicationSettings.font = fonts.get(fontBox.currentIndex).text
ApplicationSettings.fontCapitalization = fontCapitalizationModel[fontCapitalizationBox.currentIndex].value
......@@ -656,7 +669,36 @@ Item {
}
}
}
}
// download backgroundMusic.rcc if needed
if(DownloadManager.isDataRegistered("backgroundMusic")) {
// we either have it, we try to update in the background
// or we are downloading it
if(DownloadManager.haveLocalResource(DownloadManager.getBackgroundMusicResources()))
DownloadManager.updateResource(DownloadManager.getBackgroundMusicResources())
}
else {
// download automatically if automatic download else ask for download
if(isAutomaticDownloadsEnabled) {
var prevAutomaticDownload = ApplicationSettings.isAutomaticDownloadsEnabled
ApplicationSettings.isAutomaticDownloadsEnabled = true;
DownloadManager.updateResource(DownloadManager.getBackgroundMusicResources());
ApplicationSettings.isAutomaticDownloadsEnabled = prevAutomaticDownload
}
else {
Core.showMessageDialog(main,
qsTr("The background music is not yet installed. ")
+ qsTr("Do you want to download it now?"),
qsTr("Yes"),
function() {
if (DownloadManager.downloadResource(DownloadManager.getBackgroundMusicResources()))
var downloadDialog = Core.showDownloadDialog(pageView.currentItem, {});
},
qsTr("No"),null
);
}
}
}
ListModel {
id: fonts
......@@ -719,6 +761,7 @@ Item {
(ApplicationSettings.fontLetterSpacing != fontLetterSpacing) ||
(ApplicationSettings.isAudioVoicesEnabled != isAudioVoicesEnabled) ||
(ApplicationSettings.isAudioEffectsEnabled != isAudioEffectsEnabled) ||
(ApplicationSettings.isBackgroundMusicEnabled != isBackgroundMusicEnabled) ||
(ApplicationSettings.isFullscreen != isFullscreen) ||
(ApplicationSettings.isVirtualKeyboard != isVirtualKeyboard) ||
(ApplicationSettings.isAutomaticDownloadsEnabled != isAutomaticDownloadsEnabled) ||
......
......@@ -113,6 +113,14 @@ Item {
*/
property GCSfx audioEffects
/**
* type:GCAudio
* The global audio item for background music.
*
* @sa GCAudio backgroundMusic
*/
property GCAudio backgroundMusic
/**
* type:Loading
* The global loading object.
......@@ -201,7 +209,8 @@ Item {
// Ctrl+M toggle sound
// We mute / unmute both channels in sync
ApplicationSettings.isAudioVoicesEnabled = !ApplicationSettings.isAudioVoicesEnabled
ApplicationSettings.isAudioEffectsEnabled = !ApplicationSettings.isAudioEffectsEnabled
ApplicationSettings.isAudioEffectsEnabled = ApplicationSettings.isAudioVoicesEnabled
ApplicationSettings.isBackgroundMusicEnabled = ApplicationSettings.isAudioVoicesEnabled
} else if (event.modifiers === Qt.ControlModifier &&
event.key === Qt.Key_W) {
// Ctrl+W exit the current activity
......
......@@ -182,6 +182,13 @@ QStringList ApplicationInfo::getFontsFromRcc()
return m_fontsFromRcc;
}
QStringList ApplicationInfo::getBackgroundMusicFromRcc()
{
const QStringList backgroundMusicFilters = { QString("*.%1").arg(COMPRESSED_AUDIO) };
m_backgroundMusicFromRcc = QDir(":/gcompris/data/backgroundMusic").entryList(backgroundMusicFilters);
return m_backgroundMusicFromRcc;
}
void ApplicationInfo::notifyPortraitMode()
{
int width = qApp->primaryScreen()->geometry().width();
......
......@@ -396,7 +396,10 @@ public slots:
* @returns A list of fonts contained in the fonts resources.
*/
Q_INVOKABLE QStringList getFontsFromRcc();
/**
* @returns A list of background music contained in the background music resources.
*/
Q_INVOKABLE QStringList getBackgroundMusicFromRcc();
/**
* Stores a screenshot in the passed @p file.
*
......@@ -434,6 +437,7 @@ private:
// Symbols fonts that user can't see
QStringList m_excludedFonts;
QStringList m_fontsFromRcc;
QStringList m_backgroundMusicFromRcc;
static QQuickWindow *m_window;
};
......
......@@ -55,6 +55,10 @@ static const char *FONT_KEY = "font";
static const char *IS_CURRENT_FONT_EMBEDDED = "isCurrentFontEmbedded";
static const char *ENABLE_AUTOMATIC_DOWNLOADS = "enableAutomaticDownloads";
static const char *FILTERED_BACKGROUND_MUSIC_KEY = "filteredBackgroundMusic";
static const char *BACKGROUND_MUSIC_VOLUME_KEY = "backgroundMusicVolume";
static const char *AUDIO_EFFECTS_VOLUME_KEY = "audioEffectsVolume";
static const char *DOWNLOAD_SERVER_URL_KEY = "downloadServerUrl";
static const char *CACHE_PATH_KEY = "cachePath";
static const char *USERDATA_PATH_KEY = "userDataPath";
......@@ -95,6 +99,7 @@ ApplicationSettings::ApplicationSettings(const QString &configPath, QObject *par
// general group
m_config.beginGroup(GENERAL_GROUP_KEY);
m_isAudioEffectsEnabled = m_config.value(ENABLE_AUDIO_EFFECTS_KEY, true).toBool();
m_isBackgroundMusicEnabled = m_config.value(ENABLE_BACKGROUND_MUSIC_KEY, true).toBool();
m_isFullscreen = m_config.value(FULLSCREEN_KEY, true).toBool();
m_previousHeight = m_config.value(PREVIOUS_HEIGHT_KEY, screenSize.height()).toUInt();
m_previousWidth = m_config.value(PREVIOUS_WIDTH_KEY, screenSize.width()).toUInt();
......@@ -169,9 +174,10 @@ ApplicationSettings::ApplicationSettings(const QString &configPath, QObject *par
m_isBarHidden = false;
connect(this, &ApplicationSettings::showLockedActivitiesChanged, this, &ApplicationSettings::notifyShowLockedActivitiesChanged);
connect(this, &ApplicationSettings::audioVoicesEnabledChanged, this, &ApplicationSettings::notifyAudioVoicesEnabledChanged);
connect(this, &ApplicationSettings::audioEffectsEnabledChanged, this, &ApplicationSettings::notifyAudioEffectsEnabledChanged);
connect(this, &ApplicationSettings::fullscreenChanged, this, &ApplicationSettings::notifyFullscreenChanged);
connect(this, &ApplicationSettings::audioVoicesEnabledChanged, this, &ApplicationSettings::notifyAudioVoicesEnabledChanged);
connect(this, &ApplicationSettings::audioEffectsEnabledChanged, this, &ApplicationSettings::notifyAudioEffectsEnabledChanged);
connect(this, &ApplicationSettings::backgroundMusicEnabledChanged, this, &ApplicationSettings::notifyBackgroundMusicEnabledChanged);
connect(this, &ApplicationSettings::fullscreenChanged, this, &ApplicationSettings::notifyFullscreenChanged);
connect(this, &ApplicationSettings::previousHeightChanged, this, &ApplicationSettings::notifyPreviousHeightChanged);
connect(this, &ApplicationSettings::previousWidthChanged, this, &ApplicationSettings::notifyPreviousWidthChanged);
connect(this, &ApplicationSettings::localeChanged, this, &ApplicationSettings::notifyLocaleChanged);
......@@ -201,6 +207,7 @@ ApplicationSettings::~ApplicationSettings()
m_config.beginGroup(GENERAL_GROUP_KEY);
m_config.setValue(SHOW_LOCKED_ACTIVITIES_KEY, m_showLockedActivities);
m_config.setValue(ENABLE_AUDIO_VOICES_KEY, m_isAudioVoicesEnabled);
m_config.setValue(ENABLE_BACKGROUND_MUSIC_KEY, m_isBackgroundMusicEnabled);
m_config.setValue(LOCALE_KEY, m_locale);
m_config.setValue(FONT_KEY, m_font);
m_config.setValue(IS_CURRENT_FONT_EMBEDDED, m_isEmbeddedFont);
......@@ -260,6 +267,12 @@ void ApplicationSettings::notifyAudioEffectsEnabledChanged()
qDebug() << "notifyAudioEffects: " << m_isAudioEffectsEnabled;
}
void ApplicationSettings::notifyBackgroundMusicEnabledChanged()
{
updateValueInConfig(GENERAL_GROUP_KEY, ENABLE_BACKGROUND_MUSIC_KEY, m_isBackgroundMusicEnabled);
qDebug() << "notifyBackgroundMusic: " << m_isBackgroundMusicEnabled;
}
void ApplicationSettings::notifyLocaleChanged()
{
updateValueInConfig(GENERAL_GROUP_KEY, LOCALE_KEY, m_locale);
......@@ -323,7 +336,6 @@ void ApplicationSettings::setIsAutomaticDownloadsEnabled(const bool newIsAutomat
emit automaticDownloadsEnabledChanged();
}
}
void ApplicationSettings::notifyAutomaticDownloadsEnabledChanged()
{
updateValueInConfig(GENERAL_GROUP_KEY, ENABLE_AUTOMATIC_DOWNLOADS, m_isAutomaticDownloadsEnabled);
......
......@@ -81,7 +81,11 @@ class ApplicationSettings : public QObject
/**
* Whether audio effects should be enabled.
*/
Q_PROPERTY(bool isAudioEffectsEnabled READ isAudioEffectsEnabled WRITE setIsAudioEffectsEnabled NOTIFY audioEffectsEnabledChanged)
Q_PROPERTY(bool isAudioEffectsEnabled READ isAudioEffectsEnabled WRITE setIsAudioEffectsEnabled NOTIFY audioEffectsEnabledChanged)
/** Whether background music should be enabled.
*/
Q_PROPERTY(bool isBackgroundMusicEnabled READ isBackgroundMusicEnabled WRITE setIsBackgroundMusicEnabled NOTIFY backgroundMusicEnabledChanged)
/**
* Whether GCompris should run in fullscreen mode.
......@@ -302,6 +306,12 @@ public:
emit audioEffectsEnabledChanged();
}
bool isBackgroundMusicEnabled() const { return m_isBackgroundMusicEnabled; }
void setIsBackgroundMusicEnabled(const bool newMode) {
m_isBackgroundMusicEnabled = newMode;
emit backgroundMusicEnabledChanged();
}
bool isFullscreen() const { return m_isFullscreen; }
void setFullscreen(const bool newMode) {
if(m_isFullscreen != newMode) {
......@@ -500,6 +510,7 @@ protected slots:
Q_INVOKABLE void notifyShowLockedActivitiesChanged();
Q_INVOKABLE void notifyAudioVoicesEnabledChanged();
Q_INVOKABLE void notifyAudioEffectsEnabledChanged();
Q_INVOKABLE void notifyBackgroundMusicEnabledChanged();
Q_INVOKABLE void notifyFullscreenChanged();
Q_INVOKABLE void notifyPreviousHeightChanged();
Q_INVOKABLE void notifyPreviousWidthChanged();
......@@ -570,6 +581,7 @@ signals:
void showLockedActivitiesChanged();
void audioVoicesEnabledChanged();
void audioEffectsEnabledChanged();
void backgroundMusicEnabledChanged();
void fullscreenChanged();
void previousHeightChanged();
void previousWidthChanged();
......@@ -610,6 +622,7 @@ private:
bool m_showLockedActivities;
bool m_isAudioVoicesEnabled;
bool m_isAudioEffectsEnabled;
bool m_isBackgroundMusicEnabled;
bool m_isFullscreen;
quint32 m_previousHeight;
quint32 m_previousWidth;
......
......@@ -112,6 +112,11 @@ QString DownloadManager::getVoicesResourceForLocale(const QString& locale) const
.arg(ApplicationInfo::getInstance()->getVoicesLocale(locale));
}
QString DownloadManager::getBackgroundMusicResources() const
{
return QString("data2/backgroundMusic/backgroundMusic-" COMPRESSED_AUDIO ".rcc");
}
inline QString DownloadManager::getAbsoluteResourcePath(const QString& path) const
{
for (const QString &base : getSystemResourcePaths()) {
......@@ -423,9 +428,13 @@ bool DownloadManager::registerResourceAbsolute(const QString& filename)
emit resourceRegistered(getRelativeResourcePath(filename));
QString v = getVoicesResourceForLocale(
ApplicationSettings::getInstance()->locale());
ApplicationSettings::getInstance()->locale());
QString musicPath = getBackgroundMusicResources();
if (v == getRelativeResourcePath(filename))
emit voicesRegistered();
else if(musicPath == getRelativeResourcePath(filename))
emit backgroundMusicRegistered();
return true;
}
......@@ -440,7 +449,6 @@ bool DownloadManager::registerResource(const QString& filename)
bool DownloadManager::isDataRegistered(const QString& data) const
{
QString res = QString(":/gcompris/data/%1").arg(data);
return !QDir(res).entryList().empty();
}
......@@ -448,7 +456,6 @@ bool DownloadManager::areVoicesRegistered() const
{
QString resource = QString("voices-" COMPRESSED_AUDIO "/%1").
arg(ApplicationInfo::getInstance()->getVoicesLocale(ApplicationSettings::getInstance()->locale()));
return isDataRegistered(resource);
}
......
......@@ -237,6 +237,8 @@ public:
*/
Q_INVOKABLE QString getVoicesResourceForLocale(const QString& locale) const;
// @returns A relative background music resource path.
Q_INVOKABLE QString getBackgroundMusicResources() const;
/**
* Checks whether the given relative resource @p path exists locally.
*
......@@ -377,6 +379,14 @@ signals:
* @sa resourceRegistered
*/
void voicesRegistered();
/** Emitted when background music has been registered.
*
* Convenience signal and special case of resourceRegistered.
*
* @sa resourceRegistered
*/
void backgroundMusicRegistered();
};
#endif /* DOWNLOADMANAGER_H */
......@@ -53,7 +53,8 @@ Window {
/// @cond INTERNAL_DOCS
property var applicationState: Qt.application.state
property bool isMusicalActivityRunning: false
property var rccBackgroundMusic: ApplicationInfo.getBackgroundMusicFromRcc()
property alias backgroundMusic: backgroundMusic
onApplicationStateChanged: {
if (ApplicationInfo.isMobile && applicationState != Qt.ApplicationActive) {
......@@ -61,9 +62,9 @@ Window {
audioEffects.stop();
}
}
onClosing: Core.quit(main)
GCAudio {
id: audioVoices
muted: !ApplicationSettings.isAudioVoicesEnabled
......@@ -86,7 +87,7 @@ Window {
Component.onCompleted: {
if(ApplicationSettings.isAudioEffectsEnabled)
audioVoices.append(ApplicationInfo.getAudioFilePath("qrc:/gcompris/src/core/resource/intro.$CA"))
append(ApplicationInfo.getAudioFilePath("qrc:/gcompris/src/core/resource/intro.$CA"))
if (DownloadManager.areVoicesRegistered())
delayedWelcomeTimer.playWelcome();
......@@ -103,6 +104,39 @@ Window {
muted: !ApplicationSettings.isAudioEffectsEnabled && !main.isMusicalActivityRunning
}
GCAudio {
id: backgroundMusic
muted: !ApplicationSettings.isBackgroundMusicEnabled
Timer {
id: delayedbackgroundMusic
interval: (ApplicationSettings.isAudioVoicesEnabled && !ApplicationSettings.isAudioEffectsEnabled) ? 2000 : 20000
repeat: false
onTriggered: {
delayedbackgroundMusic.playBackgroundMusic();
}
function playBackgroundMusic() {
rccBackgroundMusic = ApplicationInfo.getBackgroundMusicFromRcc()
for(var i = 0; i < rccBackgroundMusic.length; i++)
backgroundMusic.append(ApplicationInfo.getAudioFilePath("backgroundMusic/" + rccBackgroundMusic[i]))
}
}
Component.onCompleted: {
if(!ApplicationSettings.isAudioEffectsEnabled && !ApplicationSettings.isAudioVoicesEnabled) {
delayedbackgroundMusic.playBackgroundMusic()
}
else if(ApplicationSettings.isBackgroundMusicEnabled && DownloadManager.haveLocalResource(DownloadManager.getBackgroundMusicResources())) {
delayedbackgroundMusic.start()
}
else {
DownloadManager.backgroundMusicRegistered.connect(delayedbackgroundMusic.playBackgroundMusic)
delayedbackgroundMusic.start()
}
}
}
function playIntroVoice(name) {
name = name.split("/")[0]
audioVoices.play(ApplicationInfo.getAudioFilePath("voices-$CA/$LOCALE/intro/" + name + ".$CA"))
......@@ -140,6 +174,42 @@ Window {
);
}
}
function checkBackgroundMusic() {
var music = DownloadManager.getBackgroundMusicResources()
if(rccBackgroundMusic == '') {
rccBackgroundMusic = ApplicationInfo.getBackgroundMusicFromRcc()
}
if(music == '') {
music = DownloadManager.getBackgroundMusicResources()
}
// We have local music but it is not yet registered
else if(!DownloadManager.isDataRegistered("backgroundMusic") && DownloadManager.haveLocalResource(music)) {
// We have music and automatic download is enabled. Download the music and register it
if(DownloadManager.updateResource(music) && DownloadManager.downloadIsRunning()) {
DownloadManager.registerResource(music)
rccBackgroundMusic = Core.shuffle(ApplicationInfo.getBackgroundMusicFromRcc())
}
else {
rccBackgroundMusic = ApplicationInfo.getBackgroundMusicFromRcc()
}
}
else if(!DownloadManager.haveLocalResource(music)) {
Core.showMessageDialog(
main,
qsTr("The background music is not yet downloaded. ")
+ qsTr("Do you want to download it now?"),
qsTr("Yes"),
function() {
if(DownloadManager.downloadResource(DownloadManager.getBackgroundMusicResources())) {
var downloadDialog = Core.showDownloadDialog(pageView.currentItem, {});
}
},
qsTr("No"), null,
function() { pageView.currentItem.focus = true }
);
}
}
ChangeLog {
id: changelog
}
......@@ -179,6 +249,7 @@ Window {
function() {
pageView.currentItem.focus = true
checkWordset()
checkBackgroundMusic()
}
);
}
......@@ -189,9 +260,8 @@ Window {
DownloadManager.updateResource(
DownloadManager.getVoicesResourceForLocale(ApplicationSettings.locale));
}
checkWordset()
checkBackgroundMusic()
if(changelog.isNewerVersion(ApplicationSettings.lastGCVersionRan, ApplicationInfo.GCVersionCode)) {
// display log between ApplicationSettings.lastGCVersionRan and ApplicationInfo.GCVersionCode
Core.showMessageDialog(
......@@ -234,7 +304,7 @@ Window {
if(!properties.exitItem.isDialog && // if coming from menu and
!properties.enterItem.isDialog) // going into an activity then
playIntroVoice(properties.enterItem.activityInfo.name); // play intro
playIntroVoice(properties.enterItem.activityInfo.name); // play intro
if (!properties.exitItem.isDialog || // if coming from menu or
properties.enterItem.alwaysStart) // start signal enforced (for special case like transition from config-dialog to editor)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment