Subversion Repositories tpanel

Rev

Rev 473 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
457 andreas 1
/*
2
 * Copyright (C) 2024 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#ifndef TQINTERCOM_H
20
#define TQINTERCOM_H
21
 
458 andreas 22
#include <string>
460 andreas 23
#include <QObject>
461 andreas 24
#include <QAudioFormat>
460 andreas 25
#include <QAbstractSocket>
468 andreas 26
#include <QAudioSink>
469 andreas 27
#include <QUdpSocket>
470 andreas 28
#include <QScopedPointer>
458 andreas 29
 
30
typedef struct INTERCOM_t
31
{
32
    std::string ip;         // IP address of panel to connect to
33
    long rxPort{0};          // Network port number for receiver
34
    long txPort{0};          // Network port number for transmitter
35
    int mode{0};            // The mode (0 = talk, 1 = listen, 2 = both)
36
}INTERCOM_t;
37
 
461 andreas 38
class QAudioSource;
465 andreas 39
class QBuffer;
469 andreas 40
class QTimer;
41
class QTimerEvent;
460 andreas 42
 
469 andreas 43
class TMicrophone : public QIODevice
467 andreas 44
{
45
    public:
470 andreas 46
        TMicrophone();
469 andreas 47
        ~TMicrophone();
467 andreas 48
 
470 andreas 49
        void start();
50
        void stop();
51
 
467 andreas 52
        qint64 readData(char *data, qint64 maxlen) override;
53
        qint64 writeData(const char *data, qint64 len) override;
54
        qint64 bytesAvailable() const override;
55
        qint64 size() const override { return mBuffer.size(); }
56
 
57
    protected:
58
        int8_t uLawEncode(int16_t number);
59
 
60
    private:
61
        qint64 mPos{0};
62
        QByteArray mBuffer;
63
};
64
 
460 andreas 65
class TQIntercom : public QObject
457 andreas 66
{
460 andreas 67
    Q_OBJECT
68
 
458 andreas 69
    public:
460 andreas 70
        TQIntercom(QObject *parent);
71
        TQIntercom(QObject *parent, INTERCOM_t ic);
458 andreas 72
        ~TQIntercom();
457 andreas 73
 
458 andreas 74
        void start();
75
        void stop();
461 andreas 76
        void setMicrophoneLevel(int level);
458 andreas 77
        void setSpeakerLevel(int level);
78
        void setMute(bool mute);
79
        void setIntercom(INTERCOM_t ic);
80
 
459 andreas 81
    protected:
460 andreas 82
        bool connectTalker();   // Talker
83
        bool spawnServer();     // Listener
459 andreas 84
 
465 andreas 85
        void onInputStateChanged(QAbstractSocket::SocketState socketState);
86
        void onOutputStateChanged(QAbstractSocket::SocketState socketState);
87
        void stateChangedMessage(QAbstractSocket::SocketState socketState, bool in);
88
        void onInputErrorOccurred(QAbstractSocket::SocketError socketError);
89
        void onOutputErrorOccurred(QAbstractSocket::SocketError socketError);
90
        void socketErrorMessages(QAbstractSocket::SocketError socketError, const std::string& msg);
91
        void onReadPendingDatagrams();
469 andreas 92
        void onRecordPermissionGranted();
471 andreas 93
        void onMicStateChanged(QAudio::State state);
460 andreas 94
 
466 andreas 95
        int16_t uLawDecode(int8_t number);
471 andreas 96
        int8_t uLawEncode(int16_t number);
470 andreas 97
        qreal convertVolume(int volume);
466 andreas 98
 
458 andreas 99
    private:
474 andreas 100
        typedef struct _HEADER_t
101
        {
102
            uint16_t ident{0x8000};
103
            uint16_t counter{0};
104
            uint32_t position{0};
105
            uint16_t unk1{0xf8a8};
106
            uint16_t unk2{0xe554};
107
        }_HEADER_t;
108
 
109
        long getNextBlock(QByteArray *target, const QByteArray& data);
110
 
458 andreas 111
        INTERCOM_t mIntercom;
470 andreas 112
        int mMicLevel{100};
113
        int mSpkLevel{100};
458 andreas 114
        bool mInitialized{false};
471 andreas 115
        bool mTalkConnected{false};
469 andreas 116
        bool mRecordPermissionGranted{false};
474 andreas 117
        bool mMicOpen{false};
118
        _HEADER_t mHeader;
459 andreas 119
 
460 andreas 120
        QUdpSocket *mUdpTalker{nullptr};
121
        QUdpSocket *mUdpListener{nullptr};
461 andreas 122
        QAudioFormat mAudioFormat;
123
        QAudioSource *mMicrophone{nullptr}; // Talk --> from mic to remote
124
        QAudioSink *mRemote{nullptr};       // Listen --> from remote to speaker
469 andreas 125
        QByteArray mReadBuffer;
126
        TMicrophone *mMicDevice{nullptr};
470 andreas 127
        QTimer *mPushTimer{nullptr};
473 andreas 128
        QTimer *mPullTimer{nullptr};
471 andreas 129
        QAudioDevice mAudioMicDevice;
457 andreas 130
};
131
 
132
#endif // TQINTERCOM_H