Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 96fbe824ef9c41518d42efb9ef9f93e6571765f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
   audio/vorbis.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "string.h"

#include <iostream>
#include <string>

#include "audio/vorbisfile.h"
#include "filesystem/filesystem.h"
#include "sys/sys.h"

#ifdef _WIN32
/*
	READ vorbisfile.h!
	This works because I have a clean development environment
*/
#define OV_EXCLUDE_STATIC_CALLBACKS
#endif

#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>

namespace audio
{

PCM *Vorbis::load(std::string const & filename)
{
	if (!filename.size())
		return 0;

	filesystem::File *fs_file = filesystem::open(filename.c_str());
	if (!fs_file) {
		return 0;
	}
	
	std::string vorbis_path(fs_file->path());
	vorbis_path.append(fs_file->name());
	filesystem::close(fs_file);

	OggVorbis_File vorbis_file;
	// workaround for Ubuntu 10: ov_fopen parameter 1 is char * instead of const char *
	char tmpstr[vorbis_path.size()+1];
	::strncpy((char *)tmpstr, vorbis_path.c_str(), vorbis_path.size());
	if (ov_fopen(tmpstr, &vorbis_file) < 0 ) {
		return 0;
	}
		
	vorbis_info* vorbis_streaminfo;	
	vorbis_streaminfo = ov_info(&vorbis_file, -1);

	unsigned int samplerate = vorbis_streaminfo->rate;
	unsigned int channels  = vorbis_streaminfo->channels;
	unsigned int bitspersample = 16;			// always.

	const size_t blocksize = 1024*1024; // 1Mb blocks	
	
	PCM *pcm = new PCM(samplerate, bitspersample, channels, blocksize);
	pcm->set_size(0);
	
	long bytesread = 0;
	
	do {
		// enlarge the pcm buffer if required
		if (pcm->size() + 4096 > pcm->buff_size()) {
			pcm->grow(pcm->buff_size() + blocksize);
		}
		bytesread = ov_read(&vorbis_file, (char *)pcm->data() + pcm->size(),4096,0,2,1,0);
		pcm->set_size( pcm->size() + bytesread);
	} while (bytesread > 0);

	ov_clear(&vorbis_file);	
	con_debug << "  " << filename << " " << pcm->samplerate() << "Hz " << pcm->bitspersample() << "bit "
		  << pcm->channels() << " chan " << pcm->size() << " bytes" << std::endl;
	
	return pcm;
}

}