void SoundEnvironment::OnPlay()
{
	sound_env m_env;

	if ( !sound_env_supported() ) {
		MessageBox("Sound environment effects are not available! Unable to preview!", "Error", MB_OK | MB_ICONEXCLAMATION);
		return;
	}

	if (m_wave_id < 0) {
		OnBrowseWave();
		return;
	}

	UpdateData(TRUE);

	if (m_environment > 0) {
		m_env.id = m_environment - 1;
		m_env.volume = m_volume;
		m_env.damping = m_damping;
		m_env.decay = m_decay_time;

		sound_env_set(&m_env);
	}

	audiostream_play(m_wave_id, 1.0f, 0);
}
// start playback of the red alert voice
void red_alert_voice_play()
{
	if ( !Briefing_voice_enabled ) {
		return;
	}

	if ( Red_alert_voice < 0 ) {
		// play simulated speech?
		if (fsspeech_play_from(FSSPEECH_FROM_BRIEFING)) {
			if (fsspeech_playing()) {
				return;
			}

			fsspeech_play(FSSPEECH_FROM_BRIEFING, Briefing->stages[0].text.c_str());
			Red_alert_voice_started = 1;
		}
	} else {
		if (audiostream_is_playing(Red_alert_voice)) {
			return;
		}

		audiostream_play(Red_alert_voice, Master_voice_volume, 0);
		Red_alert_voice_started = 1;
	}
}
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_start_music()
{
	if (Credits_music_handle != -1) {
		if ( !audiostream_is_playing(Credits_music_handle) ){
			audiostream_play(Credits_music_handle, Master_event_music_volume, 1);
		}
	} else {
		nprintf(("Warning", "Cannot play credits music\n"));
	}
}
// unpause the audio stream identified by handle i.
void audiostream_unpause(int i)
{
	int is_looping;

	if ( i == -1 )
		return;

	Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
	if ( Audio_streams[i].status == ASF_FREE )
		return;

	if ( audiostream_is_paused(i) == TRUE ) {
		is_looping = Audio_streams[i].Is_looping();
		audiostream_play(i, -1.0f, is_looping);
	}
}
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);
	}
}
// 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 fiction_viewer_init()
{
	if (Fiction_viewer_inited)
		return;

	// no fiction viewer?
	if (!mission_has_fiction())
		return;

	// music
	common_music_init(SCORE_FICTION_VIEWER);

	// see if we have a background bitmap, and if so, which one
	// currently, we prioritize the UI that comes latest in the array;
	// in the future we might specify this in the mission or in a tbl
	for (Fiction_viewer_ui = NUM_FVW_SETTINGS - 1; Fiction_viewer_ui >= 0; Fiction_viewer_ui--)
	{
		// load the first available background bitmap
		Fiction_viewer_bitmap = bm_load(Fiction_viewer_screen_filename[Fiction_viewer_ui][gr_screen.res]);
		if (Fiction_viewer_bitmap >= 0)
			break;
	}
	
	// no ui is valid?
	if (Fiction_viewer_ui < 0)
	{
		Warning(LOCATION, "No fiction viewer graphics -- cannot display fiction viewer!");
		return;
	}

	// set up fiction viewer font
	use_fv_font();

	// calculate text area lines from font
	Fiction_viewer_text_max_lines = Fiction_viewer_text_coordinates[Fiction_viewer_ui][gr_screen.res][3] / gr_get_font_height();

	// window
	Fiction_viewer_window.create(0, 0, gr_screen.max_w_unscaled, gr_screen.max_h_unscaled, 0, Fiction_viewer_fontnum);
	Fiction_viewer_window.set_mask_bmap(Fiction_viewer_screen_mask[Fiction_viewer_ui][gr_screen.res]);	

	// add the buttons
	for (int i = 0; i < NUM_FVW_BUTTONS; i++)
	{
		int repeat = (i == FVW_BUTTON_SCROLL_UP || i == FVW_BUTTON_SCROLL_DOWN);
		ui_button_info *b = &Fiction_viewer_buttons[Fiction_viewer_ui][gr_screen.res][i];

		b->button.create(&Fiction_viewer_window, "", b->x, b->y, b->xt, b->yt, repeat, 1);		
		b->button.set_highlight_action(common_play_highlight_sound);
		b->button.set_bmaps(b->filename);
		b->button.link_hotspot(b->hotspot);
	}

	// set up hotkeys for buttons
	Fiction_viewer_buttons[Fiction_viewer_ui][gr_screen.res][FVW_BUTTON_ACCEPT].button.set_hotkey(KEY_CTRLED | KEY_ENTER);
	Fiction_viewer_buttons[Fiction_viewer_ui][gr_screen.res][FVW_BUTTON_SCROLL_UP].button.set_hotkey(KEY_UP);
	Fiction_viewer_buttons[Fiction_viewer_ui][gr_screen.res][FVW_BUTTON_SCROLL_DOWN].button.set_hotkey(KEY_DOWN);

	// init brief text
	brief_color_text_init(Fiction_viewer_text, Fiction_viewer_text_coordinates[Fiction_viewer_ui][gr_screen.res][2], default_fiction_viewer_color, 0, 0);

	// if the story is going to overflow the screen, add a slider
	if (Num_brief_text_lines[0] > Fiction_viewer_text_max_lines)
	{
		Fiction_viewer_slider.create(&Fiction_viewer_window,
			Fiction_viewer_slider_coordinates[Fiction_viewer_ui][gr_screen.res][0],
			Fiction_viewer_slider_coordinates[Fiction_viewer_ui][gr_screen.res][1],
			Fiction_viewer_slider_coordinates[Fiction_viewer_ui][gr_screen.res][2],
			Fiction_viewer_slider_coordinates[Fiction_viewer_ui][gr_screen.res][3],
			Num_brief_text_lines[0] - Fiction_viewer_text_max_lines,
			Fiction_viewer_slider_filename[Fiction_viewer_ui][gr_screen.res],
			&fiction_viewer_scroll_up,
			&fiction_viewer_scroll_down,
			&fiction_viewer_scroll_capture);
	}

	if (Fiction_viewer_voice >= 0)
	{
		audiostream_play(Fiction_viewer_voice, Master_voice_volume, 0);
	}
	
	Fiction_viewer_inited = 1;
}
// 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);
}
/**
 * Start playback of the voice for a particular briefing stage
 * @param stage_num Particular briefing stage
 */
