[41] | 1 | //sample.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[668] | 5 | * |
---|
| 6 | * This file is part of roard a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[668] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[41] | 26 | #include "roard.h" |
---|
| 27 | |
---|
[5192] | 28 | // declared 'extern' |
---|
| 29 | struct roar_sample * g_samples[ROAR_SAMPLES_MAX]; |
---|
| 30 | |
---|
[44] | 31 | int samples_init (void) { |
---|
| 32 | int i; |
---|
| 33 | |
---|
| 34 | for (i = 0; i < ROAR_SAMPLES_MAX; i++) |
---|
| 35 | g_samples[i] = NULL; |
---|
| 36 | |
---|
| 37 | return 0; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | |
---|
[45] | 41 | int samples_free (void) { |
---|
| 42 | int i; |
---|
| 43 | |
---|
| 44 | for (i = 0; i < ROAR_SAMPLES_MAX; i++) |
---|
| 45 | if ( g_samples[i] != NULL ) |
---|
| 46 | samples_delete(i); |
---|
| 47 | |
---|
| 48 | return 0; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | int samples_new (void) { |
---|
[4957] | 52 | struct roar_sample * c = roar_mm_malloc(sizeof(struct roar_sample)); |
---|
[45] | 53 | int i; |
---|
| 54 | |
---|
| 55 | if ( c == NULL ) |
---|
| 56 | return -1; |
---|
| 57 | |
---|
| 58 | c->name[0] = 0; |
---|
| 59 | c->data = NULL; |
---|
| 60 | |
---|
| 61 | for (i = 0; i < ROAR_SAMPLES_MAX; i++) { |
---|
| 62 | if ( g_samples[i] == NULL ) { |
---|
| 63 | g_samples[i] = c; |
---|
| 64 | return i; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
[4957] | 68 | roar_mm_free(c); |
---|
[45] | 69 | |
---|
| 70 | return -1; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | int samples_delete (int id) { |
---|
| 74 | struct roar_sample * c = g_samples[id]; |
---|
| 75 | |
---|
| 76 | if ( c == NULL ) |
---|
| 77 | return -1; |
---|
| 78 | |
---|
| 79 | if (c->data != NULL ) |
---|
| 80 | roar_buffer_free(c->data); |
---|
| 81 | |
---|
[4975] | 82 | roar_mm_free(c); |
---|
[45] | 83 | |
---|
| 84 | g_samples[id] = NULL; |
---|
| 85 | |
---|
| 86 | return 0; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | int samples_set_name (int id, char * name) { |
---|
| 90 | struct roar_sample * c = g_samples[id]; |
---|
| 91 | |
---|
| 92 | if ( c == NULL ) |
---|
| 93 | return -1; |
---|
| 94 | |
---|
| 95 | strncpy(c->name, name, ROAR_BUFFER_NAME-1); |
---|
| 96 | |
---|
| 97 | c->name[ROAR_BUFFER_NAME-1] = 0; |
---|
| 98 | |
---|
| 99 | return 0; |
---|
| 100 | } |
---|
[44] | 101 | |
---|
[50] | 102 | int samples_add_data (int id, void * data, size_t len) { |
---|
| 103 | struct roar_buffer * new; |
---|
| 104 | struct roar_sample * c = g_samples[id]; |
---|
[5162] | 105 | int save_err; |
---|
[50] | 106 | |
---|
| 107 | if ( c == NULL ) |
---|
| 108 | return -1; |
---|
| 109 | |
---|
| 110 | if ( roar_buffer_new(&new, len) == -1 ) |
---|
| 111 | return -1; |
---|
| 112 | |
---|
| 113 | if ( c->data == NULL ) { |
---|
| 114 | c->data = new; |
---|
| 115 | } else { |
---|
[5301] | 116 | if ( roar_buffer_moveinto(c->data, &new) == -1 ) { |
---|
[5162] | 117 | save_err = roar_error; |
---|
| 118 | roar_buffer_free(new); |
---|
| 119 | roar_err_set(save_err); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
[50] | 122 | } |
---|
| 123 | |
---|
| 124 | return 0; |
---|
| 125 | } |
---|
| 126 | |
---|
[41] | 127 | //ll |
---|