xref: /haiku/src/kits/game/GSUtility.cpp (revision 3be9edf8da228afd9fec0390f408c964766122aa)
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 <new>
13 
14 #include "GSUtility.h"
15 
16 
17 _gs_ramp*
18 InitRamp(float* value, float set, float frames, bigtime_t duration)
19 {
20 	float diff = (set > *value) ? set - *value : *value - set;
21 	bigtime_t sec = bigtime_t(duration / 1000000.0);
22 	float inc = diff * 200;
23 
24 	_gs_ramp* ramp = new (std::nothrow) _gs_ramp;
25 	if (ramp != NULL) {
26 		ramp->value = value;
27 
28 		ramp->frame_total = frames * sec;
29 		ramp->frame_inc = int(ramp->frame_total / inc);
30 
31 		ramp->inc = (set - *value) / inc;
32 
33 		ramp->frame_count = 0;
34 		ramp->frame_inc_count = 0;
35 
36 		ramp->duration = duration;
37 	}
38 	return ramp;
39 }
40 
41 
42 bool
43 ChangeRamp(_gs_ramp* ramp)
44 {
45 	if (ramp->frame_count > ramp->frame_total)
46 		return true;
47 
48 	if (ramp->frame_inc_count >= ramp->frame_inc) {
49 		ramp->frame_inc_count = 0;
50 		*ramp->value += ramp->inc;
51 	} else
52 		ramp->frame_inc_count++;
53 
54 	ramp->frame_count++;
55 	return false;
56 }
57 
58 
59 size_t
60 get_sample_size(int32 format)
61 {
62 	size_t sample;
63 
64 	switch(format) {
65 		case media_raw_audio_format::B_AUDIO_CHAR:
66 			sample  = sizeof(char);
67 			break;
68 
69 		case gs_audio_format::B_GS_U8:
70 			sample = sizeof(uint8);
71 			break;
72 
73 		case gs_audio_format::B_GS_S16:
74 			sample = sizeof(int16);
75 			break;
76 
77 		case gs_audio_format::B_GS_S32:
78 			sample = sizeof(int32);
79 			break;
80 
81 		case gs_audio_format::B_GS_F:
82 			sample = sizeof(float);
83 			break;
84 
85 		default:
86 			sample = 0;
87 			break;
88 	}
89 
90 	return sample;
91 }
92 
93 
94 void
95 media_to_gs_format(gs_audio_format* dest, media_raw_audio_format* source)
96 {
97 	dest->format = source->format;
98 	dest->frame_rate = source->frame_rate;
99 	dest->channel_count = source->channel_count;
100 	dest->byte_order = source->byte_order;
101 	dest->buffer_size = source->buffer_size;
102 }
103