source: roaraudio/roarfish/play.c @ 12:78dfd1b91bcc

Last change on this file since 12:78dfd1b91bcc was 12:78dfd1b91bcc, checked in by phi, 16 years ago

added playback code

File size: 1.8 KB
Line 
1//play.c:
2
3#include "roarfish.h"
4
5struct roarfish_play_inst {
6 int roarfh;
7 int begun;
8 FishSoundInfo fsinfo;
9 FishSound * fsound;
10};
11
12int decoded_float (FishSound * fsound, float ** pcm, long frames, void * user_data) {
13 struct roarfish_play_inst * inst = (struct roarfish_play_inst*) user_data;
14 int16_t * data;
15 int i;
16 double s;
17
18 if (!inst->begun) {
19   fish_sound_command (fsound, FISH_SOUND_GET_INFO, &(inst->fsinfo),
20                       sizeof (FishSoundInfo));
21   if ( (inst->roarfh = roar_simple_play(inst->fsinfo.samplerate, inst->fsinfo.channels,
22                                         16, ROAR_CODEC_NATIVE, NULL, "roarfish")) == -1 ) {
23    return -1;
24   }
25   inst->begun = 1;
26 }
27
28 if ( (data = malloc(frames*2)) == NULL ) {
29  return -1;
30 }
31
32 for (i = 0; i < frames; i++) {
33  s  = ((float*)pcm)[i];
34  s *= 32767;
35  data[i] = s;
36 }
37
38
39 write(inst->roarfh, (char*)data, frames * 2);
40
41
42 free(data);
43
44 return 0;
45}
46
47int read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data) {
48 FishSound * fsound = (FishSound *)user_data;
49
50 fish_sound_prepare_truncation (fsound, op->granulepos, op->e_o_s);
51 fish_sound_decode (fsound, op->packet, op->bytes);
52
53 return 0;
54}
55
56int play (char * file, char ** opts) {
57 OGGZ * oggz;
58 struct roarfish_play_inst inst;
59
60 inst.roarfh = -1;
61 inst.begun  = 0;
62
63 inst.fsound = fish_sound_new(FISH_SOUND_DECODE, &inst.fsinfo);
64 fish_sound_set_interleave(inst.fsound, 1);
65
66 fish_sound_set_decoded_float_ilv(inst.fsound, decoded_float, (void*)&inst);
67
68 if ((oggz = oggz_open(file, OGGZ_READ)) == NULL) {
69  ROAR_ERR("Can not open input file: %s", file);
70  return -1;
71 }
72
73 oggz_set_read_callback(oggz, -1, read_packet, inst.fsound);
74
75 // TODO: add some status display here?
76 while (oggz_read(oggz, 1024));
77
78 oggz_close(oggz);
79
80 fish_sound_delete(inst.fsound);
81
82 return -1;
83}
84
85//ll
Note: See TracBrowser for help on using the repository browser.