OpenAL C++ on Linux - c++

I just need a simple program which allows me to play and stop an audio file. I'm guessing OpenAL is the way to go? All I need is this functionality - start audio file with spacebar and stop audio file with a second press of the spacebar. I notice the OpenAL documentation is quite involved. Can someone point me to something really simple in order to just get the start/stop functionality of a .wav file?

OpenAL may not be the simplest choice. If you use a gui-framework like QT, check for what their ecosystems provide for playing sound (e.g. qsound). Another choice may be Allegro which may feel more straightforward.
For OpenAL, there is a working example to play a wav in the example repositories. Playback can be paused using alSourcePause.
Edit:
For choosing libraries I like to consult Awesome-cpp. The simple_playback.c example of mini_al looks extremely straightforward. Simply use mal_device_stop(&device) to pause the running playback and mal_device_start(&device) to continue. Works perfectly on my machine and seems very portable.

Related

How do I play .wav files in C++ on Linux?

I have a question about playing .wav files in C++. I search for a method to play it on Linux and Windows and I want to break up it while a function has been ended, did you know a sample for this?
Thanks for any Answer :)!
I am from Germany, please don't be angry about my grammar or spelling :D.
There are several ways to do this.
The simplest, ugliest and most hackish way to do this is to write directly to your sound device located in /dev/snd/. However this really depends on your linux distribution and some modern ones no longer allows you to do this. In general, direct read / write to /dev/ devices is fading away. Here is a question answering this.
For a very long time, the "official" way was to use alsa library which uses a C style interface. It is usually pretty tricky, full of pitfalls and "workarounds" that depends on your exact audio hardware.
I think that alsa is getting gradually replaced by jack, which I hope is easier to use. I don't have any experience with this one.
The last method I know is with portaudio, which as the name implies, is somewhat portable between at least windows, linux and osx.
All of these library will allow you to control your audio hardware, init / setup / play. What is simple about wav files is that the content is raw pcm, which is usually the format used by those libraries. The wav format is usually like this :
[wav header] [audio data until the end of the file]
If you can manage a few milliseconds of garbage when you start playing, you can "safely" send the header for playback as well and avoid parsing it. You will have to understand PCM formats however and should bring some additional readings.
As an added "trick" which doesn't directly concern C++, I strongly suggest using Audacity. It is a small program which I see like the notepad / vim of audio files. It is small, it loads fast, allows you to copy / paste segments of audio and detect pcm formats. (Actually, you just change pcm settings until the audio "sounds" right, but still useful)
The title mentioned Linux, but then you mentioned Windows and Linux in the post.
For Linux, best is to use gstreamer if you insist on C++. Look through the gstreamer code for gst-launch. It is run as below in a Linux machine.
$ gst-launch filesrc location="location of file" ! wavparse ! alsasink
From, http://sanchayanmaity.github.io/beagleboard-xm/linux/programming/software/2014/08/07/playing-wavmp3-file-using-gstreamer-in-code.html
For windows, or if you want to use OS agnostic code on both Windows and Linux, you can use SDL,
http://lazyfoo.net/SDL_tutorials/lesson11/
Another alternative (cross-platform, Object oriented), is SFML. Check the audio wav file playback example at,
http://www.sfml-dev.org/tutorials/2.0/audio-sounds.php

Get Notes From Midi Playback

So, I've been working on a project that is going to require the playback of Midi. I have already done this with a little bit of code that looks like this:
mciSendString(L"play C:\\aha.mid", NULL, 0, NULL);
And that works perfectly fine for playing a Midi file.
But, what I also need to do is to get the note events from the playback of the midi file itself. I've seen a great deal of libraries that can help with playback or with reading the raw data of the midifile, but not both, and not both at the same time. I've tried Midifile, did some searching around the Juce library, but nothing seems to get the functionality that I need.
Is there something out there that can solve my problem or am I just thinking about this problem the wrong way? Maybe there's some way to get the current notes being played on the Microsoft wavetable (would be much easier if that's possible).

Cross-platform real-time MP3 recorder

I have an SDL app, that works under Linux, Mac and Windows. It's something like a media player, and can play audio just fine. I'd like to add audio recording feature to it, but I'd like to encode it in real time to MP3. Can anyone point me to an example how can I use LibLame, LibSoX, or possibly some other library to achieve this?
-- OR --
I'm also willing to rewrite the whole thing into something easier to manage than C++. I've looked at Kivy and Love2d which uses Lua, but audio recording it's still an issue there. If you know ANY toolkit that:
is cross platform
helps you build GUI using your own graphics
can play AND record mp3 files
ideally can operate under framebuffer (no X Window server under Linux)
Please let me know. I'm looking at Python + Pygame + Pyaudio, it can do graphics and output sound, but still can't record MP3's, only WAV's. Any way to integrate LAME into this to make it work?
FMOD can play practically anything, and handle audio input as well, although I don't know if integrating an entire audio engine is a bit overkill for your project.
It's free for non-commercial usage.
As for encoding, LAME is definitely the de-facto choice for MP3.
There's a very simple library called lame_enc.dll which wraps LAME's capabilities in a simple API. It's Windows only, but you could look at it's source for a good reference on how to use LAME.

Audiooutput problem in Qt using qmultimedia low level API

I'm trying to get mpg123 audio decoder to work with QT on windows. How do i play the decoded audio data at the right speed with Qmultimedia module in push mode. Currently i'm using simple timer to get it to play audio but it's not very efficient way to do it, if I do anything else at the same time audio get all distorted. Is there any better way to send the decoded data to audio output? It would be nice if anyone could point me to any nice examples using Qmultimedia module and Qaudiooutput class. I've tried to figure out QT example project "audiooutput" but it seems that it's also using timer to send audio to output in push mode.. Hope that I'm not too confusing.
I also had to figure that out and I would also suggest using the Phonon framework to do this.
It uses Windows Media Player as host on Windows, QuickTime on Mac and some KDE stuff on Linux.
So it's pretty platform independent.
If you need more low-level functionality, you should take a look into an open-source project called portaudio. It's very easy to use and you can manipulate or even fill buffers from code.
I used it to build an oscillator.
Hope that helps!
Best,
guitarflow

Audio Recording in C++

I was wondering, what was a good cross-platform utility for doing audio recording/ playback/ seeking in C++? I was thinking going the route of ALUT (OpenAL), but is there a better way? If not, do you guys know of any good tutorials/sample code for ALUT?
SFML and SDL have support for playing many different sound formats and are cross plattform. Neither of them provides you with means for recording audio. Then there is PortAudio which looks pretty active but I do have no experience with it at all.
Qt actually has some audio functions since version 4.6.
Didn't try the input for myself, but if you scroll down a bit in the Qt-Documentation
there is a basic example.
For Input you can work a layer higher with Qt. Here is an example.