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