// open and pre-load the stream buffers for the different voice streams
void red_alert_voice_load()
{
	Assert( Briefing != NULL );
	if ( strnicmp(Briefing->stages[0].voice, NOX("none"), 4) && (Briefing->stages[0].voice[0] != '\0') ) {
		Red_alert_voice = audiostream_open( Briefing->stages[0].voice, ASF_VOICE );
	}
}
void SoundEnvironment::OnBrowseWave()
{
	int z;

	if (m_wave_id >= 0) {
		audiostream_close_file(m_wave_id, 0);
		m_wave_id = -1;
	}

	UpdateData(TRUE);

	m_wave_filename = _T("");

	z = cfile_push_chdir(CF_TYPE_DATA);

	CFileDialog dlg(TRUE, "wav", m_wave_filename, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR,
		"Voice Files (*.ogg, *.wav)|*.ogg;*.wav|Ogg Vorbis Files (*.ogg)|*.ogg|Wave Files (*.wav)|*.wav||");

	if (dlg.DoModal() == IDOK) {
		m_wave_filename = dlg.GetFileName();
		m_wave_id = audiostream_open((char *)(LPCSTR) m_wave_filename, ASF_SOUNDFX);
	}

	if ( !z ) {
		cfile_pop_dir();
	}
}
void send_autopilot_msg(char *msg, char *snd)
{
	// setup
	if (audio_handle != -1)
	{
		audiostream_close_file(audio_handle, 0);
		audio_handle = -1;
	}

	if (msg[0] != '\0' && strcmp(msg, "none"))
		change_message("autopilot builtin message", msg, -1, 0);

	// load sound
	if ((snd != NULL) && (snd[0] != '\0' && !strcmp(snd, "none")))
	{
		audio_handle = audiostream_open(snd, ASF_MENUMUSIC );
	}

	// send/play
	if (audio_handle != -1)
	{
		audiostream_play(audio_handle, (Master_event_music_volume * aav_music_volume), 0);
	}

	if (msg[0] != '\0' && strcmp(msg, "none"))
		message_training_queue("autopilot builtin message", timestamp(0), 5); // display message for five seconds
}
void credits_load_music(char* fname)
{
	if ( Credits_music_handle != -1 ){
		return;
	}

	if ( fname && *fname ){
		Credits_music_handle = audiostream_open( fname, ASF_MENUMUSIC );
	}
}
void fiction_viewer_load(const char *filename, const char *font_filename, const char *voice_filename)
{
	int file_length;
	Assertion(filename, "Invalid fictionviewer filename pointer given!");
	Assertion(font_filename, "Invalid fictionviewer font filename pointer given!");
	Assertion(voice_filename, "Invalid fictionviewer voice filename pointer given!");

	// just to be sure
	if (Fiction_viewer_text != NULL)
	{
		Int3();
		fiction_viewer_reset();
	}

	// save our filenames
	strcpy_s(Fiction_viewer_filename, filename);
	strcpy_s(Fiction_viewer_font_filename, font_filename);
	strcpy_s(Fiction_viewer_voice_filename, voice_filename);

	// see if we have a matching font
	Fiction_viewer_fontnum = gr_get_fontnum(Fiction_viewer_font_filename);
	if (Fiction_viewer_fontnum < 0 && !Fred_running)
		strcpy_s(Fiction_viewer_font_filename, "");

	Fiction_viewer_voice = audiostream_open(Fiction_viewer_voice_filename, ASF_VOICE);
	if (Fiction_viewer_voice < 0 && !Fred_running)
		strcpy_s(Fiction_viewer_voice_filename, "");

	if (!strlen(filename))
		return;

	// load up the text
	CFILE *fp = cfopen(filename, "rb", CFILE_NORMAL, CF_TYPE_FICTION);
	if (fp == NULL)
	{
		Warning(LOCATION, "Unable to load fiction file '%s'.", filename);
		return;
	}

	// we don't need to copy the text in Fred
	if (!Fred_running)
	{
		// allocate space
		file_length = cfilelength(fp);
		Fiction_viewer_text = (char *) vm_malloc(file_length + 1);
		Fiction_viewer_text[file_length] = '\0';

		// copy all the text
		cfread(Fiction_viewer_text, file_length, 1, fp);
	}

	// we're done, close it out
	cfclose(fp);
}
void event_editor::OnPlay() 
{
	GetDlgItem(IDC_WAVE_FILENAME)->GetWindowText(m_wave_filename);

	if (m_wave_id >= 0) {
		audiostream_close_file(m_wave_id, 0);
		m_wave_id = -1;
		return;
	}

	// we use ASF_EVENTMUSIC here so that it will keep the extension in place
	m_wave_id = audiostream_open((char *)(LPCSTR) m_wave_filename, ASF_EVENTMUSIC);

	if (m_wave_id >= 0) {
		audiostream_play(m_wave_id, 1.0f, 0);
	}
}
void cmd_brief_init_voice()
{
	int i;

	Assert(Cur_cmd_brief);
	for (i=0; i<Cur_cmd_brief->num_stages; i++) {
		Cur_cmd_brief->stage[i].wave = -1;
		if (stricmp(Cur_cmd_brief->stage[i].wave_filename, NOX("none")) && Cur_cmd_brief->stage[i].wave_filename[0]) {
			Cur_cmd_brief->stage[i].wave = audiostream_open(Cur_cmd_brief->stage[i].wave_filename, ASF_VOICE);
			if (Cur_cmd_brief->stage[i].wave < 0) {
				nprintf(("General", "Failed to load \"%s\"\n", Cur_cmd_brief->stage[i].wave_filename));
			}
		}
	}

	Cmd_brief_last_voice = -1;
	Cmd_brief_last_stage = -1;
}
// plays the voice file associated with a training message.  Automatically streams the file
// from disk if it's over 100k, otherwise plays it as a normal file in memory.  Returns -1
// if it didn't play, otherwise index of voice
int message_play_training_voice(int index)
{
	int len;
	CFILE *fp;

	if (index < 0) {
		if (Training_voice >= 0) {
			if (Training_voice_type) {
				audiostream_close_file(Training_voice_handle, 0);

			} else {
				snd_stop(Training_voice_handle);
			}
		}

		Training_voice = -1;
		return -1;
	}

	if (Message_waves[index].num < 0) {
		fp = cfopen(Message_waves[index].name, "rb");
		if (!fp)
			return -1;

		len = cfilelength(fp);
		cfclose(fp);
		if (len > 100000) {
			if ((Training_voice < 0) || !Training_voice_type || (Training_voice != index)) {
				if (Training_voice >= 0) {
					if (Training_voice_type) {
						if (Training_voice == index)
							audiostream_stop(Training_voice_handle, 1, 0);
						else
							audiostream_close_file(Training_voice_handle, 0);

					} else {
						snd_stop(Training_voice_handle);
					}
				}

				if (stricmp(Message_waves[index].name, NOX("none.wav"))) {
					Training_voice_handle = audiostream_open(Message_waves[index].name, ASF_VOICE);
					if (Training_voice_handle < 0) {
						nprintf(("Warning", "Unable to load voice file %s\n", Message_waves[index].name));
					//	Warning(LOCATION, "Unable to load voice file %s\n", Message_waves[index].name);
					}
				}
			}  // Training_voice should be valid and loaded now

			Training_voice_type = 1;
			if (Training_voice_handle >= 0)
				audiostream_play(Training_voice_handle, Master_voice_volume, 0);

			Training_voice = index;
			return Training_voice;

		} else {
			game_snd tmp_gs;
			memset(&tmp_gs, 0, sizeof(game_snd));
			strcpy(tmp_gs.filename, Message_waves[index].name);
			Message_waves[index].num = snd_load(&tmp_gs, 0);
			if (Message_waves[index].num < 0) {
				nprintf(("Warning", "Cannot load message wave: %s.  Will not play\n", Message_waves[index].name));
				return -1;
			}
		}
	}

	if (Training_voice >= 0) {
		if (Training_voice_type) {
			audiostream_close_file(Training_voice_handle, 0);

		} else {
			snd_stop(Training_voice_handle);
		}
	}

	Training_voice = index;
	if (Message_waves[index].num >= 0)
		Training_voice_handle = snd_play_raw(Message_waves[index].num, 0.0f);
	else
		Training_voice_handle = -1;

	Training_voice_type = 0;
	return Training_voice;
}
// init
void loop_brief_init()
{
	int idx;
	ui_button_info *b;

	// load the background bitmap
	Loop_brief_bitmap = bm_load(Loop_brief_fname[gr_screen.res]);
	Assert(Loop_brief_bitmap != -1);

	// window
	Loop_brief_window.create(0, 0, gr_screen.max_w_unscaled, gr_screen.max_h_unscaled, 0);
	Loop_brief_window.set_mask_bmap(Loop_brief_mask[gr_screen.res]);	

	// add the buttons
	for (idx=0; idx<NUM_LOOP_BRIEF_BUTTONS; idx++) {
		b = &Loop_buttons[gr_screen.res][idx];

		b->button.create(&Loop_brief_window, "", b->x, b->y, 60, 30, 0, 1);		
		b->button.set_highlight_action(common_play_highlight_sound);
		b->button.set_bmaps(b->filename);
		b->button.link_hotspot(b->hotspot);
	}

	// add text
	for(idx=0; idx<NUM_LOOP_TEXT; idx++){
		Loop_brief_window.add_XSTR(&Loop_text[gr_screen.res][idx]);
	}

	const char* anim_name;
	// load animation if any
	if(Campaign.missions[Campaign.current_mission].mission_branch_brief_anim != NULL){
		anim_name = Campaign.missions[Campaign.current_mission].mission_branch_brief_anim;
	} else {
		anim_name = "CB_default";
	}

	int stream_result = generic_anim_init_and_stream(&Loop_anim, anim_name, bm_get_type(Loop_brief_bitmap), true);
	// we've failed to load any animation
	if (stream_result < 0) {
		// load an image and treat it like a 1 frame animation
		Loop_anim.first_frame = bm_load(anim_name);	//if we fail here, the value is still -1
		if(Loop_anim.first_frame != -1) {
			Loop_anim.num_frames = 1;
		}
	}

	// init brief text
	if(Campaign.missions[Campaign.current_mission].mission_branch_desc != NULL){
		brief_color_text_init(Campaign.missions[Campaign.current_mission].mission_branch_desc, Loop_brief_text_coords[gr_screen.res][2], default_loop_briefing_color);
	}

	bool sound_played = false;


	// open sound
	if(Campaign.missions[Campaign.current_mission].mission_branch_brief_sound != NULL){
		Loop_sound = audiostream_open(Campaign.missions[Campaign.current_mission].mission_branch_brief_sound, ASF_VOICE);

		if(Loop_sound != -1){
			audiostream_play(Loop_sound, Master_voice_volume, 0);
			sound_played = true;
		}
	}

	if(sound_played == false) {
		fsspeech_play(FSSPEECH_FROM_BRIEFING, 
			Campaign.missions[Campaign.current_mission].mission_branch_desc);

	}

	// music
	common_music_init(SCORE_BRIEFING);
}