summaryrefslogtreecommitdiff
path: root/src/sound.c
blob: 73bd6d49e43bf9cae8cefd9eab1cdf439984c566 (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
#include <stdlib.h>
#include <string.h>
#include <mossrose.h>
#include "sound.h"


int sound_copy(struct mossrose_sound_t *dest, struct mossrose_sound_t *src)
{
	sound_free_audio(dest);
	dest->len = src->len;
	dest->mono = src->mono;
	
	/* left channel */
	if (src->left != NULL) {
		dest->left = malloc(src->len * sizeof(float));
		if (dest->left == NULL) return 1;
		memcpy(dest->left, src->left, src->len * sizeof(float));
	}
	else
		dest->left = NULL;

	/* right channel */
	if (src->right != NULL) {
		dest->right = malloc(src->len * sizeof(float));
		if (dest->right == NULL) return 1;
		memcpy(dest->right, src->right, src->len * sizeof(float));
	}
	else
		dest->right = NULL;

	return 0;
}


void sound_free_audio(struct mossrose_sound_t *sound)
{
	if (sound->left != NULL) {
		free(sound->left);
		sound->left = NULL;
	}
	if (sound->right != NULL) {
		free(sound->right);
		sound->right = NULL;
	}
}