Skip to content

Commit faf746b

Browse files
Allow to trigger samples
Add stereo support
1 parent d3ce7fb commit faf746b

File tree

2 files changed

+99
-6
lines changed

2 files changed

+99
-6
lines changed

src/ml_scratch.cpp

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ struct scratch_sample_s
6969
uint32_t sample_count;
7070
uint8_t note;
7171
uint32_t pos = 0;
72+
uint16_t volume;
7273

7374
float pitch;
7475
float pos_f;
76+
77+
bool loop;
78+
bool stereo;
7579
};
7680

7781
struct scratch_s
@@ -147,6 +151,16 @@ bool Scratch_AddSampleStatic(const uint8_t *data, uint32_t size, uint8_t idx)
147151
newSample->data = wav_data;
148152
newSample->size = (wavHdr->nextTag.tag_data_size);
149153
newSample->sample_count = (wavHdr->nextTag.tag_data_size / wavHdr->bytesPerSample);
154+
newSample->loop = true;
155+
newSample->volume = 1 << 15;
156+
if (wavHdr->numberOfChannels == 2)
157+
{
158+
newSample->stereo = true;
159+
}
160+
else
161+
{
162+
newSample->stereo = false;
163+
}
150164

151165
scratch.sample_count++;
152166

@@ -167,15 +181,49 @@ void Scratch_ProcessSample(Q1_14 *samples_l, Q1_14 *samples_r, uint32_t len, str
167181
{
168182
for (uint32_t n = 0 ; n < len; n++)
169183
{
184+
170185
sample->pos = sample->pos_f;
186+
if (sample->stereo)
187+
{
188+
sample->pos -= sample->pos % 2;
189+
}
190+
191+
int32_t s;
192+
193+
s = sample->samples[sample->pos] / 4;
194+
s *= sample->volume;
195+
s >>= 15;
196+
samples_l[n].s16 += s;
197+
if (sample->stereo)
198+
{
199+
sample->pos++;
171200

172-
samples_l[n].s16 += sample->samples[sample->pos] / 4;
173-
samples_r[n].s16 += sample->samples[sample->pos] / 4;
201+
s = sample->samples[sample->pos] / 4;
202+
s *= sample->volume;
203+
s >>= 15;
204+
}
205+
206+
samples_r[n].s16 += s;
174207

175208
sample->pos_f += sample->pitch;
176-
if (sample->pos_f >= sample->sample_count)
209+
210+
uint32_t sample_end_count = sample->sample_count;
211+
if (sample->stereo)
212+
{
213+
sample_end_count *= 2;
214+
}
215+
216+
if (sample->pos_f >= sample_end_count)
177217
{
178-
sample->pos_f -= sample->sample_count;
218+
if (sample->loop)
219+
{
220+
sample->pos_f -= sample->sample_count;
221+
}
222+
else
223+
{
224+
sample->pos_f = 0;
225+
sample->pitch = 0;
226+
}
179227
}
180228
if (sample->pos_f < 0)
181229
{
@@ -186,8 +234,50 @@ void Scratch_ProcessSample(Q1_14 *samples_l, Q1_14 *samples_r, uint32_t len, str
186234

187235
void Scratch_Process(Q1_14 *samples_l, Q1_14 *samples_r, uint32_t len)
188236
{
189-
for (uint8_t n = 0; n<scratch.sample_count; n++)
237+
for (uint8_t n = 0; n < scratch.sample_count; n++)
238+
{
239+
Scratch_ProcessSample(samples_l, samples_r, len, &scratch.samples[n]);
240+
}
241+
}
242+
243+
void Scratch_TriggerSample(uint8_t idx)
244+
{
245+
if (idx < SCRATCH_SAMPLE_COUNT)
190246
{
191-
Scratch_ProcessSample(samples_l, samples_r, len, &scratch.samples[n]);
247+
struct scratch_sample_s *sample = &scratch.samples[idx];
248+
249+
if (sample->stereo)
250+
{
251+
sample->pitch = 2;
252+
}
253+
else
254+
{
255+
sample->pitch = 1;
256+
}
257+
sample->loop = false;
258+
sample->pos_f = 0;
259+
}
260+
}
261+
262+
void Scratch_SetNoLoop(uint8_t idx)
263+
{
264+
if (idx < SCRATCH_SAMPLE_COUNT)
265+
{
266+
struct scratch_sample_s *sample = &scratch.samples[idx];
267+
268+
sample->loop = false;
269+
}
270+
}
271+
272+
void Scratch_SetVolume(float volume, uint8_t idx)
273+
{
274+
if (idx < SCRATCH_SAMPLE_COUNT)
275+
{
276+
struct scratch_sample_s *sample = &scratch.samples[idx];
277+
278+
uint16_t volume_max = 1 << 15;
279+
volume *= volume_max;
280+
281+
sample->volume = volume;
192282
}
193283
}

src/ml_scratch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ void Scratch_Init(float sample_rate);
4949
bool Scratch_AddSampleStatic(const uint8_t *data, uint32_t size, uint8_t idx);
5050
void Scratch_Process(Q1_14 *samples_l, Q1_14 *samples_r, uint32_t len);
5151
void Scratch_SetPitchAbs(float pitch, uint8_t idx);
52+
void Scratch_TriggerSample(uint8_t idx);
53+
void Scratch_SetNoLoop(uint8_t idx);
54+
void Scratch_SetVolume(float volume, uint8_t idx);
5255

5356

5457
#endif /* ML_SCRATCH_H_ */

0 commit comments

Comments
 (0)