Loads a sound from a wav file.
(sfx_t s)
| 49 | * Loads a sound from a wav file. |
| 50 | */ |
| 51 | public static sfxcache_t LoadSound(sfx_t s) { |
| 52 | if (s.name.charAt(0) == '*') |
| 53 | return null; |
| 54 | |
| 55 | // see if still in memory |
| 56 | sfxcache_t sc = s.cache; |
| 57 | if (sc != null) |
| 58 | return sc; |
| 59 | |
| 60 | String name; |
| 61 | // load it in |
| 62 | if (s.truename != null) |
| 63 | name = s.truename; |
| 64 | else |
| 65 | name = s.name; |
| 66 | |
| 67 | String namebuffer; |
| 68 | if (name.charAt(0) == '#') |
| 69 | namebuffer = name.substring(1); |
| 70 | else |
| 71 | namebuffer = "sound/" + name; |
| 72 | |
| 73 | byte[] data = FS.LoadFile(namebuffer); |
| 74 | |
| 75 | if (data == null) { |
| 76 | Com.DPrintf("Couldn't load " + namebuffer + "\n"); |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | int size = data.length; |
| 81 | |
| 82 | wavinfo_t info = GetWavinfo(s.name, data, size); |
| 83 | |
| 84 | if (info.channels != 1) |
| 85 | { |
| 86 | Com.Printf(s.name + " is a stereo sample - ignoring\n"); |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | float stepscale; |
| 91 | if (DONT_DO_A_RESAMPLING_FOR_JOAL_AND_LWJGL) |
| 92 | stepscale = 1; |
| 93 | else |
| 94 | stepscale = (float)info.rate / S.getDefaultSampleRate(); |
| 95 | |
| 96 | int len = (int) (info.samples / stepscale); |
| 97 | len = len * info.width * info.channels; |
| 98 | |
| 99 | // TODO: handle max sample bytes with a cvar |
| 100 | if (len >= maxsamplebytes) |
| 101 | { |
| 102 | Com.Printf(s.name + " is too long: " + len + " bytes?! ignoring.\n"); |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | sc = s.cache = new sfxcache_t(len); |
| 107 | |
| 108 | sc.length = info.samples; |
no test coverage detected