Good day to all.

I would like to ask some help regarding C++ sounds. I read some codes in playing sounds using C++ and this codes was based on WINDOWS. I would like to as if there is a possibility that I can play a wav file in LINUX using C++. I am a begginer in C++ and I really need your help guys.


Thank you.

Hunterfish.

Dani AI

Generated

Short, practical answer for : on Linux you can either call an external player from C++ (quick, but crude) or embed a small audio stack in your program. For an easy in-process solution that handles most WAV files, use libsndfile to read/convert the WAV and libao to send raw PCM to the system sound drivers. This keeps the code short and avoids platform-specific APIs.

Example (reads PCM and plays it back):

#include <sndfile.h>
#include <ao/ao.h>
#include <iostream>

int main(int argc, char **argv) {
    if (argc < 2) { std::cerr << "Usage: " << argv[0] << " file.wav\n"; return 1; }

    SF_INFO sfinfo;
    SNDFILE *snd = sf_open(argv[1], SFM_READ, &sfinfo);
    if (!snd) { std::cerr << "Cannot open file\n"; return 1; }

    ao_initialize();
    ao_sample_format fmt;
    fmt.bits = 16;                  // typical WAV PCM
    fmt.channels = sfinfo.channels;
    fmt.rate = sfinfo.samplerate;
    fmt.byte_format = AO_FMT_NATIVE;
    fmt.matrix = 0;

    int driver = ao_default_driver_id();
    ao_device *dev = ao_open_live(driver, &fmt, NULL);
    if (!dev) { std::cerr << "Audio device open failed\n"; sf_close(snd); ao_shutdown(); return 1; }

    const int FRAMES = 1024;
    short *buf = new short[FRAMES * sfinfo.channels];
    int frames;
    while ((frames = sf_readf_short(snd, buf, FRAMES)) > 0) {
        ao_play(dev, (char*)buf, frames * sfinfo.channels * sizeof(short));
    }

    delete[] buf;
    ao_close(dev);
    sf_close(snd);
    ao_shutdown();
    return 0;
}

Compile:

g++ -o playwav playwav.cpp -lsndfile -lao

Notes and troubleshooting:

  • Install dev packages (Debian/Ubuntu): sudo apt-get install libsndfile1-dev libao-dev. Use equivalent packages for other distros.
  • If your WAV is 24-bit or float, read with sf_readf_float (or convert) and adjust fmt.bits and the buffer type.
  • If playback fails, check that PulseAudio/ALSA is running and that libao has an appropriate driver (ao_default_driver_id may be -1).
  • For cross-platform C++ with C++-style APIs, consider SFML (simple), OpenAL (games), PortAudio (low latency) or GStreamer (complex pipelines) depending on needs.

Comments on prior replies: was right that using a library is generally preferable to WinAPI calls; correctly pointed out the platform difference; and ’s hint about dedicated audio libraries is on the right track — the libsndfile+libao pair is a lightweight, beginner-friendly example you can extend.

Recommended Answers

All 6 Replies

#include <windows.h>
PlaySound("file.wav", NULL, SND_ASYNC | SND_FILENAME);

You didn't mention what compiler.

commented: Are you an idiot? windows.h on linux? -4

He said he has examples for Windows but what he wants is examples that work under Linux

Sorry there is SDL which can do what you want. You can use this to read about the API.

You can also create a shell script to play a wav file:

#!/bin/bash
mplayer (replace with your favorite media player) $1
chmod +x wavplayer
./wavplayer file.wav

Thank you. I really appreciate it but It doesn't work.Any other codes/scripts using C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.