1 /***************************************************************************
2 * Copyright (C) 2005 by Michal Turek - Woq *
3 * WOQ (zavinac) seznam.cz *
5 * Program ukazuje nahrani zvuku ve formatu .AU pomoci knihovny *
6 * SDL_sound, jeho dekodovani a nasledne prehravani *
7 * (opet pro jednoduchost smycka). Zde je pouzit .AU, ale naprosto *
8 * stejnym zpusobem lze pouzivat zvukove soubory jakychkoli jinych *
9 * formatu (.MP3, .OGG atd.), staci jen zmenit jmeno souboru. *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
29 #include <SDL/SDL_sound.h>// Nezapomenout prilinkovat -lSDL_sound
33 * Symbolicke konstanty
36 #define SDL_SUBSYSTEMS SDL_INIT_AUDIO
42 void AudioCallback(void *unused
, Uint8
*stream
, int len
);
43 int Init(); // Inicializace
44 void Destroy(); // Deinicializace
45 int ProcessEvent(); // Osetruje udalosti
46 int main(int argc
, char *argv
[]); // Vstup do programu
54 Sound_Sample
*g_sample
; // Zvuk
55 Uint32 g_pos
; // Pozice pri prehravani
59 * Audio callback funkce, posila data do streamu
62 void AudioCallback(void *unused
, Uint8
*stream
, int len
)
64 // Ukazatel na cast, kde se ma zacit prehravat
65 Uint8
*wave_ptr
= (Uint8
*)g_sample
->buffer
+ g_pos
;
67 // Delka zvuku do konce
68 int wave_left
= g_sample
->buffer_size
- g_pos
;
70 // Zbyvajici delka je mensi nez pozadovana
71 // Cyklus, protoze cely zvuk muze byt kratsi
72 while(wave_left
<= len
)
74 // Posle data na zvukovou kartu
75 SDL_MixAudio(stream
, wave_ptr
, wave_left
, SDL_MIX_MAXVOLUME
);
77 // Posune se o prave zapsana data
82 wave_ptr
= (Uint8
*)g_sample
->buffer
;
83 wave_left
= g_sample
->buffer_size
;
87 // Je jistota, ze zbyvajici cast zvuku je delsi nez pozadovana
88 SDL_MixAudio(stream
, wave_ptr
, len
, SDL_MIX_MAXVOLUME
);
94 * Inicializacni funkce
100 if(SDL_Init(SDL_SUBSYSTEMS
) == -1)
102 fprintf(stderr
, "Unable to initialize SDL: %s\n",
107 // Inicializace SDL_sound
108 if(Sound_Init() == 0)
110 fprintf(stderr
, "Unable to initialize SDL_sound: %s\n",
115 // Vypise verzi SDL_sound
116 Sound_Version compiled
;
117 Sound_Version linked
;
118 SOUND_VERSION(&compiled
);
119 Sound_GetLinkedVersion(&linked
);
121 printf("\nVersion of compiled SDL_sound: %d.%d.%d\n",
122 compiled
.major
, compiled
.minor
, compiled
.patch
);
123 printf("Version of linked SDL_sound: %d.%d.%d\n\n",
124 linked
.major
, linked
.minor
, linked
.patch
);
126 // Vypise dostupne audio formaty
127 const Sound_DecoderInfo
**dec_info
= Sound_AvailableDecoders();
129 printf("Supported sound formats:\n");
131 for(i
= 0; dec_info
[i
] != NULL
; i
++)
134 *dec_info
[i
]->extensions
, dec_info
[i
]->description
);
139 SDL_AudioSpec desired
, obtained
;
140 desired
.freq
= 22050;// 16-bit. stereo na 22 kHz
141 desired
.format
= AUDIO_S16
;
142 desired
.channels
= 2;
143 desired
.samples
= 512;// Vhodne pro hry
144 desired
.callback
= AudioCallback
;
145 desired
.userdata
= NULL
;
147 // Otevre audio zarizeni
148 if(SDL_OpenAudio(&desired
, &obtained
) == -1)
150 fprintf(stderr
, "Unable to open audio: %s\n", SDL_GetError());
154 // Loading audia a dekodovani (vse najednou)
155 Sound_AudioInfo info
;
156 info
.channels
= obtained
.channels
;
157 info
.format
= obtained
.format
;
158 info
.rate
= obtained
.freq
;
160 g_sample
= Sound_NewSampleFromFile("bd.wav", &info
, 512);
163 fprintf(stderr
, "Unable to load sound: %s\n", Sound_GetError());
167 // Dekoduje cely zvuk najednou
168 Sound_DecodeAll(g_sample
);
169 if(g_sample
->flags
& SOUND_SAMPLEFLAG_ERROR
)
171 fprintf(stderr
, "Unable to decode sound: %s\n",
184 * Deinicializacni funkce
189 Sound_FreeSample(g_sample
); // Uvolni zvuk
190 Sound_Quit(); // Ukonci SDL_sound
191 SDL_CloseAudio(); // Zavre audio zarizeni
192 SDL_Quit(); // Deinicializuje SDL
204 while(SDL_WaitEvent(&event
))
209 switch(event
.key
.keysym
.sym
)
237 int main(int argc
, char *argv
[])
239 printf("\nPress ESC key to quit.\n");
248 // Hlavni smycka programu
252 done
= !ProcessEvent();
255 // Deinicializace a konec
This page took 0.661944 seconds and 4 git commands to generate.