comparison data/fileio/OggVorbisFileReader.cpp @ 148:1a42221a1522

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 11:49:58 +0000
parents
children 4b2ea82fd0ed
comparison
equal deleted inserted replaced
147:3a13b0d4934e 148:1a42221a1522
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #ifdef HAVE_OGGZ
17 #ifdef HAVE_FISHSOUND
18
19 #include "OggVorbisFileReader.h"
20 #include "base/Profiler.h"
21 #include "base/System.h"
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/mman.h>
26 #include <fcntl.h>
27 #include <cmath>
28
29 #include <QApplication>
30 #include <QFileInfo>
31 #include <QProgressDialog>
32
33 static int instances = 0;
34
35 OggVorbisFileReader::OggVorbisFileReader(QString path, bool showProgress,
36 CacheMode mode) :
37 CodedAudioFileReader(mode),
38 m_path(path),
39 m_progress(0),
40 m_fileSize(0),
41 m_bytesRead(0),
42 m_cancelled(false)
43 {
44 m_frameCount = 0;
45 m_channelCount = 0;
46 m_sampleRate = 0;
47
48 std::cerr << "OggVorbisFileReader::OggVorbisFileReader(" << path.toLocal8Bit().data() << "): now have " << (++instances) << " instances" << std::endl;
49
50 Profiler profiler("OggVorbisFileReader::OggVorbisFileReader", true);
51
52 QFileInfo info(path);
53 m_fileSize = info.size();
54
55 OGGZ *oggz;
56 if (!(oggz = oggz_open(path.toLocal8Bit().data(), OGGZ_READ))) {
57 m_error = QString("File %1 is not an OGG file.").arg(path);
58 return;
59 }
60
61 FishSoundInfo fsinfo;
62 m_fishSound = fish_sound_new(FISH_SOUND_DECODE, &fsinfo);
63
64 fish_sound_set_decoded_callback(m_fishSound, acceptFrames, this);
65 oggz_set_read_callback(oggz, -1, readPacket, this);
66
67 if (showProgress) {
68 m_progress = new QProgressDialog
69 (QObject::tr("Decoding Ogg file..."),
70 QObject::tr("Stop"), 0, 100);
71 m_progress->hide();
72 }
73
74 while (oggz_read(oggz, 1024) > 0);
75
76 fish_sound_delete(m_fishSound);
77 m_fishSound = 0;
78 oggz_close(oggz);
79
80 if (isDecodeCacheInitialised()) finishDecodeCache();
81
82 if (showProgress) {
83 delete m_progress;
84 m_progress = 0;
85 }
86 }
87
88 OggVorbisFileReader::~OggVorbisFileReader()
89 {
90 std::cerr << "OggVorbisFileReader::~OggVorbisFileReader(" << m_path.toLocal8Bit().data() << "): now have " << (--instances) << " instances" << std::endl;
91 }
92
93 int
94 OggVorbisFileReader::readPacket(OGGZ *, ogg_packet *packet, long, void *data)
95 {
96 OggVorbisFileReader *reader = (OggVorbisFileReader *)data;
97 FishSound *fs = reader->m_fishSound;
98
99 fish_sound_prepare_truncation(fs, packet->granulepos, packet->e_o_s);
100 fish_sound_decode(fs, packet->packet, packet->bytes);
101
102 reader->m_bytesRead += packet->bytes;
103
104 if (reader->m_fileSize > 0 && reader->m_progress) {
105 // The number of bytes read by this function is smaller than
106 // the file size because of the packet headers
107 int progress = lrint(double(reader->m_bytesRead) * 114 /
108 double(reader->m_fileSize));
109 if (progress > 99) progress = 99;
110 if (progress > reader->m_progress->value()) {
111 reader->m_progress->setValue(progress);
112 reader->m_progress->show();
113 reader->m_progress->raise();
114 qApp->processEvents();
115 if (reader->m_progress->wasCanceled()) {
116 reader->m_cancelled = true;
117 }
118 }
119 }
120
121 if (reader->m_cancelled) return 1;
122 return 0;
123 }
124
125 int
126 OggVorbisFileReader::acceptFrames(FishSound *fs, float **frames, long nframes,
127 void *data)
128 {
129 OggVorbisFileReader *reader = (OggVorbisFileReader *)data;
130
131 if (reader->m_channelCount == 0) {
132 FishSoundInfo fsinfo;
133 fish_sound_command(fs, FISH_SOUND_GET_INFO,
134 &fsinfo, sizeof(FishSoundInfo));
135 reader->m_channelCount = fsinfo.channels;
136 reader->m_sampleRate = fsinfo.samplerate;
137 reader->initialiseDecodeCache();
138 }
139
140 if (nframes > 0) {
141
142 reader->m_frameCount += nframes;
143
144 for (long i = 0; i < nframes; ++i) {
145 for (size_t c = 0; c < reader->m_channelCount; ++c) {
146 reader->addSampleToDecodeCache(frames[c][i]);
147 // reader->m_data.push_back(frames[c][i]);
148 }
149 }
150
151 MUNLOCK_SAMPLEBLOCK(reader->m_data);
152 }
153
154 if (reader->m_cancelled) return 1;
155 return 0;
156 }
157
158 #endif
159 #endif