TbBool init_sound_heap_two_banks(unsigned char *heap_mem, long heap_size, char *snd_fname, char *spc_fname, long a5) { long i; long buf_len; unsigned char *buf; SYNCDBG(8,"Starting"); LbMemorySet(heap_mem, 0, heap_size); using_two_banks = 0; // Open first sound bank and prepare sample table if (sound_file != -1) close_sound_bank(0); samples_in_bank = 0; sound_file = _DK_LbFileOpen(snd_fname,Lb_FILE_MODE_READ_ONLY); if (sound_file == -1) { ERRORLOG("Couldn't open primary sound bank file \"%s\"",snd_fname); return false; } buf = heap_mem; buf_len = heap_size; i = parse_sound_file(sound_file, buf, &samples_in_bank, buf_len, a5); if (i == 0) { ERRORLOG("Couldn't parse sound bank file \"%s\"",snd_fname); close_sound_heap(); return false; } sample_table = (struct SampleTable *)buf; buf_len -= i; buf += i; if (buf_len <= 0) { ERRORLOG("Sound bank buffer too short"); close_sound_heap(); return false; } // Open second sound bank and prepare sample table if (sound_file2 != -1) close_sound_bank(1); samples_in_bank2 = 0; sound_file2 = _DK_LbFileOpen(spc_fname,Lb_FILE_MODE_READ_ONLY); if (sound_file2 == -1) { ERRORLOG("Couldn't open secondary sound bank file \"%s\"",spc_fname); return false; } i = parse_sound_file(sound_file2, buf, &samples_in_bank2, buf_len, a5); if (i == 0) { ERRORLOG("Couldn't parse sound bank file \"%s\"",spc_fname); close_sound_heap(); return false; } sample_table2 = (struct SampleTable *)buf; buf_len -= i; buf += i; if (buf_len <= 0) { ERRORLOG("Sound bank buffer too short"); close_sound_heap(); return false; } SYNCLOG("Got sound buffer of %ld bytes, samples in banks: %d,%d",buf_len,(int)samples_in_bank,(int)samples_in_bank2); sndheap = heapmgr_init(buf, buf_len, samples_in_bank2 + samples_in_bank); if (sndheap == NULL) { ERRORLOG("Sound heap manager init error"); close_sound_heap(); return false; } using_two_banks = 1; return true; }
TbBool setup_heaps(void) { TbBool low_memory; char snd_fname[2048]; char *spc_fname; long i; SYNCDBG(8,"Starting"); low_memory = false; if (!SoundDisabled) { StopAllSamples(); close_sound_heap(); if (sound_heap_memory != NULL) { LbMemoryFree(sound_heap_memory); sound_heap_memory = NULL; } } if (heap != NULL) { ERRORLOG("Graphics heap already allocated"); LbMemoryFree(heap); heap = NULL; } // Allocate sound heap if (!SoundDisabled) { i = mem_size; while (sound_heap_memory == NULL) { sound_heap_size = get_best_sound_heap_size(i); i = get_smaller_memory_amount(i); sound_heap_memory = LbMemoryAlloc(sound_heap_size); if ((i <= 8) && (sound_heap_memory == NULL)) { low_memory = true; break; } } } // Allocate graphics heap i = mem_size; while (heap == NULL) { heap_size = get_best_sound_heap_size(i); i = get_smaller_memory_amount(i); heap = LbMemoryAlloc(heap_size); if ((i <= 8) && (heap == NULL)) { low_memory = true; break; } } SYNCMSG("GraphicsHeap Size %d", heap_size); if (low_memory) { SYNCDBG(8,"Low memory mode entered on heap allocation."); while (heap != NULL) { if ((!SoundDisabled) && (sound_heap_memory == NULL)) { break; } if (!SoundDisabled) { if (sound_heap_size < heap_size) { heap_size -= 16384; } else if (sound_heap_size == heap_size) { heap_size -= 16384; sound_heap_size -= 16384; } else { sound_heap_size -= 16384; } if (sound_heap_size < 524288) { ERRORLOG("Unable to allocate heaps (small_mem)"); return false; } } else { heap_size -= 16384; } if (heap_size < 524288) { if (sound_heap_memory != NULL) { LbMemoryFree(sound_heap_memory); sound_heap_memory = NULL; } ERRORLOG("Unable to allocate heaps (small_mem)"); return false; } } if (sound_heap_memory != NULL) { LbMemoryFree(sound_heap_memory); sound_heap_memory = NULL; } if (heap != NULL) { LbMemoryFree(heap); heap = NULL; } if (!SoundDisabled) { sound_heap_memory = LbMemoryAlloc(sound_heap_size); } heap = LbMemoryAlloc(heap_size); } if (!SoundDisabled) { SYNCMSG("SoundHeap Size %d", sound_heap_size); // Prepare sound sample bank file names prepare_file_path_buf(snd_fname,FGrp_LrgSound,sound_fname); // language-specific speech file spc_fname = prepare_file_fmtpath(FGrp_LrgSound,"speech_%s.dat",get_language_lwrstr(install_info.lang_id)); // default speech file if (!LbFileExists(spc_fname)) spc_fname = prepare_file_path(FGrp_LrgSound,speech_fname); // speech file for english if (!LbFileExists(spc_fname)) spc_fname = prepare_file_fmtpath(FGrp_LrgSound,"speech_%s.dat",get_language_lwrstr(1)); // Initialize sample banks if (!init_sound_heap_two_banks(sound_heap_memory, sound_heap_size, snd_fname, spc_fname, 1622)) { LbMemoryFree(sound_heap_memory); sound_heap_memory = NULL; SoundDisabled = true; ERRORLOG("Unable to initialize sound heap. Sound disabled."); } } return true; }