serialize_instrument.c 3.12 KB
Newer Older
1
/* serialize_instrument.c -- Serializer for instrument data
Erik Faye-Lund's avatar
Erik Faye-Lund committed
2
 * Copyright (C) 2005-2006 Jørn Nystad and Erik Faye-Lund
3
4
5
 * For conditions of distribution and use, see copyright notice in LICENSE.TXT
 */

Erik Faye-Lund's avatar
Erik Faye-Lund committed
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include "../src/pimp_module.h"
#include "../src/pimp_mixer.h"
#include "serializer.h"

#if 0
#include <stdio.h>
#define TRACE() printf("AT %s:%d\n", __FILE__, __LINE__);
#else
#define TRACE()
#endif

Erik Faye-Lund's avatar
Erik Faye-Lund committed
18
int serialize_sample(struct serializer *s, const struct pimp_sample *samp)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
	int pos;
	
	ASSERT(NULL != samp);
	ASSERT(NULL != s);
	TRACE();
	
	serializer_align(s, 4);
	pos = s->pos;
	
	serialize_word(s, samp->data_ptr);
	serialize_word(s, samp->length);
	serialize_word(s, samp->loop_start);
	serialize_word(s, samp->loop_length);
	
	serialize_halfword(s, samp->fine_tune);
	serialize_halfword(s, samp->rel_note);
	
	serialize_byte(s, samp->volume);
	serialize_byte(s, samp->loop_type);
	serialize_byte(s, samp->pan);
	
	serialize_byte(s, samp->vibrato_speed);
	serialize_byte(s, samp->vibrato_depth);
	serialize_byte(s, samp->vibrato_sweep);
	serialize_byte(s, samp->vibrato_waveform);
	
	return pos;
}

Erik Faye-Lund's avatar
Erik Faye-Lund committed
49
int serialize_envelope(struct serializer *s, const struct pimp_envelope *env)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{
	int pos, i;
	
	ASSERT(NULL != env);
	ASSERT(NULL != s);
	TRACE();
	
	serializer_align(s, 4);
	pos = s->pos;
	
	for (i = 0; i < 25; ++i)
	{
		serialize_halfword(s, env->node_tick[i]);
	}
	
	for (i = 0; i < 25; ++i)
	{
		serialize_halfword(s, env->node_magnitude[i]);
	}
	
	for (i = 0; i < 25; ++i)
	{
		serialize_halfword(s, env->node_delta[i]);
	}
	
	serialize_byte(s, env->node_count);
	serialize_byte(s, env->flags);
	
	serialize_byte(s, env->loop_start);
	serialize_byte(s, env->loop_end);
	
	serialize_byte(s, env->sustain_loop_start);
	serialize_byte(s, env->sustain_loop_end);
	
	return pos;
}

/* dumps all samples in an instrument, returns position of first sample */
Erik Faye-Lund's avatar
Erik Faye-Lund committed
88
void serialize_instrument_data(struct serializer *s, const struct pimp_instrument *instr)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
89
{
Erik Faye-Lund's avatar
Erik Faye-Lund committed
90
	struct pimp_envelope *vol_env, *pan_env;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
91
92
93
94
95
96
97
98
99
100
101
	int i;
	
	ASSERT(NULL != instr);
	ASSERT(NULL != s);
	TRACE();
	
	serializer_align(s, 4);
	serializer_set_pointer(s, pimp_get_ptr(&instr->sample_ptr), s->pos);
	
	for (i = 0; i < instr->sample_count; ++i)
	{
102
		serialize_sample(s, pimp_instrument_get_sample(instr, i));
Erik Faye-Lund's avatar
Erik Faye-Lund committed
103
104
	}
	
105
	vol_env = pimp_instrument_get_vol_env(instr);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
106
107
	if (NULL != vol_env) serializer_set_pointer(s, pimp_get_ptr(&instr->vol_env_ptr), serialize_envelope(s, vol_env));
	
108
	pan_env = pimp_instrument_get_pan_env(instr);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
109
110
111
112
	if (NULL != pan_env) serializer_set_pointer(s, pimp_get_ptr(&instr->pan_env_ptr), serialize_envelope(s, pan_env));
}

/* dump instrument structure, return position */
Erik Faye-Lund's avatar
Erik Faye-Lund committed
113
void serialize_instrument(struct serializer *s, const struct pimp_instrument *instr)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{
	int i;
	
	ASSERT(NULL != instr);
	ASSERT(NULL != s);
	TRACE();
	
	serializer_align(s, 4);
	serialize_pointer(s, pimp_get_ptr(&instr->sample_ptr));
	serialize_pointer(s, pimp_get_ptr(&instr->vol_env_ptr));
	serialize_pointer(s, pimp_get_ptr(&instr->pan_env_ptr));
	
	serialize_halfword(s, instr->volume_fadeout);
	serialize_byte(s, instr->sample_count);
	
	for (i = 0; i < 120; ++i)
	{
		serialize_byte(s, instr->sample_map[i]);
	}
}