xref: /haiku/src/kits/game/GSUtility.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*
2  * Copyright 2001-2002, Haiku Inc.
3  * Authors:
4  *		Christopher ML Zumwalt May (zummy@users.sf.net)
5  *
6  * Distributed under the terms of the MIT License.
7  */
8 
9 #include <GameSoundDefs.h>
10 #include <MediaDefs.h>
11 
12 #include "GSUtility.h"
13 
14 
15 _gs_ramp*
16 InitRamp(float* value, float set, float frames, bigtime_t duration)
17 {
18 	float diff = (set > *value) ? set - *value : *value - set;
19 	bigtime_t sec = bigtime_t(duration / 1000000.0);
20 	float inc = diff * 200;
21 
22 	_gs_ramp* ramp = new _gs_ramp;
23 	ramp->value = value;
24 
25 	ramp->frame_total = frames * sec;
26 	ramp->frame_inc = int(ramp->frame_total / inc);
27 
28 	ramp->inc = (set - *value) / inc;
29 
30 	ramp->frame_count = 0;
31 	ramp->frame_inc_count = 0;
32 
33 	ramp->duration = duration;
34 
35 	return ramp;
36 }
37 
38 
39 bool
40 ChangeRamp(_gs_ramp* ramp)
41 {
42 	if (ramp->frame_count > ramp->frame_total)
43 		return true;
44 
45 	if (ramp->frame_inc_count >= ramp->frame_inc) {
46 		ramp->frame_inc_count = 0;
47 		*ramp->value += ramp->inc;
48 	}
49 	else
50 		ramp->frame_inc_count++;
51 
52 	ramp->frame_count++;
53 	return false;
54 }
55 
56 
57 size_t
58 get_sample_size(int32 format)
59 {
60 	size_t sample;
61 
62 	switch(format) {
63 		case media_raw_audio_format::B_AUDIO_CHAR:
64 			sample  = sizeof(char);
65 			break;
66 
67 		case gs_audio_format::B_GS_U8:
68 			sample = sizeof(uint8);
69 			break;
70 
71 		case gs_audio_format::B_GS_S16:
72 			sample = sizeof(int16);
73 			break;
74 
75 		case gs_audio_format::B_GS_S32:
76 			sample = sizeof(int32);
77 			break;
78 
79 		case gs_audio_format::B_GS_F:
80 			sample = sizeof(float);
81 			break;
82 
83 		default: sample = 0;
84 	}
85 
86 	return sample;
87 }
88 
89 
90 void
91 media_to_gs_format(gs_audio_format* dest, media_raw_audio_format* source)
92 {
93 	dest->format = source->format;
94 	dest->frame_rate = source->frame_rate;
95 	dest->channel_count = source->channel_count;
96 	dest->byte_order = source->byte_order;
97 	dest->buffer_size = source->buffer_size;
98 }
99