[12] | 1 | //play.c: |
---|
| 2 | |
---|
[702] | 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
[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 { |
---|
| 29 | int roarfh; |
---|
| 30 | int begun; |
---|
| 31 | FishSoundInfo fsinfo; |
---|
| 32 | FishSound * fsound; |
---|
| 33 | }; |
---|
| 34 | |
---|
| 35 | int decoded_float (FishSound * fsound, float ** pcm, long frames, void * user_data) { |
---|
| 36 | struct roarfish_play_inst * inst = (struct roarfish_play_inst*) user_data; |
---|
| 37 | int16_t * data; |
---|
| 38 | int i; |
---|
| 39 | double s; |
---|
| 40 | |
---|
| 41 | if (!inst->begun) { |
---|
| 42 | fish_sound_command (fsound, FISH_SOUND_GET_INFO, &(inst->fsinfo), |
---|
| 43 | sizeof (FishSoundInfo)); |
---|
| 44 | if ( (inst->roarfh = roar_simple_play(inst->fsinfo.samplerate, inst->fsinfo.channels, |
---|
| 45 | 16, ROAR_CODEC_NATIVE, NULL, "roarfish")) == -1 ) { |
---|
| 46 | return -1; |
---|
| 47 | } |
---|
| 48 | inst->begun = 1; |
---|
| 49 | } |
---|
| 50 | |
---|
[379] | 51 | if ( (data = malloc(frames*2*inst->fsinfo.channels)) == NULL ) { |
---|
[12] | 52 | return -1; |
---|
| 53 | } |
---|
| 54 | |
---|
[379] | 55 | frames *= inst->fsinfo.channels; |
---|
| 56 | |
---|
[12] | 57 | for (i = 0; i < frames; i++) { |
---|
| 58 | s = ((float*)pcm)[i]; |
---|
| 59 | s *= 32767; |
---|
| 60 | data[i] = s; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | write(inst->roarfh, (char*)data, frames * 2); |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | free(data); |
---|
| 68 | |
---|
| 69 | return 0; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | int read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data) { |
---|
| 73 | FishSound * fsound = (FishSound *)user_data; |
---|
| 74 | |
---|
| 75 | fish_sound_prepare_truncation (fsound, op->granulepos, op->e_o_s); |
---|
| 76 | fish_sound_decode (fsound, op->packet, op->bytes); |
---|
| 77 | |
---|
| 78 | return 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | int play (char * file, char ** opts) { |
---|
| 82 | OGGZ * oggz; |
---|
| 83 | struct roarfish_play_inst inst; |
---|
| 84 | |
---|
| 85 | inst.roarfh = -1; |
---|
| 86 | inst.begun = 0; |
---|
| 87 | |
---|
| 88 | inst.fsound = fish_sound_new(FISH_SOUND_DECODE, &inst.fsinfo); |
---|
| 89 | fish_sound_set_interleave(inst.fsound, 1); |
---|
| 90 | |
---|
| 91 | fish_sound_set_decoded_float_ilv(inst.fsound, decoded_float, (void*)&inst); |
---|
| 92 | |
---|
| 93 | if ((oggz = oggz_open(file, OGGZ_READ)) == NULL) { |
---|
| 94 | ROAR_ERR("Can not open input file: %s", file); |
---|
| 95 | return -1; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | oggz_set_read_callback(oggz, -1, read_packet, inst.fsound); |
---|
| 99 | |
---|
| 100 | // TODO: add some status display here? |
---|
| 101 | while (oggz_read(oggz, 1024)); |
---|
| 102 | |
---|
| 103 | oggz_close(oggz); |
---|
| 104 | |
---|
| 105 | fish_sound_delete(inst.fsound); |
---|
| 106 | |
---|
| 107 | return -1; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | //ll |
---|