Just Intonation  Version 1.3.1 (19)
Explore key-independent dynamically adapting tuning in just intonation
startupsound.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2016-2017 Karolin Stange, Christoph Wick, and Haye Hinrichsen
3  *
4  * This file is part of JustIntonation.
5  *
6  * JustIntonation is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * JustIntonation is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with JustIntonation. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Class for playing a startup sound
22 //=============================================================================
23 
24 #include "startupsound.h"
25 
26 #include <QSettings>
27 #include <QStandardPaths>
28 #include <QFile>
29 #include <QDir>
30 #include <QResource>
31 
32 //-----------------------------------------------------------------------------
33 // Constructor
34 //-----------------------------------------------------------------------------
40 
41 StartupSound::StartupSound(const QString &resourcefile)
42  : mResourceFile(resourcefile)
43  , mPlayer()
44 {
45  // TODO: The following command avoids hanging in MSVC, no idea why
46  mPlayer.setMedia(QUrl());
47 }
48 
49 
50 //-----------------------------------------------------------------------------
51 // Ask whether startup sound is enabled
52 //-----------------------------------------------------------------------------
57 
59 {
60  QSettings settings;
61  return settings.value("app/startupsound",true).toBool();
62 }
63 
64 
65 //-----------------------------------------------------------------------------
66 // Public slot: Enable / disable startup sound
67 //-----------------------------------------------------------------------------
74 
75 void StartupSound::enable (bool enabled)
76 {
77  QSettings settings;
78  LOGSTATUS << "Setting enabled =" << (enabled ? "true" : "false");
79  settings.setValue("app/startupsound",enabled);
80 }
81 
82 
83 //-----------------------------------------------------------------------------
84 // Play startup sound
85 //-----------------------------------------------------------------------------
94 
95 void StartupSound::play(double volume)
96 {
97  if (not isEnabled())
98  {
99  LOGMESSAGE << "Startup sound is disabled in the settings";
100  return;
101  }
102  LOGMESSAGE << "Play startup sound" << mResourceFile;
103 
104  // Identify the extension of the file (mp3,ogg,...)
105  if (mResourceFile.size()==0) return;
106  int lastPoint = mResourceFile.lastIndexOf(".");
107  const QString extension =
108  mResourceFile.right(mResourceFile.size()-lastPoint-1);
109  if (extension.size() != 3)
110  {
111  LOGWARNING << "Could not identify extension of resource file"
112  << mResourceFile << ":" << extension;
113  return;
114  }
115 
116  // MAC-OS cannot play the startupsound directly from the resource file
117  // For this reason the mp3/ogg file is first copied to local disk
118  // First make sure that the local storage is writable
119  const QString localpath =
120  QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
121  QDir directory;
122  if (not directory.mkpath(localpath))
123  {
124  LOGWARNING << "Cannot create local path" << localpath;
125  return;
126  }
127 
128  // Then copy the file
129  const QString filename = "startupsound." + extension;
130  QFile localfile (localpath + "/" + filename);
131  if (localfile.exists())
132  {
133  if (localfile.size() != QResource(mResourceFile).size())
134  {
135  // If file size changed delete and reload
136  localfile.remove();
137  QFile::copy(mResourceFile, localfile.fileName());
138  }
139  }
140  else QFile::copy(mResourceFile, localfile.fileName());
141  if (not localfile.exists()) LOGWARNING<< "Startupsound could not be copied.";
142  LOGSTATUS << mResourceFile << "was copied to" << localfile.fileName();
143 
144  mPlayer.setMedia(QUrl::fromLocalFile(localfile.fileName()));
145  mPlayer.setVolume(static_cast<int>(100*volume));
146  mPlayer.play();
147  if (mPlayer.error() != QMediaPlayer::NoError)
148  LOGWARNING << "Startupsound:" << mPlayer.errorString().toStdString().c_str();
149  LOGSTATUS << "Leaving Startupsound::play()";
150 }
151 
QMediaPlayer mPlayer
Pointer to the player.
Definition: startupsound.h:59
QString mResourceFile
Resource file object.
Definition: startupsound.h:58
#define LOGMESSAGE
Definition: log.h:43
void play(double volume)
Play startup sound.
void enable(bool enabled)
Public slot: Enable / disable startup sound.
StartupSound(const QString &resourcefile)
Constructor.
bool isEnabled()
StartupSound::isEnabled.
#define LOGSTATUS
Definition: log.h:42
#define LOGWARNING
Definition: log.h:44