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 */
| 85 | and stores the resulting buffer and length in the sound structure |
| 86 | */ |
| 87 | void |
| 88 | loadSound(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 */ |
| 121 | void |
no test coverage detected