Salut,
J'essaie de créer un programme de reconnaissance musicale ( voici le sujet principale ).
J'ai posté mon code à la fin de ce message.
J'ai deux soucis pour l'instant:
- l'un c'est de quitter le programme plus proprement que exit(1) dans la fonction void Window::changeTexte3() du fichier window.cpp
- l'autre c'est de, même si j'ai lu de la doc sur les signaux, de emmètre un signal avec un string à partir de ma boucle de reconnaissance musicale vers l'icone de mon programme pour afficher une infobulle du titre de la musique reconnue.
Je suis un peu perdu de savoir où mettre le connect(), où definir les fonction signal et comment bien emmètre le signal. En principe le signal devrait être émit dans la fonction run(), à la fin de la boucle de capturing/analysing (à l'endoit ou j'ai écris : C'EST ICI QUE JE VEUX EMMETRE UN SIGNAL) du fichier RecognitionThread.cpp
la fonction qui devra être appelé pour afficher le message sera : sticon->showMessage("",message.c_str());
Je sais qu'après avoir codé le signal, je devrais faire un qmake
mon code :
main.cpp
window.hpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdlib.h> #include <windows.h> #include <tchar.h> #include <fstream> #include <string> #include <QSystemTrayIcon> #include <QWidget> #include <QMenu> #include <QLabel> #include <QApplication> #include <QThread> #include "window.hpp" #include "RecognitionThread.hpp" using namespace std; #undef main int main(int argc, char **argv) { QApplication app(argc, argv); Window w; RecognitionThread* T = new RecognitionThread(); w.hide(); return app.exec(); }
window.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #ifndef WINDOW_HPP #define WINDOW_HPP #include <QSystemTrayIcon> #include <QWidget> #include <QMenu> #include <QLabel> #include <QApplication> #include <QObject> class Window : public QWidget { Q_OBJECT public: Window(); ~Window(); QSystemTrayIcon* sticon; private slots: void changeTexte1(); void changeTexte2(); void changeTexte3(); }; #endif
RecognitionThread.hpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "window.hpp" #include <QSystemTrayIcon> #include <QWidget> #include <QMenu> #include <QLabel> #include <QApplication> Window::Window() { resize(0,0); // Builds icon sticon = new QSystemTrayIcon(this); QIcon icon("music.ico"); sticon->setIcon(icon); // Builds menu and menu items QMenu* stmenu = new QMenu(NULL); QAction* actTexte1 = new QAction("unknown",NULL); QAction* actTexte2 = new QAction("Capturing...",NULL); QAction* actTexte3 = new QAction("Quit",NULL); stmenu->addAction(actTexte1); stmenu->addAction(actTexte2); stmenu->addAction(actTexte3); sticon->setContextMenu(stmenu); sticon->show(); //sticon->showMessage("unknown","unknown"); connect(actTexte1, SIGNAL(triggered()), this, SLOT(changeTexte1())); connect(actTexte2, SIGNAL(triggered()), this, SLOT(changeTexte2())); connect(actTexte3, SIGNAL(triggered()), this, SLOT(changeTexte3())); } Window::~Window() { } void Window::changeTexte1() { } void Window::changeTexte2() { } void Window::changeTexte3() { exit(1); }
RecognitionThread.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #ifndef THREAD_HPP #define THREAD_HPP #include <stdlib.h> #include <windows.h> #include <tchar.h> #include <fstream> #include <string> #include <QThread> #include <QObject> using namespace std; class RecognitionThread : public QThread { Q_OBJECT public: RecognitionThread(QObject* parent = NULL); ~RecognitionThread(); void run(); }; #endif
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "RecognitionThread.hpp" #include "window.hpp" #include <stdlib.h> #include <windows.h> #include <tchar.h> #include <fstream> #include <string> #include <QThread> using namespace std; extern QSystemTrayIcon* sticon; RecognitionThread::RecognitionThread(QObject* parent):QThread(parent) { start(); } RecognitionThread::~RecognitionThread() { exit(); wait(); } void RecognitionThread::run() { // Following lines needed for CreateProcess(), startupinfo receive the output of the process run PROCESS_INFORMATION procinfo = {0}; STARTUPINFO startupinfo = {0}; ZeroMemory( &startupinfo, sizeof(startupinfo) ); startupinfo.cb = sizeof(startupinfo); startupinfo.dwFlags |=STARTF_USESTDHANDLES ; startupinfo.hStdInput=GetStdHandle(STD_INPUT_HANDLE); startupinfo.hStdError=GetStdHandle(STD_ERROR_HANDLE); SECURITY_ATTRIBUTES sa; ZeroMemory( &sa, sizeof(sa) ); sa.nLength=sizeof(sa); sa.bInheritHandle=TRUE; // Declaration of read and written files ofstream messageFile; ifstream infoFile; string message; string info; // Position of words found in info (string), string::npos if not found size_t found_song_id,found_artist_b1,found_artist_b2,found_artist_e,found_title_b1,found_title_b2,found_title_e; string artist,title; // Here, the loop begins start : CODE DE RECONNAISSANCE MUSICALE, QUI DURE ENVIRON 80 SECONDES // Open info.txt and copy content in info (string) infoFile.open("info.txt"); while(infoFile){ getline(infoFile, info); } infoFile.close(); // Looks for "song_id" found_song_id = info.find("song_id"); message = ""; if(found_song_id!=string::npos){//if "song_id" exists, the song has been recognized artist = ""; title = ""; // Gets the artist name found_artist_b1 = info.find("\"artist\":"); found_artist_b2 = found_artist_b1 + 11; found_artist_e = info.find("\"",found_artist_b2); artist = info.substr(found_artist_b2,found_artist_e-found_artist_b2); // Gets the title found_title_b1 = info.find("\"title\":"); found_title_b2 = found_title_b1 + 10; found_title_e = info.find("\"",found_title_b2); title = info.substr(found_title_b2,found_title_e-found_title_b2); // Puts "ARTIST - TITLE" in message (string) message += artist; message += " - "; message += title; } else{//if the song has not been recognized, puts "unknown" in message (string) message += "unknown"; } WinExec("del message.txt", SW_HIDE); // Sends a command to delete message.txt Sleep(500); messageFile.open ("message.txt"); messageFile << message; // Writes message (string) in message.txt messageFile.close(); // Shows message in a tooltip near the system tray C'EST ICI QUE JE VEUX EMMETRE UN SIGNAL CONTENANT 'message' (un string) goto start; // End of the loop }
Je vous remercie d'avance pour votre contribution.
Partager