Differences From Artifact [285830fef5]:
- File
src/sound.mm
— part of check-in
[489124a92f]
at
2025-03-16 10:11:39
on branch trunk
— Use one autorelease pool per frame
This way, nowhere else autorelease pools need to be managed. (user: js, size: 5912) [annotate] [blame] [check-ins using]
To Artifact [91b933ce07]:
- File src/sound.mm — part of check-in [7e4ba7f32a] at 2025-03-20 16:39:53 on branch trunk — Remove non-functional FMOD support (user: js, size: 4026) [annotate] [blame] [check-ins using]
|
| < < < < < < < < < | | < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include "cube.h" #import "DynamicEntity.h" VARP(soundvol, 0, 255, 255); VARP(musicvol, 0, 128, 255); bool nosound = false; #define MAXCHAN 32 #define SOUNDFREQ 22050 struct soundloc { OFVector3D loc; bool inuse; } soundlocs[MAXCHAN]; #include <SDL_mixer.h> #define MAXVOL MIX_MAX_VOLUME Mix_Music *mod = NULL; void *stream = NULL; void stopsound() { if (nosound) return; if (mod) { Mix_HaltMusic(); Mix_FreeMusic(mod); mod = NULL; } if (stream != NULL) stream = NULL; } VAR(soundbufferlen, 128, 1024, 4096); void initsound() { memset(soundlocs, 0, sizeof(soundloc) * MAXCHAN); if (Mix_OpenAudio(SOUNDFREQ, MIX_DEFAULT_FORMAT, 2, soundbufferlen) < 0) { conoutf(@"sound init failed (SDL_mixer): %s", (size_t)Mix_GetError()); nosound = true; } Mix_AllocateChannels(MAXCHAN); } void music(OFString *name) { if (nosound) return; stopsound(); if (soundvol && musicvol) { name = [name stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]; OFString *path = [OFString stringWithFormat:@"packages/%@", name]; OFIRI *IRI = [Cube.sharedInstance.gameDataIRI IRIByAppendingPathComponent:path]; if ((mod = Mix_LoadMUS( IRI.fileSystemRepresentation.UTF8String)) != NULL) { Mix_PlayMusic(mod, -1); Mix_VolumeMusic((musicvol * MAXVOL) / 255); } } } COMMAND(music, ARG_1STR) vector<Mix_Chunk *> samples; static OFMutableArray<OFString *> *snames; int registersound(OFString *name) { int i = 0; |
︙ | ︙ | |||
154 155 156 157 158 159 160 | void cleansound() { if (nosound) return; stopsound(); | < < < < | 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | void cleansound() { if (nosound) return; stopsound(); Mix_CloseAudio(); } VAR(stereo, 0, 1, 1); static void updatechanvol(int chan, const OFVector3D *loc) { |
︙ | ︙ | |||
180 181 182 183 184 185 186 | float yaw = -atan2(v.x, v.y) - player1.yaw * (PI / 180.0f); // range is from 0 (left) to 255 (right) pan = int(255.9f * (0.5 * sin(yaw) + 0.5f)); } } vol = (vol * MAXVOL) / 255; | < < < < < < < < < | 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | float yaw = -atan2(v.x, v.y) - player1.yaw * (PI / 180.0f); // range is from 0 (left) to 255 (right) pan = int(255.9f * (0.5 * sin(yaw) + 0.5f)); } } vol = (vol * MAXVOL) / 255; Mix_Volume(chan, vol); Mix_SetPanning(chan, 255 - pan, pan); } static void newsoundloc(int chan, const OFVector3D *loc) { assert(chan >= 0 && chan < MAXCHAN); soundlocs[chan].loc = *loc; soundlocs[chan].inuse = true; } void updatevol() { if (nosound) return; loopi(MAXCHAN) if (soundlocs[i].inuse) { if (Mix_Playing(i)) updatechanvol(i, &soundlocs[i].loc); else soundlocs[i].inuse = false; } } void |
︙ | ︙ | |||
250 251 252 253 254 255 256 | if (!samples[n]) { OFString *path = [OFString stringWithFormat:@"packages/sounds/%@.wav", snames[n]]; OFIRI *IRI = [Cube.sharedInstance.gameDataIRI IRIByAppendingPathComponent:path]; | < < < < < < < < < < < < < | 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | if (!samples[n]) { OFString *path = [OFString stringWithFormat:@"packages/sounds/%@.wav", snames[n]]; OFIRI *IRI = [Cube.sharedInstance.gameDataIRI IRIByAppendingPathComponent:path]; samples[n] = Mix_LoadWAV(IRI.fileSystemRepresentation.UTF8String); if (!samples[n]) { conoutf(@"failed to load sample: %@", IRI.string); return; } } int chan = Mix_PlayChannel(-1, samples[n], 0); if (chan < 0) return; if (loc) newsoundloc(chan, loc); updatechanvol(chan, loc); } void sound(int n) { playsound(n, NULL); } COMMAND(sound, ARG_1INT) |