Greetings This note is aimed at novice programmers, the most that novice is. It is about the simple Audiere library. If you once wanted to bring the sound simply and quickly, then Audiere you most likely will like it!
To begin, I will tell you a little about what it is:
Audiere is an open source high-level audio API licensed under the LGPL. It can be used in many languages, including Java, C ++, Python, Delphi, and others. It is allowed to use in commercial applications, free of charge, if the source code is not affected (see the documentation for the library for details).
Prepare for the implementation of the simplest example in C ++:
')
- Download the library at the office (in my case - this is Win32 Binary Release)
- Traditionally, we transfer the “audiere.lib” file to the compiler's “lib” folder, and the “audiere.h” heading also to the compiler's “include” folder.
***paths to the compiler folder for CodeBlocks "Program Files (x86) / CodeBlocks / MinGW /" , for Microsoft Visual Studio - "Program Files (x86) / Microsoft Visual Studio / VC /"
- Create an example C ++ console project
- In the linker settings in the project add “audiere.lib”
- Do not forget to "audiere.dll" from the folder "bin" put next to the source code of our project, and then next to the exe-file of our program.
If completely newIf you are the third day, as you learned to display "cout <<", then I will say in advance, friend, just repeat what is written below, I will try to explain everything.
- You need to connect the header files:
#include <iostream> // "cout<<" #include "conio.h" // "getche()", "cin>>" #include "windows.h" // audiere #include "audiere.h" // using namespace std; using namespace audiere; //
int main() { setlocale(0, "Russian");
Now, comments are a bit more detailed:
AudioDevicePtr device = OpenDevice();
Here we create, according to the library concept, the
pointer device (not quite explicit, but still a pointer). OpenDevice () can be left without arguments, then the library itself will select the appropriate device.
Next, open our file to the stream:
OutputStreamPtr sound = OpenSound(device , "sound.wav" , false);
Opening our sound, you need to pass three arguments to OpenSound:
1. device - device where output will occur
2. "sound.wav" is our arbitrary sound.
3. false or true - we want our sound to be loaded into memory and reproduced (false) or, through a stream (for example, if it is a large file), reproduced without being fully loaded into memory (true). For soundtracks, it is necessary to “true”, otherwise the entire memory will be taken by sound.
sound->play();
We lose our sound! As you can see, everything is very simple. For sounds, you can set the volume:
sound->setVolume(1.0f);
You can make the sound repeat (looped):
sound->setRepeat(true);
Stop playing this stream:
sound->stop();
Find out if this stream is playing now:
sound->isPlaying();
Find out the current volume of this stream:
sound->getVolume();
Set the sound pan (left, center, right):
sound->setPan(-1.0);
Set the speed of sound playback (the pitch also changes):
sound->setPitchShift(1.0);
sound->getPitchShift();
In the header file “audiere.h” some other functions are also defined, such as “CreateTone ();”, “CreatePinkNoise ()” and others, including MIDI capabilities. The list of members of the “audiere” namespace, as well as a link to the online documentation
here .
Of course, we still need to add error handling to our program, then the source code will look like this:
AudioDevicePtr device = OpenDevice(); if(!device) { cout<<" AudioDevice. .."; getche(); return 0;
As well as processing the file opening error:
if(!sound){ cout<<" ! .."; getche(); return 0; }
Source code completely #include <iostream> // "cout<<" #include "conio.h" // "getche()" , "cin>>" #include "windows.h" // audiere #include "audiere.h" // using namespace std; using namespace audiere; // int main() { setlocale(0, "Russian"); // - cout<<" "<<endl; AudioDevicePtr device = OpenDevice(); // AudioDevice if(!device){ cout<<" AudioDevice. .."; getche(); return 0; // } OutputStreamPtr sound = OpenSound(device , "sound.wav" , false); // if(!sound){ cout<<" ! .."; getche(); return 0; } sound->play(); // getche(); // }
And finally, the program is more interesting: let's make a primitive musical instrument!
This time we will use the getch () method. Its difference is that it works the same as “getche ()”, but does not display the entered characters. But suddenly you got an interesting melody and you need to see what you pressed ?: you can use getche (), or you can write to get an array with getch (). I deliberately do not complicate the text for beginners, so as not to hide the simplicity of the code, so you can add the error check and everything else yourself.
The source code of the musical instrument:
#include <iostream> #include "conio.h" #include "windows.h" #include "audiere.h" using namespace std; using namespace audiere; int main() { setlocale(0, "Russian"); cout << " QWERTYUIO . X - " << endl; AudioDevicePtr device = OpenDevice(); OutputStreamPtr do1 = OpenSound(device , "do.wav" , false); // OutputStreamPtr re = OpenSound(device , "re.wav" , false); OutputStreamPtr mi = OpenSound(device , "mi.wav" , false); OutputStreamPtr fa = OpenSound(device , "fa.wav" , false); OutputStreamPtr sol = OpenSound(device , "sol.wav" , false); OutputStreamPtr la = OpenSound(device , "la.wav" , false); OutputStreamPtr si = OpenSound(device , "si.wav" , false); OutputStreamPtr do2 = OpenSound(device , "do2.wav" , false); // - , char notePlay = '1'; // while(notePlay!='x') // , "x" { notePlay=getch(); if(notePlay=='q') do1->play(); else if(notePlay=='w') re->play(); else if(notePlay=='e') mi->play(); else if(notePlay=='r') fa->play(); else if(notePlay=='t') sol->play(); else if(notePlay=='y') la->play(); else if(notePlay=='u') si->play(); else if(notePlay=='i') do2->play(); } return 0; }
Files * .wav for this project can be downloaded
here . Unzip them into the source folder.
And this is for completely newbies.In conio.h, there is a good alternative to the cin >> method - this is getche (). “Getche ()” does not wait for the “Enter” command to be entered, but assigns the value to a variable immediately after pressing the key:
char inputVar; inputVar=getche();
But as you can see, you can only press the key once, so this method is best used for arrays in cycles:
char inputVar[30];
"! inputVar [i] == '\ r'" is a test of pressing “Enter”, if it is pressed - the cycle will end. '\ r' is a sign of carriage return.
And now, why in the code "getche ()" is so lonely and not assigned to any variable: we do not need it. You just need to wait for any key to be pressed, after which the program ends.
With the help of “setlocale (0,“ Russian “);” the support for outputting Russian text to the console (but not input) is set.
The following two lines will be clearer after a longer acquaintance with C ++, for example, from the book
Herbert Shildt - "Complete Reference for C ++"
Since the “sound” is a pointer, the methods for it will be performed using the arrow operator "->", and not the points; how could it be: "sound.play ()". This, in the aforementioned book, is devoted to the chapter "Pointers".
Comment to the
“if else” construct:
For all the following checks, we used
“else if” , which gives a speed increase for the program, because after checking each value and its correspondence to the key pressed, the other else if statements will not be executed. If we recorded:
while(notePlay!='x')
despite the fact that, for example, the 'q' key pressed - all other conditions would still be checked, which is meaningless in this case.
I wish you success in learning!
Let this short review be a start for newcomers to their designs. All the best!