-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSlice.c
181 lines (151 loc) · 4.38 KB
/
Slice.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
49
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <malloc.h>
#include "Slice.h"
#include "time.h"
//Methods for Slice
int SliceCreate( struct AudioObject Object, char * mode, int Pos, int Size, int key)
{
int i;
struct Slice *next = &EmptySlice;
struct Slice *previous = EmptySlice.previous;
int index=0;
//Find the last slice
while (next->next != NULL) {
next = next->next;
previous = next;
index++;
}
//Allocate memory
next->next = (struct Slice*) malloc(sizeof(struct Slice));
//Copy Data
next->next->length = Size;
next->next->playmode = mode;
next->next->pos= 0;
next->next->isPlaying = 0;
next->next->previous= next;
next->next->next= NULL;
next->next->index = index +1 ;
next->next->source = Object;
next->next->key = key;
next->next->stream = malloc(Size * sizeof(float));
for(i=0; i<Size; i++)
{
next->next->stream[i] = Object.stream[Pos+i] ;
}
//Initialize basic values for envelope
next->next->envelope.attack_t = next->next->length*0.4;
next->next->envelope.decay_v = 0.85;
next->next->envelope.decay_t = next->next->length*0.5;
next->next->envelope.sustain_v = 0.8;
next->next->envelope.sustain_t = next->next->length*0.6;
next->next->envelope.release_t = next->next->length*0.7;
//Initialize playback
next->next->playback.state = "done";
next->next->playback.pos = 0;
next->next->playback.next = NULL;
return 0;
}
int SliceDelete(int index)
{
int found = 1;
struct Slice *next = &EmptySlice;
while(next->next != NULL) {
if (next->next->index == index)
{
next->next = next->next->next;
next->next->next->previous = next->next->previous;
free(next->next);
found = 0;
}
next= next->next;
}
return found;
}
int SliceResize(int index, int pos, int size)
{
int found = 1;
struct Slice *next = &EmptySlice;
while(next->next !=NULL) {
if (next->next->index == index)
{
int i;
next->next->stream = malloc(size * sizeof(float));
for(i =0; i<size; i++)
{
next->next->stream[i] = next->next->source.stream[pos+i];
}
found = 0;
}
}
return found;
}
int SliceAutoSlice( struct AudioObject Object, char * mode, int div)
{
int i=0;
while(i<div)
{
SliceCreate(Object, mode, (Object.size/div) * i, (int) Object.size/div, 60+i);
i++;
}
return 0;
}
float * SliceGenerateSample(struct Slice * slice, int nframe, int speed)
{
float * stream = malloc(sizeof(float)*nframe);
int i;
if(slice->pos + nframe < slice->length)
{
for(i=0; i<nframe; i++)
{
stream[i]=slice->stream[i + slice->pos];
}
slice->pos+=nframe;
}
else {
slice->pos = 0;
for(i=0; i<nframe;i++)
{
stream[i]=0;
}
}
return stream;
}
float SliceVolume(struct Slice * slice, int pos)
{
if (pos >= 0 && pos < slice->envelope.decay_t)
{
return (slice->envelope.decay_v) * (pos / slice->envelope.attack_t) ;
}
if (pos >= slice->envelope.decay_t && pos < slice->envelope.sustain_t)
{
return (slice->envelope.decay_v) - (slice->envelope.decay_v - slice->envelope.sustain_v)*((pos-slice->envelope.attack_t)/slice->envelope.decay_t);
}
if ( pos >= slice->envelope.sustain_t && pos < slice->envelope.release_t)
{
return slice->envelope.sustain_v;
}
if ( pos >= slice->envelope.release_t && pos <= slice->length )
{
return slice->envelope.sustain_v - (slice->envelope.sustain_v * ((pos- slice->envelope.release_t)/(slice->length - slice->envelope.release_t)));
}
return 0;
}
int SliceStartPlayback(struct Slice * slice)
{
struct Playback * current;
//find an available playback in the list or the last one if there isnt any
for(current = &slice->playback; current->next != NULL || current->next->state != "done"; current = current->next){}
if(current->next==NULL)
{
current->next = malloc(sizeof(struct Playback));
current->next->pos = 0;
current->next->state = "playing";
current->next->next = NULL;
}
else if (current->next->state == "done")
{
current->next->pos =0;
current->next->state = "playing";
}
return 0;
}