-1

I made a program that downloads sound and then plays it with PlaySound function.

My problem is that the compiler can only play wav files, and usually this such of files weigh a lot and take a lot of time to download.

And if the song is 30 seconds, when the song ends there is a delay for one second, and then it start again. and it is not fun to hear it, I want the song to be 5 minutes for example so the user does not hear the delay.

My 'nyan' cat sound is only 30 seconds, and weigh 25 MB.

Is there a way to play other file types ?

Or reduce the size of the file ?


My Function:

Void PlayNyanSound()
{
    // Nyan cat sound

    string dir = "C:\\Users\\" + username() + "\\Documents\\Dragonite";
    string dwnld_URL = "https://srv-file4.gofile.io/download/TyizCg/y2mate.com%20-%20nyan_cat_sound_effect_12_OAN-miWCDm4_360p.wav";
    string savepath = "C:\\Users\\" + username() + "\\Documents\\Dragonite\\nyan.wav";

    CreateDirectory(dir.c_str(), NULL);

    URLDownloadToFile(NULL, dwnld_URL.c_str(), savepath.c_str(), 0, NULL);

    std::string pathtosound = "C:\\Users\\" + username() + "\\Documents\\Dragonite\\nyan.wav";
    while (true) {
        PlaySound(pathtosound.c_str(), 0, SND_SYNC);
    }

}

Edit:

With Strive's help, I came to this result. But I can't hear anything, it does not work.

#include <iostream>
#include <Windows.h>
#pragma comment (lib,"Winmm.lib")

using namespace std;

int main()
{

    while (true)
    {
        LPCSTR const Sound_File_Open = "open C:\\Users\\roeil\\Desktop\\nyan.mp3";

        MCIERROR open = mciSendString(Sound_File_Open, NULL, 0, NULL);

        MCIERROR play = mciSendString(Sound_File_Open, NULL, 0, NULL);
    }

    return 0;
}
nakE
  • 362
  • 1
  • 13
  • 1
    WAV is just a container format, what is important is the *codec* that is used to encode the audio samples within in. There are MANY audio codecs available, some more compressed than others (for instance, my company uses Microsoft's GSM, which is highly compressed without losing audio quality, we produce HOURS of audio that make small WAVs). `PlaySound()` can play any WAV file as long as there is an appropriate codec installed on the machine. – Remy Lebeau Dec 11 '19 at 19:35
  • @RemyLebeau Hey Remy, can you help me with compressing my wav file ? – nakE Dec 11 '19 at 20:03
  • there is really nothing to help with. Just be sure to select a codec that uses compression when creating your WAVs. Or, for existing WAVs, find a 3rd party tool that can re-encode them. – Remy Lebeau Dec 11 '19 at 20:09

2 Answers2

0

You might want to start digging into Windows Media Control Interface (MCI) if you want to play other media types. Microsoft also recommends it for playing larger files, which it sounds like you might want to do. Here's an example of playing a WAVE audio file with MCI.

There are other alternatives like XAudio2 with DirectX, but it can get really complicated really fast with a lot more code. A simpler solution would be to find an existing library and incorporate it into your project. Here's an article that lists some of your options. There are some open-source free solutions out there.

Whichever route you take, check to see if the solution supports streaming audio from file, meaning you're only loading and playing small chunks of an audio file at a given time. Loading in multiple audio files that span several megabytes is just taking up memory and can be a detriment to performance.

0

And if the song is 30 seconds, when the song ends there is a delay for one second, and then it start again. and it is not fun to hear it, I want the song to be 5 minutes for example so the user does not hear the delay.

This is the same as a case I saw before. Interestingly, they all want to eliminate the delay.

When using PlaySound(pathtosound.c_str(), 0, SND_ASYNC | SND_LOOP) to replay audio files, there will inevitably be a little delay, and the time is less than 1 second.

In my opinion, this delay seems inevitable. Unless you try to cut and copy the wav file from 30 seconds to 5 minutes with a third-party tool. And in this way, you can avoid repeating 30 seconds of audio.

Is there a way to play other file types ?

You can use mciSendString.

The mciSendString function sends a command string to an MCI device. The device that the command is sent to is specified in the command string.

mciSendString has many interesting functions. You can get commands from this document.

Example of using mciSendStringto play other audios, such as looping mp3.

#include <conio.h>
#include <Windows.h>
#include <iostream>

#pragma comment (lib,"Winmm.lib")
using namespace std;

int main() 
{

    LPCSTR const Sound_File_Open = "open C:\\Users\\xxx\\Desktop\\nyan-clipped.mp3 type mpegvideo alias Current_Sound_Command";

    MCIERROR open = mciSendString(Sound_File_Open, NULL, 0, NULL);

    MCIERROR play = mciSendString("play Current_Sound_Command repeat", NULL, 0,  NULL);

    system("pause");
}

More cases:

Or reduce the size of the file ?

A wav file is lossless, uncompressed audio.You should try a lossy format like mp3. A high quality mp3 file is often indistinguishable from a lossless file.

If it is absolutely crucial that the file must be a .wav, there is a way to reduce file size, although you will lose more quality in the process than you would by using a lossy audio codec.

Potential options for reducing filesize are:

  • Converting from stereo to mono
  • Lowering the audio sample rate
  • Lowering the audio bit depth

Here is an example of how to compress wav files.

Strive Sun
  • 5,988
  • 1
  • 9
  • 26