0
0

I am quite new to c-programming, Windows programming and sound programming, and I have a problem with breaking the pause of the sound in Windows API. The code is as following:

[code:14nebdl2]
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
FMOD_SYSTEM *system;
FMOD_SOUND *sound;
FMOD_CHANNEL *channel = 0;
FMOD_RESULT result;
//int key;
unsigned int version;

switch(msg)
{
case WM_CREATE:
{

    result = FMOD_System_Create(&system);
    ERRCHECK(result);

    result = FMOD_System_GetVersion(system, &version);
    ERRCHECK(result);

    result = FMOD_System_Init(system, 1, FMOD_INIT_NORMAL, NULL);
    ERRCHECK(result);

    result = FMOD_System_CreateStream(system, "dilruba.wav", FMOD_HARDWARE | FMOD_LOOP_NORMAL | FMOD_2D, 0, &sound);
    ERRCHECK(result);

    result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, 1, &channel);
    ERRCHECK(result);

    //FMOD_Channel_SetPaused(channel, 0);     

    CreateWindow(TEXT("button"), TEXT("Sa"),    
                 WS_VISIBLE | WS_CHILD ,
                 20, 50, 80, 25,        
                 hwnd, (HMENU) 1, NULL, NULL);    

    CreateWindow(TEXT("button"), TEXT("Quit"),    
                 WS_VISIBLE | WS_CHILD ,
                 240, 50, 80, 25,        
                 hwnd, (HMENU) 2, NULL, NULL);    
    break;
}

  case WM_COMMAND:
  {

        if (LOWORD(wParam) == 1) {
                    FMOD_Channel_SetPaused(channel, 0);               
                    break;
        }

        if (LOWORD(wParam) == 2) {
                        PostQuitMessage(0);
                break;
        }

   }

  case WM_DESTROY:
  {
     PostQuitMessage(0);
     break;
  }

}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
[/code:14nebdl2]

I set the PlaySound function to pause, but when I want to remove the pause with button 1 it doesn’t work, I get no sound, and I have no idea why. If I use the SetPaused function right after the playsound function, however, I get sound. If someone could help me with this, it would be greatly appreciated.

  • You must to post comments
0
0

Hi ,
The issue is that you are not storing the value of ‘channel’ anywhere. Note that after create, the WndProc function exits.
When it comes back in, it is reset to 0, so when WM_COMMAND is entered, channel = 0.

You could make it work quick and dirty and make channel ‘static’, or make it a global variable. When you write more modular code, you should store variables like these in classes or state structures or pass them around, keep them managed somewhere. Otherwise your code could get very hard to read with a mountain of global variables.

  • You must to post comments
0
0

Thank you very much. Now I can make it work. There is, however, one thing I find puzzling in relation to this. When I set "FMOD_CHANNEL *channel = 0;" as a global variable it is okay. But if I do the same with "FMOD_SYSTEM *system;" I get a lot of compiling errors, like "differs in levels of indirection from ‘int (__cdecl *)(const char *)’" and "different types for formal and actual parameter 1." Could you answer this too?

Best wishes

Keitel

  • You must to post comments
0
0

Hm sounds a bit like you made a typo possibly, you’ll have to post your code again. use [code][/code] tags in the forum btw its easier to read.

  • You must to post comments
0
0

I don’t think it is a typo, because I have just copied the definitions. Here is a short version of the code:

[code:2i4i2ux6]

include <windows.h>

include <stdio.h>

include <conio.h>

include "fmod.h"

include "fmod_errors.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

FMOD_CHANNEL *channel = 0;
float frequency;
float fr = 44100 * 1.33;
float scale[5];
FMOD_RESULT result;
unsigned int version;

FMOD_SYSTEM *system;
FMOD_SOUND *sound;

void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{

MSG msg ;
WNDCLASS wc = {0};

wc.lpszClassName = TEXT( "BrahmaNada" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0, IDC_ARROW);

RegisterClass(&wc);
CreateWindow( wc.lpszClassName, TEXT("BrahmaNada"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
450, 50, 600, 600, 0, 0, hInstance, 0);

while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{

// FMOD_SYSTEM *system;
//FMOD_SOUND *sound;

    scale[0] = (float) 18/17;
    scale[1] = (float) 20/17;
    scale[2] = (float) 4/3;
    scale[3] = (float) 3/2;
    scale[4] = (float) 27/17;
    scale[5] = (float) 37/21;

switch(msg)
{
case WM_CREATE:
{

    result = FMOD_System_Create(&amp;system);
    ERRCHECK(result);

    result = FMOD_System_GetVersion(system, &amp;version);
    ERRCHECK(result);

    result = FMOD_System_Init(system, 1, FMOD_INIT_NORMAL, NULL);
    ERRCHECK(result);

    result = FMOD_System_CreateStream(system, &quot;g-h-loop.wav&quot;, FMOD_HARDWARE | FMOD_LOOP_NORMAL | FMOD_2D, 0, &amp;sound);
    ERRCHECK(result);

    result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, 1, &amp;channel);
    ERRCHECK(result);


    CreateWindow(TEXT(&quot;button&quot;), TEXT(&quot;S&quot;),    
                 WS_VISIBLE | WS_CHILD ,
                 100, 50, 50, 225,        
                 hwnd, (HMENU) 1, NULL, NULL); 

    CreateWindow(TEXT(&quot;button&quot;), TEXT(&quot;R&quot;),    
                 WS_VISIBLE | WS_CHILD ,
                 155, 50, 50, 225,        
                 hwnd, (HMENU) 2, NULL, NULL);   

    CreateWindow(TEXT(&quot;button&quot;), TEXT(&quot;G&quot;),    
                 WS_VISIBLE | WS_CHILD ,
                 210, 50, 50, 225,        
                 hwnd, (HMENU) 3, NULL, NULL);    

            break;

}

  case WM_COMMAND:
  {

        if (LOWORD(wParam) == 1) {
                FMOD_Channel_SetPaused(channel, 1);    
                frequency = fr;
                FMOD_Channel_SetFrequency(channel, frequency);
                ERRCHECK(result);
                FMOD_Channel_SetPaused(channel, 0);               
        }

        if (LOWORD(wParam) == 2) {
                FMOD_Channel_SetPaused(channel, 1);
                frequency = fr * scale[0];
                FMOD_Channel_SetFrequency(channel, frequency);
                FMOD_Channel_SetPaused(channel, 0);
                ERRCHECK(result);
        }

        if (LOWORD(wParam) == 3) {
                FMOD_Channel_SetPaused(channel, 1);
                frequency = fr * scale[1];
                FMOD_Channel_SetFrequency(channel, frequency);
                FMOD_Channel_SetPaused(channel, 0);
                ERRCHECK(result);
        }       



        break;
   }

  case WM_DESTROY:
  {
     PostQuitMessage(0);
     break;
  }

}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

[/code:2i4i2ux6]

When I set the
FMOD_SYSTEM *system;
FMOD_SOUND *sound;

in the LRESULT CALLBACK WndProc its okay. But when I set them as global variables, as in this example, I get the compiler errors.

Best wishes
Keitel

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.