Playing an .WAV file with (or without) Direct X

Weegee

Go Weegee!
Reaction score
102
Sup TH, I've been recently learning how to program in WINDOWS API and i've been having fun making menus, dialog boxes, etc. Recently, I've tried to put these skills to the test and make an application that opens up a song, plays it, then when it finishes, it allows the user to open up another song. Currently, I'm having troubles getting to the "playing" part as I cannot seem to find any documentation on how to do this >_>. I'm still a noob so don't expect me to be perfect in my coding ;D

Here's my main.cpp:
Code:
#include <Windows.h>
#include "resource.h"
 
const char g_szClassName[] = "myWindowClass";
char szFileName[MAX_PATH] = "";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // Creating OPEN FILE Dialog
    OPENFILENAME ofn;
 
    ZeroMemory(&ofn, sizeof(ofn));
 
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = "WAVE Files (*.wav)\0*.wav\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "wav";
 
    switch(msg)
    {
    case WM_CREATE:
        {
            HMENU hMenu, hSubMenu;
 
            hMenu = CreateMenu();
 
            hSubMenu = CreatePopupMenu();
            AppendMenu (hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
 
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_LOAD_MUSIC, "&Load");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Music");
 
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&Help");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&About");
 
            SetMenu(hwnd, hMenu);
        }
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case ID_FILE_EXIT:
                PostMessage(hwnd, WM_CLOSE, 0, 0);
                break;
            case ID_LOAD_MUSIC:
                if (GetOpenFileName(&ofn))
                {
                     //
                    // Want to play the song here
                   //
                }
                break;
        }
        case WM_LBUTTONDOWN:
            {
            }
 
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
 
    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style        = 0;
    wc.lpfnWndProc  = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance    = hInstance;
    wc.hIcon        = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    wc.hCursor      = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm        = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
 
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
 
    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Happy Fun Times",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);
 
    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

The code where I open the file has comments below saying "Want to play song here". It's closer to the top. Anywyas, hopefully I provided enough information. I know this is kind of vauge but I can't find any tutorials online so my explanation of what I want to do is to be expected to be pretty vaugue :p. +Rep inbound :D!
 

Weegee

Go Weegee!
Reaction score
102
BUMP :O

Are bumps even allowed on these forums anymore xD?

Does anyone know how to play the song with the location to the file that is obtained D:?

Edit: It seems playsound is useless when playing .wav files

Or any streamed audio in that case. Any suggestions TH.net? I Figured out how to find the correct path to the file but playsound doesn't do anything with it..
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I don't know much about the Win API to play any sound files, but this one works fine for me:

Code:
PlaySound(TEXT("filename.wav"), NULL, SND_FILENAME);

People say it might have problems playing larger wav files (1+ MB).
 

Weegee

Go Weegee!
Reaction score
102
I don't know much about the Win API to play any sound files, but this one works fine for me:

Code:
PlaySound(TEXT("filename.wav"), NULL, SND_FILENAME);

People say it might have problems playing larger wav files (1+ MB).

Thank you, the 1 mb rule wasn't being followed in my past attempts but when i used a smaller wave file it worked perfectly.

Now the question is, what are my options for playing a larger WAVE file? (+rep btw)

Edit: R.I.P. +Rep QQ
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Well, you'd be best off using an audio library. Working with sound files yourself can be quite nasty.
A quick googling turns up http://www.portaudio.com/ but there are a lot of free audio libraries. I don't know which ones are easy to use, or best to use. Haven't done much with sounds yet.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,710
Try obtaining DirectMusic, from Microsoft. They still have it up there for April 2007.

You need to send a message containing a byte buffer read from a WAV file. I think it sort of works with MIDI. I have a few source codes I can dig up, but they use MFC (using C++) instead of pure WinAPI in C.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top