static wavinfo_t GetWavinfo(string name, byte[] wav, int wavlength) { wavinfo_t info = new wavinfo_t(); int i; int format; int samples; if (wav == null) { return(info); } iff_data = 0; iff_end = wavlength; data_b = wav; FindChunk("RIFF"); string s = Encoding.ASCII.GetString(data_b, 8, 4); //, data_p + 8, 4); if (!s.Equals("WAVE")) { Com.Printf("Missing RIFF/WAVE chunks\\n"); return(info); } iff_data = data_p + 12; FindChunk("fmt "); if (data_p == 0) { Com.Printf("Missing fmt chunk\\n"); return(info); } data_p += 8; format = GetLittleShort(); if (format != 1) { Com.Printf("Microsoft PCM format only\\n"); return(info); } info.channels = GetLittleShort(); info.rate = GetLittleLong(); data_p += 4 + 2; info.width = GetLittleShort() / 8; FindChunk("cue "); if (data_p != 0) { data_p += 32; info.loopstart = GetLittleLong(); FindNextChunk("LIST"); if (data_p != 0) { if (data_b.Length >= data_p + 32) { s = Encoding.ASCII.GetString(data_b, 28, 4); //new string (data_b, data_p + 28, 4); if (s.Equals("MARK")) { data_p += 24; i = GetLittleLong(); info.samples = info.loopstart + i; } } } } else { info.loopstart = -1; } FindChunk("data"); if (data_p == 0) { Com.Printf("Missing data chunk\\n"); return(info); } data_p += 4; samples = GetLittleLong() / info.width; if (info.samples != 0) { if (samples < info.samples) { Com.Error(Defines.ERR_DROP, "Sound " + name + " has a bad loop length"); } } else { info.samples = samples; if (info.loopstart > 0) { info.samples -= info.loopstart; } } info.dataofs = data_p; return(info); }
public static sfxcache_t LoadSound(sfx_t s) { if (s.name[0] == '*') { return(null); } sfxcache_t sc = s.cache; if (sc != null) { return(sc); } string name; if (s.truename != null) { name = s.truename; } else { name = s.name; } string namebuffer; if (name[0] == '#') { namebuffer = name.Substring(1); } else { namebuffer = "sound/" + name; } byte[] data = FS.LoadFile(namebuffer); if (data == null) { Com.DPrintf("Couldn't load " + namebuffer + "\\n"); return(null); } int size = data.Length; wavinfo_t info = GetWavinfo(s.name, data, size); if (info.channels != 1) { Com.Printf(s.name + " is a stereo sample - ignoring\\n"); return(null); } float stepscale; if (DONT_DO_A_RESAMPLING_FOR_JOAL_AND_LWJGL) { stepscale = 1; } else { stepscale = (float)info.rate / S.GetDefaultSampleRate(); } int len = (int)(info.samples / stepscale); len = len * info.width * info.channels; if (len >= maxsamplebytes) { Com.Printf(s.name + " is too long: " + len + " bytes?! ignoring.\\n"); return(null); } sc = s.cache = new sfxcache_t(len); sc.length = info.samples; sc.loopstart = info.loopstart; sc.speed = info.rate; sc.width = info.width; sc.stereo = info.channels; ResampleSfx(s, sc.speed, sc.width, data, info.dataofs); data = null; return(sc); }