aboutsummaryrefslogtreecommitdiff
path: root/stm32/src/sfx.c
blob: f80c8d15801a322d861c1c8241e54913453bb3ab (plain)
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
/*!
 * Sound effect player library.
 *
 * This implements a state machine for playing back various monophonic
 * sound effects such as morse code symbols, clicks and alert tones.
 *
 * Author Stuart Longland <[email protected]>
 * Copyright (C) 2015 FreeDV project.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1,
 * as published by the Free Software Foundation.  This program is
 * distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include "sfx.h"

static void sfx_next_tone(struct sfx_player_t* const sfx_player)
{
	struct tone_gen_t* tone_gen = &(sfx_player->tone_gen);
	const struct sfx_note_t* note = sfx_player->note;

	if (!note) {
		tone_reset(tone_gen, 0, 0);
	} else {
		tone_reset(tone_gen, note->freq, note->duration);

		if (!note->duration)
			/* We are done */
			sfx_player->note = NULL;
		else
			/* Move to next note */
			sfx_player->note++;
	}
}

/*!
 * Start playing a particular effect.
 * @param	sfx_player	Effect player state machine
 * @param	effect		Pointer to sound effect (NULL == stop)
 */
void sfx_play(struct sfx_player_t* const sfx_player,
		const struct sfx_note_t* effect)
{
	sfx_player->note = effect;
	sfx_next_tone(sfx_player);
}

/*!
 * Retrieve the next sample to be played.
 */
int16_t sfx_next(struct sfx_player_t* const sfx_player)
{
	if (!sfx_player)
		return(0);
	if (!sfx_player->tone_gen.remain)
		sfx_next_tone(sfx_player);
	return tone_next(&(sfx_player->tone_gen));
}