void cmd_brief_voice_play(int stage_num)
{
	int voice = -1;
	int stage = -1;

	if (!Voice_good_to_go) {
		Voice_started_time = 0;
		return;
	}

	if (!Voice_started_time) {
		Voice_started_time = timer_get_milliseconds();
		Voice_ended_time = 0;
	}

	if (!Briefing_voice_enabled){
		return;
	}

	if (Cur_stage >= 0 && Cur_stage < Cur_cmd_brief->num_stages){
		voice = Cur_cmd_brief->stage[stage_num].wave;
		stage = stage_num;
	}

	// do we need to play simulated speech?
	if (voice < 0 && fsspeech_play_from(FSSPEECH_FROM_BRIEFING)) {
		// are we still on the same stage?
		if (Cmd_brief_last_stage == stage) {
			return;  // no changes, nothing to do.
		}

		// if previous stage is still playing, stop it first.
		if (Cmd_brief_last_stage >= 0) {
			fsspeech_stop();
			Cmd_brief_last_stage = -1;
		}

		// ok, new text needs speaking
		Cmd_brief_last_stage = stage;
		if (stage >= 0) {
			fsspeech_play(FSSPEECH_FROM_BRIEFING, Cur_cmd_brief->stage[stage_num].text.c_str());
		}
	} else {
		// are we still on same voice that is currently playing/played?
		if (Cmd_brief_last_voice == voice) {
			return;  // no changes, nothing to do.
		}

		// if previous wave is still playing, stop it first.
		if (Cmd_brief_last_voice >= 0) {
			audiostream_stop(Cmd_brief_last_voice, 1, 0);  // stream is automatically rewound
			Cmd_brief_last_voice = -1;
		}

		// ok, new wave needs playing, so we can start playing it now (and it becomes the current wave)
		Cmd_brief_last_voice = voice;
		if (voice >= 0) {
			audiostream_play(voice, Master_voice_volume, 0);
		}
	}
}