[12] | 1 | //play.c: |
---|
| 2 | |
---|
[702] | 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013 |
---|
[702] | 5 | * |
---|
| 6 | * This file is part of roarfish 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. |
---|
[702] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[12] | 26 | #include "roarfish.h" |
---|
| 27 | |
---|
| 28 | struct roarfish_play_inst { |
---|
[4908] | 29 | roar_vs_t * vss; |
---|
| 30 | int _roarfh; |
---|
| 31 | int _begun; |
---|
[12] | 32 | FishSoundInfo fsinfo; |
---|
| 33 | FishSound * fsound; |
---|
| 34 | }; |
---|
| 35 | |
---|
| 36 | int decoded_float (FishSound * fsound, float ** pcm, long frames, void * user_data) { |
---|
| 37 | struct roarfish_play_inst * inst = (struct roarfish_play_inst*) user_data; |
---|
| 38 | int16_t * data; |
---|
| 39 | int i; |
---|
[4908] | 40 | float s; |
---|
[12] | 41 | |
---|
[4908] | 42 | if (inst->vss == NULL) { |
---|
[12] | 43 | fish_sound_command (fsound, FISH_SOUND_GET_INFO, &(inst->fsinfo), |
---|
| 44 | sizeof (FishSoundInfo)); |
---|
[4908] | 45 | |
---|
| 46 | if ( (inst->vss = roar_vs_new_playback(NULL, "roarfish", inst->fsinfo.samplerate, inst->fsinfo.channels, |
---|
| 47 | ROAR_CODEC_DEFAULT, 16, NULL)) == NULL ) { |
---|
[12] | 48 | return -1; |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
[4908] | 52 | if ( (data = roar_mm_malloc(frames*2*inst->fsinfo.channels)) == NULL ) { |
---|
[12] | 53 | return -1; |
---|
| 54 | } |
---|
| 55 | |
---|
[379] | 56 | frames *= inst->fsinfo.channels; |
---|
| 57 | |
---|
[12] | 58 | for (i = 0; i < frames; i++) { |
---|
| 59 | s = ((float*)pcm)[i]; |
---|
[4908] | 60 | s *= 32767.f; |
---|
[12] | 61 | data[i] = s; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
[5270] | 65 | if ( roar_vs_write(inst->vss, (char*)data, frames * 2, NULL) != (ssize_t)(frames * 2) ) { |
---|
| 66 | return -1; |
---|
| 67 | } |
---|
[12] | 68 | |
---|
| 69 | |
---|
[4908] | 70 | roar_mm_free(data); |
---|
[12] | 71 | |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | int read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data) { |
---|
| 76 | FishSound * fsound = (FishSound *)user_data; |
---|
| 77 | |
---|
[5270] | 78 | (void)oggz, (void)serialno; |
---|
| 79 | |
---|
| 80 | fish_sound_prepare_truncation(fsound, op->granulepos, op->e_o_s); |
---|
| 81 | fish_sound_decode(fsound, op->packet, op->bytes); |
---|
[12] | 82 | |
---|
| 83 | return 0; |
---|
| 84 | } |
---|
| 85 | |
---|
[5270] | 86 | int play (const char * file, char ** opts) { |
---|
[12] | 87 | OGGZ * oggz; |
---|
| 88 | struct roarfish_play_inst inst; |
---|
| 89 | |
---|
[5270] | 90 | (void)opts; |
---|
| 91 | |
---|
[4908] | 92 | inst.vss = NULL; |
---|
[12] | 93 | |
---|
| 94 | inst.fsound = fish_sound_new(FISH_SOUND_DECODE, &inst.fsinfo); |
---|
| 95 | fish_sound_set_interleave(inst.fsound, 1); |
---|
| 96 | |
---|
| 97 | fish_sound_set_decoded_float_ilv(inst.fsound, decoded_float, (void*)&inst); |
---|
| 98 | |
---|
| 99 | if ((oggz = oggz_open(file, OGGZ_READ)) == NULL) { |
---|
| 100 | ROAR_ERR("Can not open input file: %s", file); |
---|
| 101 | return -1; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | oggz_set_read_callback(oggz, -1, read_packet, inst.fsound); |
---|
| 105 | |
---|
| 106 | // TODO: add some status display here? |
---|
| 107 | while (oggz_read(oggz, 1024)); |
---|
| 108 | |
---|
| 109 | oggz_close(oggz); |
---|
| 110 | |
---|
| 111 | fish_sound_delete(inst.fsound); |
---|
| 112 | |
---|
[4908] | 113 | if ( inst.vss != NULL ) |
---|
| 114 | roar_vs_close(inst.vss, ROAR_VS_FALSE, NULL); |
---|
| 115 | |
---|
| 116 | return 0; |
---|
[12] | 117 | } |
---|
| 118 | |
---|
| 119 | //ll |
---|