sound.c
changeset 2594 c65db1a9f630
parent 2372 f751e366f618
child 2977 0240dd4704a7
equal deleted inserted replaced
2593:a9f7d3d70639 2594:c65db1a9f630
    21 
    21 
    22 static uint _file_count;
    22 static uint _file_count;
    23 static FileEntry* _files;
    23 static FileEntry* _files;
    24 
    24 
    25 #define SOUND_SLOT 31
    25 #define SOUND_SLOT 31
       
    26 // Number of levels of panning per side
       
    27 #define PANNING_LEVELS 16
    26 
    28 
    27 
    29 
    28 static void OpenBankFile(const char *filename)
    30 static void OpenBankFile(const char *filename)
    29 {
    31 {
    30 	FileEntry* fe;
    32 	FileEntry* fe;
   130 	OpenBankFile(filename);
   132 	OpenBankFile(filename);
   131 	return true;
   133 	return true;
   132 }
   134 }
   133 
   135 
   134 // Low level sound player
   136 // Low level sound player
   135 static void StartSound(uint sound, uint panning, uint volume)
   137 static void StartSound(uint sound, int panning, uint volume)
   136 {
   138 {
   137 	MixerChannel* mc;
   139 	MixerChannel* mc;
       
   140 	uint left_vol, right_vol;
   138 
   141 
   139 	if (volume == 0) return;
   142 	if (volume == 0) return;
   140 	mc = MxAllocateChannel(_mixer);
   143 	mc = MxAllocateChannel(_mixer);
   141 	if (mc == NULL) return;
   144 	if (mc == NULL) return;
   142 	if (!SetBankSource(mc, sound)) return;
   145 	if (!SetBankSource(mc, sound)) return;
   143 	MxSetChannelVolume(mc, volume << 8, volume << 8);
   146 
       
   147 	panning = clamp(panning, -PANNING_LEVELS, PANNING_LEVELS);
       
   148 	left_vol = (volume * PANNING_LEVELS) - (volume * panning);
       
   149 	right_vol = (volume * PANNING_LEVELS) + (volume * panning);
       
   150 	MxSetChannelVolume(mc, left_vol * 128 / PANNING_LEVELS, right_vol * 128 / PANNING_LEVELS);
   144 	MxActivateChannel(mc);
   151 	MxActivateChannel(mc);
   145 }
   152 }
   146 
   153 
   147 
   154 
   148 static const byte _vol_factor_by_zoom[] = {255, 190, 134};
   155 static const byte _vol_factor_by_zoom[] = {255, 190, 134};
   183 		const ViewPort* vp = w->viewport;
   190 		const ViewPort* vp = w->viewport;
   184 
   191 
   185 		if (vp != NULL &&
   192 		if (vp != NULL &&
   186 				IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) &&
   193 				IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) &&
   187 				IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) {
   194 				IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) {
   188 			int left = ((x - vp->virtual_left) >> vp->zoom) + vp->left;
   195 			int left = (x - vp->virtual_left);
   189 
   196 
   190 			StartSound(
   197 			StartSound(
   191 				_sound_idx[sound],
   198 				_sound_idx[sound],
   192 				clamp(left / 71, 0, 8),
   199 				left / (vp->virtual_width / ((PANNING_LEVELS << 1) + 1)) - PANNING_LEVELS,
   193 				(_sound_base_vol[sound] * msf.effect_vol * _vol_factor_by_zoom[vp->zoom]) >> 15
   200 				(_sound_base_vol[sound] * msf.effect_vol * _vol_factor_by_zoom[vp->zoom]) >> 15
   194 			);
   201 			);
   195 			return;
   202 			return;
   196 		}
   203 		}
   197 	}
   204 	}
   217 
   224 
   218 void SndPlayFx(SoundFx sound)
   225 void SndPlayFx(SoundFx sound)
   219 {
   226 {
   220 	StartSound(
   227 	StartSound(
   221 		_sound_idx[sound],
   228 		_sound_idx[sound],
   222 		4,
   229 		0,
   223 		(_sound_base_vol[sound] * msf.effect_vol) >> 7
   230 		(_sound_base_vol[sound] * msf.effect_vol) >> 7
   224 	);
   231 	);
   225 }
   232 }