Welcome to Stratos!
Attribute VB_Name = "Audio"Option Explicit'Declaraciones varias para reproducir WAVs, MIDIs y cosas de esas.'-----------------------------------------------------------------'Constantes para ser usadas como flags por sndPlaySound'(Los comentarios que las describen estan sacados de' la documentacion de Microsoft)Public Const SND_SYNC = &H0 'SND_SYNC specifies that the sound is played synchronously and the function does not return until the sound ends.Public Const SND_ASYNC = &H1 'SND_ASYNC specifies that the sound is played asynchronously and the function returns immediately after beginning the sound.Public Const SND_NODEFAULT = &H2 'SND_NODEFAULT specifies that if the sound cannot be found, the function returns silently without playing the default sound.Public Const SND_LOOP = &H8 'SND_LOOP specifies that the sound will continue to play continuously until sndPlaySound is called again with the lpszSoundName$ parameter set to null. You must also specify the SND_ASYNC flag to loop sounds.Public Const SND_NOSTOP = &H10 'SND_NOSTOP specifies that if a sound is currently playing, the function will immediately return False without playing the requested sound.'sndPlaySound - Permite reproducir archivos WAVDeclare Function sndPlaySound Lib "winmm.dll" Alias _ "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As _ Long) As Long'mciSendString - Permite reproducir musica y videos'(NOTA: mciSendString no soporta nombres largos de archivos.)Declare Function mciSendString Lib "winmm.dll" Alias _ "mciSendStringA" (ByVal lpstrCommand As String, ByVal _ lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _ hwndCallback As Long) As Long
'Abrimos archivo de musica archivoMusica = "musica.mid" mciSendString("open " & archivoMusica & " type sequencer alias musica", 0&, 0, 0) 'Reproducimos sonido archivoSonido = "sonido.wav" sndPlaySound archivoSonido, SND_SYNC Or SND_NODEFAULT ... 'comprobamos si la musica ha parado y si es asi la volvemos a reproducir If mciSendString("status musica mode", msg, 8, 0) = 0 Then If CStr(msg) = "stopped" Then mciSendString "play musica from 0", 0&, 0, 0 End If End If ... 'pausar la musica mciSendString "pause musica", 0&, 0, 0 ... 'reanudar la musica mciSendString "resume musica", 0&, 0, 0 ... 'Cerramos musica... mciSendString "close musica", 0&, 0, 0
#include <windows.h>#define N_SAMPLES (44100*20)struct wavh{ char riff[4]; uint32 size; char wave[4]; char fmt[4]; uint32 num; uint16 format; uint16 channels; uint32 freq; uint32 abps; uint16 blockalign; uint16 bitdepth; char data[4]; uint32 datasize; } wave = { { 'R', 'I', 'F', 'F' }, sizeof(wave) + N_SAMPLES*2, { 'W', 'A', 'V', 'E' }, { 'f', 'm', 't', ' ' }, 0x10, 1, 1, 44100, 88200, 2, 16, { 'd', 'a', 't', 'a' }, N_SAMPLES*2};int16 wave_data[sizeof(wave)+N_SAMPLES]; void synth(void ){ short *buf =wave_data+sizeof(wave); float env = 1.0f; int i=0; *(struct wavh*)wave_data = wave; for(;i<N_SAMPLES;i++) { if(env <= 0) env = 0.0f; env-=1.0f/(0.2*sample_rate); *buf++=32767*(env*sin(2*PI*i/sample_rate)); } /* { FILE *file = fopen("tune.wav", "wb"); fwrite(wave_data, wave.size, 1, file); fclose(file); }*/ sndPlaySound((LPCSTR)wave_data, SND_NODEFAULT + SND_ASYNC + SND_MEMORY); }
Para aquellos que todavia no hayan hecho nada en temas de sonido y no se quieran complicar la vida en la proxima compo, ahi va un poco de código VB para el sonido.
EX3Por supuesto que no hay problemas en usar librerias de 3eros. Es una competicion de juegos, no de librerias o motores.
// constantes globalesconst C_AUDIO_RATE = 22050; // o 44100 :P C_AUDIO_CHANNELS = 8; C_AUDIO_BUFFER = 4096;// variables globalesvar Muzik : PMix_Music; WavSng : PMix_Chunk; Canal : longint; // Al cargar el programa.... // iniciar SDL // (como podeis ver no he puesto una gestion muy avanzada de errores :P) if SDL_Init(SDL_INIT_AUDIO or SDL_INIT_VIDEO) < 0 then halt; // iniciar SDL_Mixer // Nota: por defecto se asignan 8 canales. Con Mix_AllocateChannels se pueden pedir mas. // Nota 2: el parametro canales de esta funcion vale 2 pq se refiere a 2 canales estereo, // no canales de "mixing" if Mix_OpenAudio(C_AUDIO_RATE, MIX_DEFAULT_FORMAT, 2, C_AUDIO_BUFFER) < 0 then halt; // La funcion OpenAudio hara lo que pueda. Con Mix_QuerySpec podemos averiguar que // hemos obtenido realmente. // Cargo el wav (imagino que solo los pilla descomprimidos [al menos en linux]) WavSng := Mix_LoadWAV('sound.wav'); if WavSng = NIL then halt; // Cada vez que quiero reproducir un sonido hago lo siguiente. // Nota: cada vez que llamo a esta funcion cambio la variable canal para que se vayan // turnando los 8 canales por defecto. Mix_PlayChannel(Canal mod C_AUDIO_CHANNELS, WavSng, 0); inc(Canal); // cargar la musica Muzik := Mix_LoadMUS('music.ogg'); if Muzik = NIL then halt; // reproducir la musica Mix_PlayMusic(Muzik, -1) // -1 es loop infinito // Pausar la musica Mix_PauseMusic(); // Reanudar la musica Mix_ResumeMusic(); // Fade-Out de la musica Mix_FadeOutMusic(3000); // 3000msec = 3seg. // Ajustar volumen de la musica Mix_VolumeMusic(Valor); // Valor va de 0 a MIX_MAX_VOLUME // Ajustar el volumen del sonido // Nota: me pateo todos los canales y les pongo el mismo volumen var _c : longint; begin for _c := 0 to C_AUDIO_CHANNELS - 1 do begin Mix_Volume(_c, Valor); // Valor va de 0 a MIX_MAX_VOLUME end; end; // Cerrar la aplicacion if (WavSng <> nil) then Mix_FreeChunk(WavSng); if (Muzik <> nil) then Mix_FreeMusic(Muzik); Mix_CloseAudio(); SDL_Quit;