MCPcopy Create free account
hub / github.com/RavEngine/RavEngine / loadSound

Function loadSound

deps/SDL2/Xcode-iOS/Demos/src/mixer.c:87–118  ·  view source on GitHub ↗

loads a wav file (stored in 'file'), converts it to the mixer's output format, and stores the resulting buffer and length in the sound structure */

Source from the content-addressed store, hash-verified

85 and stores the resulting buffer and length in the sound structure
86 */
87void
88loadSound(const char *file, struct sound *s)
89{
90 SDL_AudioSpec spec; /* the audio format of the .wav file */
91 SDL_AudioCVT cvt; /* used to convert .wav to output format when formats differ */
92 int result;
93 if (SDL_LoadWAV(file, &spec, &s->buffer, &s->length) == NULL) {
94 fatalError("could not load .wav");
95 }
96 /* build the audio converter */
97 result = SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
98 mixer.outputSpec.format,
99 mixer.outputSpec.channels,
100 mixer.outputSpec.freq);
101 if (result == -1) {
102 fatalError("could not build audio CVT");
103 } else if (result != 0) {
104 /*
105 this happens when the .wav format differs from the output format.
106 we convert the .wav buffer here
107 */
108 cvt.buf = (Uint8 *) SDL_malloc(s->length * cvt.len_mult); /* allocate conversion buffer */
109 cvt.len = s->length; /* set conversion buffer length */
110 SDL_memcpy(cvt.buf, s->buffer, s->length); /* copy sound to conversion buffer */
111 if (SDL_ConvertAudio(&cvt) == -1) { /* convert the sound */
112 fatalError("could not convert .wav");
113 }
114 SDL_free(s->buffer); /* free the original (unconverted) buffer */
115 s->buffer = cvt.buf; /* point sound buffer to converted buffer */
116 s->length = cvt.len_cvt; /* set sound buffer's new length */
117 }
118}
119
120/* called from main event loop */
121void

Callers 1

mainFunction · 0.85

Calls 6

fatalErrorFunction · 0.85
SDL_BuildAudioCVTFunction · 0.85
SDL_mallocFunction · 0.85
SDL_memcpyFunction · 0.85
SDL_ConvertAudioFunction · 0.85
SDL_freeFunction · 0.85

Tested by

no test coverage detected