File: SoundEngine.cpp

package info (click to toggle)
qabcs 1.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 44,724 kB
  • sloc: cpp: 2,226; sh: 2,001; xml: 20; makefile: 5
file content (149 lines) | stat: -rw-r--r-- 4,040 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "SoundEngine.h"

#include <QApplication>
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QProcess>
#include <QDebug>

#include "config_qabcs.h"
#include "LoaderAbcFormats.h"
#include "sndplayer.h"

QMediaPlayer *playerBackground=nullptr;

SoundEngine::SoundEngine(){

}

void SoundEngine::init(){
    playerBackground = new QMediaPlayer();
}

void SoundEngine::playSoundFromFile(QString filename, bool async){
    if (!QFile::exists(filename)) return;
    QFileInfo fileInfo(filename);


#if defined(_WIN32)
    QCoreApplication::instance()->processEvents();

    if (!QFile(global_path_to_play.replace("\"","")).exists()){
        qDebug() << "[DEBUG] playerBackgroud " << fileInfo.absoluteFilePath();
        if (async){
            playerBackground->stop();
            playerBackground->setMedia(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
            playerBackground->play();
        }else{
            SndPlayer player(nullptr,fileInfo.absoluteFilePath());
            player.wait();
        }
        return;
    }
#endif


    QProcess proc;
    QString cmd_to_play = global_path_to_play+" "+fileInfo.absoluteFilePath();

    qDebug() << "[DEBUG] " << cmd_to_play << async;

#if defined(_WIN32)
    cmd_to_play=global_path_to_play+" \""+fileInfo.absoluteFilePath()+"\"  -t waveaudio";

    QStringList listEvn = proc.environment();
    listEvn.push_back("AUDIODEV=0");
    proc.setEnvironment(listEvn);

    QString workingDirectory = [](){
        QString path = global_path_to_play;
        path.replace("\"","");
        QStringList s = path.split(QRegExp("(/|\\\\)"));
        int lenFilename = s.at(s.size()-1).length();
        return path.mid(0,path.length()-lenFilename);
    }();


    proc.setWorkingDirectory(workingDirectory);
#endif

    qDebug() << "[DEBUG] play " << fileInfo.absoluteFilePath();
    if (async){
        proc.startDetached(cmd_to_play);
    }else{
        proc.execute(cmd_to_play);

    }


}

void SoundEngine::playSoundFromSpeechSynthesizer(QString cmdLine, bool async){
    if (cmdLine.isEmpty()) return;

    qDebug() << "[DEBUG] " << cmdLine;

    if (async){
        QProcess::startDetached(cmdLine);
    }else{
        QProcess::execute(cmdLine);
    }
}

QString SoundEngine::findSoundfile(QString folder,QString string){
    QStringList listExtensionFiles = QStringList() << "wav" << "ogg" << "mp3" << "flac";

    string = string.toLower();

    if (QFile::exists(string)) return string;
    if (QFile::exists(folder+"/"+string)) return folder+"/"+string;

    for (QString ext:listExtensionFiles){
        QString filename = string+"."+ext;
        if (QFile::exists(folder+"/"+filename)){
            return folder+"/"+filename;
        }
    }

    return "";
}



QString SoundEngine::findSoundFile(QString filename,QString soundFile,QString typeSearch){
    ABC_CONFIG config_current = LoaderAbcFormats::LoadFilename(filename);

    QString searchFolder="words";

    if (typeSearch=="letter"){
        searchFolder="alpha";
    }

    {
        QString folderWords = QString(GLOBAL_PATH_USERDATA)+"/abcs/"+config_current.folder_lang+"/sounds/"+searchFolder;
        QString filename = SoundEngine::findSoundfile(folderWords,soundFile.toLower().replace(" ","_"));
        if (!filename.isEmpty() and QFile::exists(filename)){
            return filename;
        }
    }

    {
        ABC_CONFIG config_inherits;
        if (!config_current.inheritsFrom.isEmpty()){
            config_inherits = LoaderAbcFormats::LoadFilename( QString(GLOBAL_PATH_USERDATA)+"/abcs/"+config_current.inheritsFrom);
        }
        if (!config_inherits.filename.isEmpty()){
            QString folderForSearch = QString(GLOBAL_PATH_USERDATA)+"/abcs/"+config_inherits.folder_lang+"/sounds/"+searchFolder;
            QString filename =  SoundEngine::findSoundfile(folderForSearch,soundFile.toLower().replace(" ","_"));
            if (!filename.isEmpty() and QFile::exists(filename)){
                return filename;
            }
        }
    }


    return "";
}