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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
/*************************************************************************
VirtualAudioFile.cpp - adapter between QIODevice and libaudiofile
-------------------
begin : Mon May 06 2002
copyright : (C) 2002 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <QIODevice>
#include <stdlib.h> // for calloc()
#include <unistd.h>
#include <new>
#include "libkwave/Utils.h"
#include "libkwave/VirtualAudioFile.h"
/**
* map for finding the corresponding VirtualAudioFile
* adapter to a AFvirtualfile from libaudiofile
*/
static QMap<AFvirtualfile*, Kwave::VirtualAudioFile*> *_adapter_map = nullptr;
/** Last error number from libaudiofile. -1 if no error occurred */
static long _last_audiofile_error = -1;
/** Last error text from libaudiofile, empty if no error occurred */
static QString _last_audiofile_error_text;
//***************************************************************************
/**
* Error handler for libaudiofile
* @warning NOT THREADSAFE!
* @param error the numeric error code, >=0, defined in audiofile.h,
* something starting with AF_BAD_...
* @param str text, not localized, so not usable for us :-(
*/
static void _handle_audiofile_error(long int error, const char *str)
{
qDebug("libaudiofile error %ld: '%s'", error, str);
_last_audiofile_error = error;
_last_audiofile_error_text = QString::fromLatin1(str);
}
//***************************************************************************
/** Returns the last libaudiofile error and resets it to -1 */
static long _lastAudiofileError()
{
long err = _last_audiofile_error;
_last_audiofile_error = -1;
// ignore "bad alloc", which might occur on a "malloc(0)"
if (err == AF_BAD_MALLOC) err = -1;
return err;
}
//***************************************************************************
static ssize_t af_file_read(AFvirtualfile *vfile, void *data,
size_t nbytes)
{
Kwave::VirtualAudioFile *adapter = Kwave::VirtualAudioFile::adapter(vfile);
return (adapter) ?
static_cast<ssize_t>(adapter->read(
static_cast<char *>(data),
Kwave::toUint(nbytes)
)) : 0;
}
//***************************************************************************
static AFfileoffset af_file_length(AFvirtualfile *vfile)
{
Kwave::VirtualAudioFile *adapter = Kwave::VirtualAudioFile::adapter(vfile);
return (adapter) ? static_cast<AFfileoffset>(adapter->length()) : -1;
}
//***************************************************************************
static ssize_t af_file_write(AFvirtualfile *vfile, const void *data,
size_t nbytes)
{
Kwave::VirtualAudioFile *adapter = Kwave::VirtualAudioFile::adapter(vfile);
return (adapter) ?
static_cast<ssize_t>(adapter->write(
static_cast<const char *>(data),
Kwave::toUint(nbytes)
)) : 0;
}
//***************************************************************************
static void af_file_destroy(AFvirtualfile */*vfile*/)
{
}
//***************************************************************************
static AFfileoffset af_file_seek(AFvirtualfile *vfile, AFfileoffset offset,
int is_relative)
{
Kwave::VirtualAudioFile *adapter = Kwave::VirtualAudioFile::adapter(vfile);
return (adapter) ?
static_cast<AFfileoffset>(adapter->seek(
static_cast<qint64>(offset),
(is_relative != 0)
)) : -1;
}
//***************************************************************************
static AFfileoffset af_file_tell(AFvirtualfile *vfile)
{
Kwave::VirtualAudioFile *adapter = Kwave::VirtualAudioFile::adapter(vfile);
return (adapter) ? static_cast<AFfileoffset>(adapter->tell()) : -1;
}
//***************************************************************************
/**
* Replacement of af_virtual_file_new from original libaudiofile code.
* Unfortunately the original is not usable because it is not available
* through the shared library API of some libaudiofile versions.
*
* original version: see libaudiofile/af_vfs.c (GPL 2+)
* original author: Copyright (C) 1999, Elliot Lee <sopwith@redhat.com>
*/
static AFvirtualfile *__af_virtual_file_new(void)
{
return static_cast<AFvirtualfile *>(calloc(1, sizeof(AFvirtualfile)));
}
//***************************************************************************
//***************************************************************************
Kwave::VirtualAudioFile::VirtualAudioFile(QIODevice &device)
:m_device(device), m_file_handle(nullptr), m_virtual_file(nullptr),
m_last_error(-1), m_last_error_text()
{
// create the virtual file structure for libaudiofile
m_virtual_file = __af_virtual_file_new();
Q_ASSERT(m_virtual_file);
if (!m_virtual_file) return;
// enter our wrapper functions
m_virtual_file->closure = nullptr;
m_virtual_file->read = af_file_read;
m_virtual_file->write = af_file_write;
m_virtual_file->length = af_file_length;
m_virtual_file->destroy = af_file_destroy;
m_virtual_file->seek = af_file_seek;
m_virtual_file->tell = af_file_tell;
}
//***************************************************************************
void Kwave::VirtualAudioFile::open(Kwave::VirtualAudioFile *x,
AFfilesetup setup)
{
// register ourself
adapter(nullptr); // dummy lookup, for creating a new map if needed
Q_ASSERT(_adapter_map);
if (_adapter_map) _adapter_map->insert(m_virtual_file, x);
// determine the mode: rw/w/r
const char *mode = nullptr;
if (m_device.isWritable()) mode = "w";
else if (m_device.isReadable()) mode = "r";
Q_ASSERT(mode);
AFerrfunc old_handler;
old_handler = afSetErrorHandler(_handle_audiofile_error);
// reset the file position when opening the device, otherwise libaudiofile
// might fail when seeking to the current position and the position of
// the device currently is at EOF (in libaudiofile, File::canSeek)
m_device.seek(0);
// open the virtual file and get a handle for it
m_file_handle = afOpenVirtualFile(m_virtual_file, mode, setup);
m_last_error = _lastAudiofileError();
m_last_error_text = _last_audiofile_error_text;
afSetErrorHandler(old_handler);
}
//***************************************************************************
void Kwave::VirtualAudioFile::close()
{
// close libaudiofile stuff
afCloseFile(m_file_handle);
// de-register ourself
if (_adapter_map) _adapter_map->remove(m_virtual_file);
m_virtual_file = nullptr;
m_file_handle = nullptr;
}
//***************************************************************************
Kwave::VirtualAudioFile::~VirtualAudioFile()
{
if (m_virtual_file) close();
}
//***************************************************************************
qint64 Kwave::VirtualAudioFile::read(char *data, unsigned int nbytes)
{
Q_ASSERT(data);
if (!data) return 0;
return m_device.read(data, nbytes);
}
//***************************************************************************
qint64 Kwave::VirtualAudioFile::length()
{
return m_device.size();
}
//***************************************************************************
qint64 Kwave::VirtualAudioFile::write(const char *data, unsigned int nbytes)
{
Q_ASSERT(data);
if (!data) return 0;
return m_device.write(data, nbytes);
}
//***************************************************************************
void Kwave::VirtualAudioFile::destroy()
{
}
//***************************************************************************
qint64 Kwave::VirtualAudioFile::seek(qint64 offset, bool is_relative)
{
qint64 abs_pos = (is_relative) ? (m_device.pos() + offset) : offset;
if (abs_pos >= m_device.size())
return -1; // avoid seek after EOF
bool ok = m_device.seek(abs_pos);
return (ok) ? m_device.pos() : -1;
}
//***************************************************************************
qint64 Kwave::VirtualAudioFile::tell()
{
return m_device.pos();
}
//***************************************************************************
Kwave::VirtualAudioFile *Kwave::VirtualAudioFile::adapter(AFvirtualfile *vfile)
{
// create a new empty map if necessary
if (!_adapter_map) _adapter_map =
new(std::nothrow) QMap<AFvirtualfile*,VirtualAudioFile*>();
Q_ASSERT(_adapter_map);
if (!_adapter_map) return nullptr;
// lookup in the map
return _adapter_map->contains(vfile) ? (*_adapter_map)[vfile] : nullptr;
}
//***************************************************************************
//***************************************************************************
|