TORCS  1.3.9
The Open Racing Car Simulator
OggSoundStream.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : OggSoundStream.cpp
4  created : Fri Dec 23 17:35:18 CET 2011
5  copyright : (C) 2011 Bernhard Wymann
6  email : berniw@bluewin.ch
7  version : $Id$
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 /* Concrete implementation for ogg sound streams */
21 
22 #include "OggSoundStream.h"
23 #include <tgf.h>
24 
26  SoundStream(path),
27  valid(false),
28  rateInHz(0),
29  format(FORMAT_INVALID)
30 {
31  int result;
32 
33  if((result = ov_fopen(path, &oggStream)) < 0) {
34  GfError("OggSoundStream: Could not open Ogg stream: %s\n", errorString(result));
35  return;
36  }
37 
38  // fclose is not required here, the vorbis lib will take care of this on ov_clear.
39 
40  vorbis_info* vorbisInfo = ov_info(&oggStream, -1);
41  rateInHz = vorbisInfo->rate;
42 
43  if(vorbisInfo->channels == 1) {
45  } else {
47  }
48 
49  valid = true;
50 }
51 
52 
53 
54 
56 {
57  if (isValid()) {
58  ov_clear(&oggStream);
59  }
60 }
61 
62 
63 
64 
65 bool OggSoundStream::read(char* buffer, const int bufferSize, int* resultSize, const char** error)
66 {
67  if (!isValid()) {
68  *error = "OggSoundStream: Invalid, no data available.";
69  return false;
70  }
71 
72  int section;
73  int result;
74 
75  while(*resultSize < bufferSize) {
76  result = ov_read(&oggStream, buffer + *resultSize, bufferSize - *resultSize, 0, 2, 1, &section);
77 
78  if(result > 0) {
79  *resultSize += result;
80  } else {
81  if(result < 0) {
82  *error = errorString(result);
83  return false;
84  } else {
85  // Loop to the beginning of the stream
86  ov_time_seek(&oggStream, 0);
87  }
88  }
89  }
90 
91  if(*resultSize == 0) {
92  *error = "OggSoundStream: Read 0 bytes.";
93  return false;
94  }
95 
96  return true;
97 }
98 
99 
100 
101 
103 {
104  if (!isValid()) {
105  GfError("OggSoundStream: Invalid, no info available.\n");
106  return;
107  }
108 
109  ov_time_seek(&oggStream, 0);
110 }
111 
112 
113 
114 
116 {
117  if (!isValid()) {
118  GfError("OggSoundStream: Invalid, no info available.\n");
119  return;
120  }
121 
122  vorbis_info* vorbisInfo = ov_info(&oggStream, -1);
123  vorbis_comment* vorbisComment = ov_comment(&oggStream, -1);
124 
125  GfOut("version %d\n", vorbisInfo->version);
126  GfOut("channels %d\n", vorbisInfo->channels);
127  GfOut("rate (hz) %ld\n", vorbisInfo->rate);
128  GfOut("bitrate upper %ld\n", vorbisInfo->bitrate_upper);
129  GfOut("bitrate nominal %ld\n", vorbisInfo->bitrate_nominal);
130  GfOut("bitrate lower %ld\n", vorbisInfo->bitrate_lower);
131  GfOut("bitrate window %ld\n\n", vorbisInfo->bitrate_window);
132  GfOut("vendor %s\n", vorbisComment->vendor);
133 
134  int i;
135  for(i = 0; i < vorbisComment->comments; i++) {
136  GfOut(" %s\n", vorbisComment->user_comments[i]);
137  }
138 }
139 
140 
141 
142 
143 const char* OggSoundStream::errorString(int code)
144 {
145  switch(code)
146  {
147  case OV_EREAD:
148  return "OggSoundStream: Read from media.";
149  case OV_ENOTVORBIS:
150  return "OggSoundStream: Not Vorbis data.";
151  case OV_EVERSION:
152  return "OggSoundStream: Vorbis version mismatch.";
153  case OV_EBADHEADER:
154  return "OggSoundStream: Invalid Vorbis header.";
155  case OV_EFAULT:
156  return "OggSoundStream: Internal logic fault (bug or heap/stack corruption.";
157  default:
158  return "OggSoundStream: Unknown Ogg error.";
159  }
160 }
#define GfError
Definition: tgf.h:351
virtual bool read(char *buffer, const int bufferSize, int *resultSize, const char **error)
const char * errorString(int code)
Section header structure.
Definition: params.cpp:83
OggSoundStream(char *path)
bool valid(int s)
Definition: Convex.cpp:114
virtual ~OggSoundStream()
virtual bool isValid()
OggVorbis_File oggStream
#define GfOut
Definition: tgf.h:373
The Gaming Framework API.
virtual void display()
SoundFormat format
virtual void rewind()