Reprodução de arquivos WAV com SDL_mixer e linguagem C
O objetivo deste artigo é explicar o funcionamento da biblioteca SDL_mixer na reprodução de arquivos de áudio WAV através de aplicações escritas em linguagem C. A SDL_mixer é uma biblioteca adicional da SDL, mais poderosa para tratamento de arquivos de áudio do que as funções de SDL_audio, possibilitando por exemplo a execução de vários arquivos de áudio simultaneamente.
Parte 2: Compilação
Para compilar o trecho de código execute:
gcc -o programaExecutavel programaFonte.c -lSDL -lSDL_mixer
Os parâmetros -lSDL e -lSDL_mixer informam ao compilador que o código sendo compilado utiliza as libraries SDL e SDL_mixer.
Para executar o programa, digite:
./programaExecutavel
Obs.: Não se esqueça de obter algum arquivo de áudio no formato .wav e mantê-lo no mesmo diretório do programa executável, ou então adapte o código na função Mix_LoadWAV("caminho do arquivo WAV");
Se nós não criarmos os jogos, quem irá criar?
gcc -o programaExecutavel programaFonte.c -lSDL -lSDL_mixer
Os parâmetros -lSDL e -lSDL_mixer informam ao compilador que o código sendo compilado utiliza as libraries SDL e SDL_mixer.
Para executar o programa, digite:
./programaExecutavel
Obs.: Não se esqueça de obter algum arquivo de áudio no formato .wav e mantê-lo no mesmo diretório do programa executável, ou então adapte o código na função Mix_LoadWAV("caminho do arquivo WAV");
Conclusão
Tive a ideia de escrever este artigo após ter lido um outro sobre a SDL aqui no Viva O Linux. Acredito que SDL, OpenGL e Allegro são ferramentas que precisam ser exploradas por nós que utilizamos o Linux para que os games possam evoluir nesse sistema. Muitas pessoas alegam que esse é um dos motivos pelos quais elas não utilizam o Linux ou não abandonam o Windows, e essa talvez seja uma maneira viável para se mudar essa história.Se nós não criarmos os jogos, quem irá criar?
Sugestões para artigos
Seguem algumas sugestões para artigos com temas relacionados:- Integração entre SDL e OpenGL
- Comparação entre SDL e Allegro
- SDL - Instalação e configuração
Referências
- Simple DirectMedia Layer
- SDL_mixer at libsdl.org
- SDL mixer:Tutorials:Playing a WAV Sound File - GPWiki
- A biblioteca multimídia SDL
Linda iniciativa, mas o código não compila. Consegui reduzir o número de erros com a inclusão de stdlib, stdio e string.h no cabeçalho, mas isto não resolveu o problema. Não está claro também se tenho que instalar alguma biblioteca a parte.
Segue abaixo o código dos erros:
teste.c:1:21: error: SDL/SDL.h: Arquivo ou diretório não encontrado
teste.c:2:27: error: SDL/SDL_mixer.h: Arquivo ou diretório não encontrado
teste.c:7: error: conflicting types for ‘argv’
teste.c:7: note: previous definition of ‘argv’ was here
teste.c: In function ‘main’:
teste.c:10: warning: implicit declaration of function ‘SDL_Init’
teste.c:10: error: ‘SDL_INIT_VIDEO’ undeclared (first use in this function)
teste.c:10: error: (Each undeclared identifier is reported only once
teste.c:10: error: for each function it appears in.)
teste.c:10: error: ‘SDL_INIT_AUDIO’ undeclared (first use in this function)
teste.c:11: warning: implicit declaration of function ‘SDL_GetError’
teste.c:11: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
teste.c:18: error: ‘Uint16’ undeclared (first use in this function)
teste.c:18: error: expected ‘;’ before ‘audio_format’
teste.c:23: warning: implicit declaration of function ‘Mix_OpenAudio’
teste.c:23: error: ‘audio_format’ undeclared (first use in this function)
teste.c:24: warning: implicit declaration of function ‘Mix_GetError’
teste.c:24: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
teste.c:29: error: ‘Mix_Chunk’ undeclared (first use in this function)
teste.c:29: error: ‘sound’ undeclared (first use in this function)
teste.c:31: warning: implicit declaration of function ‘Mix_LoadWAV’
teste.c:33: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
teste.c:37: error: ‘SDL_Surface’ undeclared (first use in this function)
teste.c:37: error: ‘screen’ undeclared (first use in this function)
teste.c:38: warning: implicit declaration of function ‘SDL_SetVideoMode’
teste.c:38: error: ‘SDL_ANYFORMAT’ undeclared (first use in this function)
teste.c:40: warning: too many arguments for format
teste.c:45: warning: implicit declaration of function ‘Mix_PlayChannel’
teste.c:47: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
teste.c:51: warning: implicit declaration of function ‘Mix_Playing’
teste.c:53: warning: implicit declaration of function ‘Mix_FreeChunk’
teste.c:55: warning: implicit declaration of function ‘Mix_CloseAUdio’
teste.c:57: warning: implicit declaration of function ‘SDL_Quit’
Eu gostaria muito de que este programa funcionasse.