xref: /haiku/src/kits/game/GSUtility.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		GameSoundDevice.h
23 //	Author:			Christopher ML Zumwalt May (zummy@users.sf.net)
24 //	Description:	Utility functions used by sound system
25 //------------------------------------------------------------------------------
26 
27 // Standard Includes -----------------------------------------------------------
28 
29 // System Includes -------------------------------------------------------------
30 #include <MediaDefs.h>
31 
32 // Project Includes ------------------------------------------------------------
33 #include <GameSoundDefs.h>
34 
35 // Local Includes --------------------------------------------------------------
36 #include <GSUtility.h>
37 
38 // Local Defines ---------------------------------------------------------------
39 
40 // Utility functions -----------------------------------------------------------
41 _gs_ramp* InitRamp(float* value, float set, float frames, bigtime_t duration)
42 {
43 	float diff = (set > *value) ? set - *value : *value - set;
44 	bigtime_t sec = bigtime_t(duration / 1000000.0);
45 	float inc = diff * 200;
46 
47 	_gs_ramp* ramp = new _gs_ramp;
48 	ramp->value = value;
49 
50 	ramp->frame_total = frames * sec;
51 	ramp->frame_inc = int(ramp->frame_total / inc);
52 
53 	ramp->inc = (set - *value) / inc;
54 
55 	ramp->frame_count = 0;
56 	ramp->frame_inc_count = 0;
57 
58 	ramp->duration = duration;
59 
60 	return ramp;
61 }
62 
63 bool ChangeRamp(_gs_ramp* ramp)
64 {
65 	if (ramp->frame_count > ramp->frame_total) return true;
66 
67 	if (ramp->frame_inc_count >= ramp->frame_inc)
68 	{
69 		ramp->frame_inc_count = 0;
70 		*ramp->value += ramp->inc;
71 	}
72 	else ramp->frame_inc_count++;
73 
74 	ramp->frame_count++;
75 	return false;
76 }
77 
78 size_t get_sample_size(int32 format)
79 {
80 	size_t sample;
81 
82 	switch(format)
83 	{
84 		case media_raw_audio_format::B_AUDIO_CHAR:
85 			sample  = sizeof(char);
86 			break;
87 
88 		case gs_audio_format::B_GS_U8:
89 			sample = sizeof(uint8);
90 			break;
91 
92 		case gs_audio_format::B_GS_S16:
93 			sample = sizeof(int16);
94 			break;
95 
96 		case gs_audio_format::B_GS_S32:
97 			sample = sizeof(int32);
98 			break;
99 
100 		case gs_audio_format::B_GS_F:
101 			sample = sizeof(float);
102 			break;
103 
104 		default: sample = 0;
105 	}
106 
107 	return sample;
108 }
109 
110 void media_to_gs_format(gs_audio_format* dest, media_raw_audio_format* source)
111 {
112 	dest->format = source->format;
113 	dest->frame_rate = source->frame_rate;
114 	dest->channel_count = source->channel_count;
115 	dest->byte_order = source->byte_order;
116 	dest->buffer_size = source->buffer_size;
117 }
118