Why is my SDL_mixer playing my wav. file too fast?
From this Lazy Foo tutorial (https://lazyfoo.net/tutorials/SDL/21_sound_effects_and_music/index.php) I wrote the following lines of code:
#include <SDL.h>
#include <SDL_mixer.h>
bool running = true;
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
SDL_Window* window = SDL_CreateWindow("testing musique", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event quit;
Mix_Music* music;
while (running) {
while(SDL_PollEvent(&quit)){
switch(quit.type) {
case SDL_QUIT:
running = false;
break;
}
}
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
music = Mix_LoadMUS("../pikachu/keypress_BMP/beat.wav");
Mix_PlayMusic(music, -1);
SDL_SetRenderDrawColor(renderer, 20, 20, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
Mix_FreeMusic(music);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
I even used the same audio file (beat.wav) as Lazy Foo did in his tutorial, but while his program runs without a hitch, mine plays the wav too fast (despite the fact that I checked every parameters to make sure mine matched with his). I tried decreasing the frequency parameter in Mix_OpenAudio, but while the wav did slow down, so did the pitch, and it should not have made sense to do this in the first place. What should I do?
Thank you.
do you know?
how many words do you know
See also questions close to this topic
-
C++ increment with macro
I have the following code and I want to use increment with macro:
#include <iostream> #define ABS(x) ((x) < 0 ? -(x) : (x)) int main(int argc, char** argv) { int x = 5; const int result = ABS(x++); std::cout << "R: " << result << std::endl; std::cout << "X: " << x << std::endl; return EXIT_SUCCESS; }
But output will be incorrect:
R: 6 X: 7
Is it possible to somehow use macros with an increment, or should this be abandoned altogether?
-
Can anyone pls tell me whats wrong with my code ? im stuck for the last 3 hours ,this question is bipartite graph in c++
idk why im getting error ,can someone help ? im trying to prove if a graph is bipartite or not in c++
bool isBipartite(vector<int> graph[],int V) { vector<int> vis(V,0); vector<int> color(V,-1); color[0]=1; queue <int> q; q.push(0); while (!q.empty()) { int temp = q.front(); q.pop(); for (int i=0;i<V;i++) { if (!vis[i] && color[i] == -1) "if there is an edge, and colour is not assigned" { color[i] = 1 - color[temp]; q.push(i); vis[i]=1; } else if (!vis[i] && color[i] == color[temp] "if there is an edge and both vertices have same colours" { vis[i]=1; return 0; // graph is not bipartite } } } return 1; }
it gives output "no" for whatever i enter
-
How to assign two or more values to a QMap Variable in Qt
I am getting confused of how to store the values assigned from 3 different functions and storing them in a single map variable
QMap<QString,QString> TrainMap = nullptr; if(......) ( TrainMap = PrevDayTrainMap(); TrainMap = NextDayTrainMap(); TrainMap = CurrentDayTrainMap(); }
The PrevDayTrainMap,NextDayTrainMap & CurrentDayTrainMap returns a set of values with Date and the TrainIdName.I need to store all the values from prevday,currentday and nextday in the TrainMap but it stores only the currentday values to the TrainMap as it is assigned at the last.I am not sure what to do so that it doesn't overwrite.If I should merge what is the way to do it?
-
Cannot include "SDL2_ttf" or "SDL2_image"
When i run code it had an error:
src/snake/Screen.hpp:7:10: fatal error: 'SDL_ttf.h' file not found #include "SDL_ttf.h" ^~~~~~~~~~~ 1 error generated. make: *** [all] Error 1
I included sdl2 library in my project and i only have problems with sdl2_image and sdl2_ttf.
This is my makefile:
SRC_DIR = src/snake BUILD_DIR = build/debug CC = g++ SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp) OBJ_NAME = play INCLUDE_PATHS = -Iinclude LIBRARY_PATHS = -Llib COMPILER_FLAGS = -std=c++11 -Wall -O0 -g LINKER_FLAGS = -lsdl2 -lsdl2_image -lsdl2_ttf all: $(CC) $(COMPILER_FLAGS) $(LINKER_FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(SRC_FILES) -o $(BUILD_DIR)/$(OBJ_NAME)
-
Code terminates at if( player == NULL ) in the loadMedia function
The code terminates once it reaches the if( player == NULL ) statement in the loadMedia function. No errors are given, just ends.
loadMedia function
bool loadMedia() { std::cout << "Loading media" << std::endl; player = SDL_LoadBMP( "player.bmp" ); if( player == NULL ) { std::cout << "Media could not be loaded " << static_cast< std::string >( SDL_GetError() ); return false; } return true; }
Entire code
//Johnathan Regha-Dodge: CS_version1 #include <iostream> #include <limits> #include "src/include/SDL2/SDL.h" //Screen dimensions const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; //Starts up SDL and creates Window bool init(); //Loads Media bool loadMedia(); //Close SDL window void close(); //window to render to SDL_Window* window = NULL; SDL_Surface *screen = NULL; SDL_Surface *player = NULL; SDL_Rect *playerPos; bool init() { std::cout << "Initializing" << std::endl; //SDL_Init returns -1 if an error occurs, function below to check for error if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { std::cout << "Initialization failed " << static_cast< std::string >( SDL_GetError() ); return false; } //Assigning parameters to create window window = SDL_CreateWindow( "CS_version1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if( window == NULL ) { std::cout << "Window could not be created " << static_cast< std::string >( SDL_GetError() ); } //Get window surface screen = SDL_GetWindowSurface( window ); return true; } bool loadMedia() { std::cout << "Loading media" << std::endl; player = SDL_LoadBMP( "player.bmp" ); if( player == NULL ) { std::cout << "Media could not be loaded " << static_cast< std::string >( SDL_GetError() ); return false; } return true; } void close() { std::cout << "Closing" << std::endl; //Deallocate surface SDL_FreeSurface( player ); //Destroy window SDL_DestroyWindow( window ); window = NULL; //Quits SDL SDL_Quit(); } int main( int argc, char* args[] ) { if( !init() ) { std::cout << "Initialization failed " << static_cast< std::string >( SDL_GetError() ); return false; } else { //Load Media if( !loadMedia() ) { std::cout << "media failed " << static_cast< std::string >( SDL_GetError() ); return false; } else { playerPos->x = 0; playerPos->y = 0; playerPos->w = 20; playerPos->h = 40; SDL_BlitSurface(player, NULL, screen, playerPos); SDL_UpdateWindowSurface( window ); SDL_Delay(2000); } } close(); //Console closes once program ends or error occurs, code used to keep it open std::cout << "Enter to continue..." << std::endl; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); return 0; }
-
Mesa glGetString() version differs from glxinfo
In my application I have this piece of code
void showOpenGLInfo() { printf("Graphic card vendor: %s\n", glGetString(GL_VENDOR)); printf("Renderer: %s\n", glGetString(GL_RENDERER)); printf("GL version: %s\n", glGetString(GL_VERSION)); printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); }
that shows this output on the application log:
Graphic card vendor: Intel Renderer: Mesa Intel(R) UHD Graphics 600 (GLK 2) GL version: 4.6 (Compatibility Profile) Mesa 20.3.5 GLSL version: 4.60
I have build Mesa driver 22.0.1 version from source code and glxinfo reports the correct value:
OpenGL version string: 4.6 (Compatibility Profile) Mesa 22.0.1
I don't understand this difference, any tip? Debian 11 Bullseye.
Add some OpenGL information:
OpenGL vendor string: Intel OpenGL renderer string: Mesa Intel(R) UHD Graphics 600 (GLK 2) OpenGL core profile version string: 4.6 (Core Profile) Mesa 22.0.1 OpenGL core profile shading language version string: 4.60 OpenGL core profile context flags: (none) OpenGL core profile profile mask: core profile OpenGL core profile extensions: OpenGL version string: 4.6 (Compatibility Profile) Mesa 22.0.1 OpenGL shading language version string: 4.60 OpenGL context flags: (none) OpenGL profile mask: compatibility profile OpenGL extensions: OpenGL ES profile version string: OpenGL ES 3.2 Mesa 22.0.1 OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20 OpenGL ES profile extensions:
Add other information based on comments:
ldd /usr/bin/glxinfo linux-vdso.so.1 (0x00007ffc9c5f6000) libGLEW.so.2.1 => /lib/x86_64-linux-gnu/libGLEW.so.2.1 (0x00007f3b2a68f000) libGLU.so.1 => /lib/x86_64-linux-gnu/libGLU.so.1 (0x00007f3b2a61f000) libGL.so.1 => /lib/x86_64-linux-gnu/libGL.so.1 (0x00007f3b2a598000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f3b2a454000) libX11.so.6 => /lib/x86_64-linux-gnu/libX11.so.6 (0x00007f3b2a311000) libXext.so.6 => /lib/x86_64-linux-gnu/libXext.so.6 (0x00007f3b2a2fc000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3b2a135000) libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f3b29f68000) libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f3b29f4e000) libGLdispatch.so.0 => /lib/x86_64-linux-gnu/libGLdispatch.so.0 (0x00007f3b29e96000) libGLX.so.0 => /lib/x86_64-linux-gnu/libGLX.so.0 (0x00007f3b29e62000) /lib64/ld-linux-x86-64.so.2 (0x00007f3b2a760000) libxcb.so.1 => /lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f3b29e37000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f3b29e2f000) libXau.so.6 => /lib/x86_64-linux-gnu/libXau.so.6 (0x00007f3b29e2a000) libXdmcp.so.6 => /lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f3b29c24000) libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f3b29c0d000) libmd.so.0 => /lib/x86_64-linux-gnu/libmd.so.0 (0x00007f3b29c00000) ldd my_application linux-gate.so.1 (0xf7f58000) libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xf7d3f000) libz.so.1 => /lib/i386-linux-gnu/libz.so.1 (0xf7d21000) libSDL2-2.0.so.0 => /lib/i386-linux-gnu/libSDL2-2.0.so.0 (0xf7bca000) libSDL2_image-2.0.so.0 => /lib32/libSDL2_image-2.0.so.0 (0xf7b9c000) libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xf7b96000) libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xf7a91000) /lib/ld-linux.so.2 (0xf7f5a000) libasound.so.2 => /lib/i386-linux-gnu/libasound.so.2 (0xf797b000) libpulse.so.0 => /lib/i386-linux-gnu/libpulse.so.0 (0xf791d000) libX11.so.6 => /lib/i386-linux-gnu/libX11.so.6 (0xf77ce000) libXext.so.6 => /lib/i386-linux-gnu/libXext.so.6 (0xf77b7000) libXcursor.so.1 => /lib/i386-linux-gnu/libXcursor.so.1 (0xf77a8000) libXinerama.so.1 => /lib/i386-linux-gnu/libXinerama.so.1 (0xf77a3000) libXi.so.6 => /lib/i386-linux-gnu/libXi.so.6 (0xf778e000) libXrandr.so.2 => /lib/i386-linux-gnu/libXrandr.so.2 (0xf777f000) libXss.so.1 => /lib/i386-linux-gnu/libXss.so.1 (0xf777a000) libXxf86vm.so.1 => /lib/i386-linux-gnu/libXxf86vm.so.1 (0xf7772000) libwayland-egl.so.1 => /lib/i386-linux-gnu/libwayland-egl.so.1 (0xf776d000) libwayland-client.so.0 => /lib/i386-linux-gnu/libwayland-client.so.0 (0xf775d000) libwayland-cursor.so.0 => /lib/i386-linux-gnu/libwayland-cursor.so.0 (0xf7753000) libxkbcommon.so.0 => /lib/i386-linux-gnu/libxkbcommon.so.0 (0xf770c000) libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xf76e9000) libpulsecommon-13.99.so => /usr/lib/i386-linux-gnu/pulseaudio/libpulsecommon-13.99.so (0xf7659000) libdbus-1.so.3 => /lib/i386-linux-gnu/libdbus-1.so.3 (0xf75fb000) libxcb.so.1 => /lib/i386-linux-gnu/libxcb.so.1 (0xf75cc000) libXrender.so.1 => /lib/i386-linux-gnu/libXrender.so.1 (0xf75c0000) libXfixes.so.3 => /lib/i386-linux-gnu/libXfixes.so.3 (0xf75b8000) libffi.so.7 => /lib/i386-linux-gnu/libffi.so.7 (0xf75ae000) libsystemd.so.0 => /lib/i386-linux-gnu/libsystemd.so.0 (0xf74f2000) libwrap.so.0 => /lib/i386-linux-gnu/libwrap.so.0 (0xf74e6000) libsndfile.so.1 => /lib/i386-linux-gnu/libsndfile.so.1 (0xf7454000) libasyncns.so.0 => /lib/i386-linux-gnu/libasyncns.so.0 (0xf744b000) libapparmor.so.1 => /lib/i386-linux-gnu/libapparmor.so.1 (0xf7436000) librt.so.1 => /lib/i386-linux-gnu/librt.so.1 (0xf742a000) libXau.so.6 => /lib/i386-linux-gnu/libXau.so.6 (0xf7424000) libXdmcp.so.6 => /lib/i386-linux-gnu/libXdmcp.so.6 (0xf741c000) liblzma.so.5 => /lib/i386-linux-gnu/liblzma.so.5 (0xf73f0000) liblz4.so.1 => /lib/i386-linux-gnu/liblz4.so.1 (0xf73cd000) libgcrypt.so.20 => /lib/i386-linux-gnu/libgcrypt.so.20 (0xf72ea000) libnsl.so.1 => /lib/i386-linux-gnu/libnsl.so.1 (0xf72ce000) libFLAC.so.8 => /lib/i386-linux-gnu/libFLAC.so.8 (0xf7293000) libogg.so.0 => /lib/i386-linux-gnu/libogg.so.0 (0xf7285000) libvorbis.so.0 => /lib/i386-linux-gnu/libvorbis.so.0 (0xf7258000) libvorbisenc.so.2 => /lib/i386-linux-gnu/libvorbisenc.so.2 (0xf71cb000) libresolv.so.2 => /lib/i386-linux-gnu/libresolv.so.2 (0xf71b2000) libbsd.so.0 => /lib/i386-linux-gnu/libbsd.so.0 (0xf7193000) libgpg-error.so.0 => /lib/i386-linux-gnu/libgpg-error.so.0 (0xf716d000)
-
SDL_Mixer not playing music from the start
I use
Mix_LoadMUS()
to load a song and then play it withMix_PlayMusic(music, -1)
. The first time I useMix_PlayMusic()
it works fine, but when usingMix_HaltMusic()
to stop the music and then try to play it again it starts from where the previous song left off and plays that part for half a second before properly restarting. How do I fix this? -
How to set audio position in Sdl_mixer
I am using SDL_mixer library to play multiple audio together.
I am able to play music together and it working fine, but i want to add a delay in playing audio.For example play first audio from 3 to 10 seconds and second audio from 5 to 14 seconds and third audio should play from 0 to 14.
How can i do this with sdl_audiomixer library?