File: outputmanager.cpp

package info (click to toggle)
olive-editor 20200620-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,228 kB
  • sloc: cpp: 51,932; sh: 56; makefile: 7; xml: 7
file content (153 lines) | stat: -rw-r--r-- 4,217 bytes parent folder | download
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
/***

  Olive - Non-Linear Video Editor
  Copyright (C) 2019 Olive Team

  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 3 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, see <http://www.gnu.org/licenses/>.

***/

#include "outputmanager.h"

#include <QDebug>
#include <QtMath>

#include <QFile>

OLIVE_NAMESPACE_ENTER

AudioOutputManager::AudioOutputManager(QObject *parent) :
  QObject(parent),
  output_(nullptr),
  push_device_(nullptr)
{
}

AudioOutputManager::~AudioOutputManager()
{
  Close();
}

void AudioOutputManager::Push(const QByteArray& samples)
{
  // This function is not queued and is intended to be called from the caller's thread
  QMutexLocker lock(&push_sample_lock_);

  // Replace sample buffer with this one
  push_samples_ = samples;
  push_sample_index_ = 0;

  // If we had another device connected, disconnect it now
  QMetaObject::invokeMethod(this, "ResetToPushMode", Qt::QueuedConnection);

  // Start pushing samples to the output
  QMetaObject::invokeMethod(this, "PushMoreSamples", Qt::QueuedConnection);
}

void AudioOutputManager::ResetToPushMode()
{
  // If we have a null push device, then we currently have the output in pull mode. We restore it to push mode here.
  if (output_ && !push_device_) {
    output_->stop();

    device_proxy_.close();

    // Put QAudioOutput back into push mode
    push_device_ = output_->start();
  }
}

void AudioOutputManager::SetParameters(AudioParams params)
{
  device_proxy_.SetParameters(params);
}

void AudioOutputManager::Close()
{
  if (output_) {
    output_->stop();

    if (device_proxy_.isOpen()) {
      device_proxy_.close();
    }

    output_->deleteLater();
    output_ = nullptr;
  }
}

void AudioOutputManager::PullFromDevice(const QString &filename, qint64 offset, int playback_speed)
{
  if (!output_) {
    return;
  }

  // Stop any current output and disable push mode
  output_->stop();
  push_device_ = nullptr;
  push_samples_.clear();

  // Pull from the device
  device_proxy_.SetDevice(filename, offset, playback_speed);
  device_proxy_.open(QIODevice::ReadOnly);
  output_->start(&device_proxy_);
}

void AudioOutputManager::PushMoreSamples()
{
  QMutexLocker lock(&push_sample_lock_);

  // Check if we're currently in push mode and if we have samples to push
  if (!push_device_ || push_samples_.isEmpty()) {
    return;
  }

  const char* read_ptr = push_samples_.constData() + push_sample_index_;

  // Push the bytes we have to the audio output
  qint64 write_count = push_device_->write(read_ptr,
                                           push_samples_.size() - push_sample_index_);

  // Increment sample buffer index (faster than shift the bytes up)
  push_sample_index_ += static_cast<int>(write_count);

  // If we've pushed all samples, we can clear this array
  if (push_sample_index_ == push_samples_.size()) {
    push_samples_.clear();
  }
}

void AudioOutputManager::SetOutputDevice(QAudioDeviceInfo info, QAudioFormat format)
{
  // Whatever the output is doing right now, stop it
  Close();

  // Create a new output device and start it in push mode
  output_ = new QAudioOutput(info, format, this);
  output_->setBufferSize(131072);
  output_->setNotifyInterval(1);
  push_device_ = output_->start();
  connect(output_, &QAudioOutput::notify, this, &AudioOutputManager::PushMoreSamples);
  connect(output_, &QAudioOutput::notify, this, &AudioOutputManager::OutputNotified);

  // Un-comment this to get debug information about what the audio output is doing
  //connect(output_, &QAudioOutput::stateChanged, this, &AudioOutputManager::OutputStateChanged);
}

void AudioOutputManager::OutputStateChanged(QAudio::State state)
{
  qDebug() << state << output_->error();
}

OLIVE_NAMESPACE_EXIT