Switch to unified view

a b/mpd2src/pcmencoder.cpp
1
/* Copyright (C) 2015-2018 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
18
#include "pcmencoder.h"
19
#include "audioutil.h"
20
#include "log.h"
21
22
#ifdef DEBUG_ENCODER
23
#include <sys/stat.h>
24
#include <fcntl.h>
25
#include <unistd.h>
26
#endif
27
28
using namespace std;
29
30
PcmEncoder::PcmEncoder(AudioReader* audioReader, OhmSenderDriver *ohmSender,
31
                       unsigned packetBytes)
32
    : AudioEncoder(audioReader, ohmSender, true, "PCM"),
33
      m_packetBytes(packetBytes)
34
{
35
    LOGDEB("New PCM encoder\n");
36
37
#ifdef DEBUG_ENCODER
38
    dumpfd = open("/tmp/mpd2dump.pcm", O_WRONLY|O_CREAT|O_TRUNC, 0666);
39
#endif
40
}
41
42
PcmEncoder::~PcmEncoder()
43
{
44
    LOGDEB("Delete PCM encoder\n");
45
46
#ifdef DEBUG_ENCODER
47
    if (dumpfd > 0)
48
        close(dumpfd);
49
#endif
50
}
51
52
void PcmEncoder::start()
53
{
54
    LOGDEB("Start PCM encoder\n");
55
}
56
57
int PcmEncoder::encode(const unsigned char *buffer, unsigned num_bytes, bool halt)
58
{
59
    if (num_bytes > 0) {
60
        unsigned missing_bytes = m_packetBytes - num_bytes;
61
        if (missing_bytes > 0) {
62
            LOGDEB("PcmEncoder::encode: inserting " << missing_bytes << " bytes\n");
63
            memset((void*)(buffer + num_bytes), 0, missing_bytes);
64
        }
65
66
        if (m_audioReader->needswap())
67
            swapSamples((unsigned char*)buffer, m_audioReader->bytesPerSample(),
68
                        m_packetBytes / m_audioReader->bytesPerSample());
69
    }
70
71
    m_ohmSender->SendAudio((const TByte*)buffer, (TUint)num_bytes, (TBool)halt);
72
73
#ifdef DEBUG_ENCODER
74
    if (dumpfd > 0)
75
        write(dumpfd, buffer, num_bytes);
76
#endif
77
78
    return num_bytes;
79
}
80
81
void PcmEncoder::finish()
82
{
83
    LOGDEB("Finish PCM encoder\n");
84
}