Articoli

Sound with C++

Sometimes happen that you need to insert in your programs some sound or simple music, to alert the user that some operations failed or succeed or stuff like this. You have two options: the first is to embed an audio file with the selected melody and to play it using some external library (for example irrKlang); the other is to compose by yourself the melody using the musical notes and the Windows Beep function (or analog function for *ix system).

The following program work only on Windows, and "play music" using the <windows.h> header and the windows' Beep function, that take as input two parameters: the first is the frequency of the note to play, and the second the time (in milliseconds) the note has to be played. The program is based on the following functions:

  1. LoadNotes(map<string, int>& note): load all the frequency' notes into a map container.

void LoadNotes(map<string, int>& note){
    note["C2"] = 66 ;
    note["C#2"] = 70 ;
    note["D2"] = 74 ;
    note["D#2"] = 78 ;
    note["E2"] = 83 ;
    note["F2"] = 88 ;
    note["F#2"] = 93 ;
    note["G2"] = 98 ;
    note["G#2"] = 104 ;
    //more notes...
}

  1. The melody you want to play. In this example I used the well know (at least in Italy) "Fra Martino Campanaro" melody, but it's simple to create your own melody writing down the notes in the following format:

    melodia[index] = "C6:500:10";

    Where:
    - index is the progressive notes' number
    - C6 is the note following this convention
    - 500 is the time in milliseconds the note will be played; use an integer number.
    - 10 is the waiting time before to sound the following note; use an integer number.

void LoadFraMartino(map<int,string>& melodia){
    melodia[1] = "C6:500:0";
    melodia[2] = "D6:500:0";
    melodia[3] = "E6:500:0";
    melodia[4] = "C6:500:0";
    melodia[5] = "C6:500:0";
    melodia[6] = "D6:500:0";
    melodia[7] = "E6:500:0";
    melodia[8] = "C6:500:0";
    //more notes...
}

  1. The SuonaMelodia(map<int, string>& melodia) that receive the melodia map's structure and play it. The code is following:

void SuonaMelodia(map<int, string>& melodia){

    //Dichiaro la mappa delle note
    //Declare notes' map
    map<string, int> note;
    LoadNotes(note);

    vector<string> vecTemp;
    vector< vector<string> > vecSplit;
    int intTemp;
    stringstream ss;

    //Splitto gli elementi della melodia
    //Split melody's elements
    for(multimap<int, string>::iterator it=melodia.begin();it!=melodia.end();++it){
        split((*it).second,":",vecTemp);
        vecSplit.push_back(vecTemp);
        vecTemp.clear();
    }

    //Suono la melodia
    //Play melody
    int i = 0;
    for(multimap<int, string>::iterator it=melodia.begin();it!=melodia.end();++it){
          cout << (*it).first << ": " << (*it).second << endl;
          // Converto il secondo parametro da stringa in numero
          // Convert 2-nd parameter from string to number
          ss << vecSplit[i][1];
          ss >> intTemp;
          // Suono la nota
          Beep(note[vecSplit[i][0]],intTemp);
          // Converto il terzo parametro da stringa in numero
          // Convert 3-nd parameter from string to number
          if(vecSplit[i][2]!= "0"){
              ss << vecSplit[i][2];
              ss >> intTemp;
              Sleep(intTemp);
          }
          // incremento il contatore di vecSplit
          // increment vecSplit's counter
          ++i;

    }

}

  1. The main() that put all together is:

int main()
{
    //Melodia
    map<int, string> melodia;
    LoadFraMartino(melodia);
    // Suono Melodia - Play melody
    SuonaMelodia(melodia);

    return 0;
}

Below it's possible to download all the working code.

Allegati:
FileDescrizioneDimensione del FileDownloadsCreato
Scarica questo file (C++Sound.zip)C++SoundSimple C++ consolle program to play simple melodies using the Windows beep function2 kB4242013-01-25 12:20

Aggiungi commento


Codice di sicurezza
Aggiorna

Copyright © 2017 Berta Danilo. Tutti i diritti riservati.
Joomla! è un software libero rilasciato sotto licenza GNU/GPL.