Download this file

pcmdecoder.cpp    114 lines (95 with data), 3.0 kB

  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
/* Copyright (C) 2015-2018 J.F.Dockes
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "pcmdecoder.h"
#include "rcvqueue.h"
#include "log.h"
#include <string.h>
#ifdef DEBUG_DECODER
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif
using namespace std;
PcmDecoder::PcmDecoder(bool needswap) : m_needswap(needswap)
{
LOGDEB("Create PCM decoder\n");
#ifdef DEBUG_DECODER
dumpfd = open("/tmp/sc2dump.pcm", O_WRONLY|O_CREAT|O_TRUNC, 0666);
#endif
}
PcmDecoder::~PcmDecoder()
{
LOGDEB("Delete PCM decoder" << endl << flush);
#ifdef DEBUG_DECODER
if (dumpfd > 0)
close(dumpfd);
#endif
}
void PcmDecoder::start()
{
LOGDEB("Start PCM decoder\n");
}
int PcmDecoder::decode(OhmMsgAudio& aMsg)
{
unsigned int bytes = 0;
unsigned int allocbytes = 0;
char *buf = NULL;
if (aMsg.Audio().Bytes() == 0) {
if (aMsg.Halt()) {
LOGDEB("PcmDecoder::decode: empty message with halt flag set\n");
goto put_audio_message;
} else {
LOGDEB("PcmDecoder::decode: ignoring empty message\n");
return 0;
}
} else if (aMsg.Halt()) {
LOGDEB("PcmDecoder::decode: halt flag set in message\n");
}
bytes = aMsg.Audio().Bytes();
// We allocate a bit more space to avoir reallocations in the resampler
allocbytes = bytes + 100;
buf = (char *)malloc(allocbytes);
if (buf == 0) {
LOGERR("PcmDecoder::decode: can't allocate " <<
allocbytes << " bytes\n");
return -1;
}
if (m_needswap) {
copyswap((unsigned char *)buf, aMsg.Audio().Ptr(), bytes, aMsg.BitDepth());
} else {
memcpy(buf, aMsg.Audio().Ptr(), bytes);
}
#ifdef DEBUG_DECODER
if (dumpfd > 0)
write(dumpfd, buf, bytes);
#endif
put_audio_message:
AudioMessage *ap = new
AudioMessage(aMsg.BitDepth(), aMsg.Channels(), aMsg.Samples(),
aMsg.SampleRate(), aMsg.Halt(), buf, allocbytes);
// There is nothing special we can do if put fails: no way to
// return status. Should we just exit ?
if (!audioqueue.put(ap, false)) {
LOGERR("sc2mpd: queue dead: exiting\n");
exit(1);
}
return bytes;
}
void PcmDecoder::finish()
{
LOGDEB("Finish PCM decoder\n");
}