summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/phonon/qt7/mediaobjectaudionode.mm
blob: 39b0d4e43e01835d9c443d5a77a711e15de9bb8d (plain)
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
/*  This file is part of the KDE project.

    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).

    This library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2.1 or 3 of the License.

    This library 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <QTKit/QTMovie.h>

#include "mediaobjectaudionode.h"
#include "quicktimeaudioplayer.h"
#include "quicktimevideoplayer.h"
#include "audiomixer.h"

QT_BEGIN_NAMESPACE

namespace Phonon
{
namespace QT7
{

MediaObjectAudioNode::MediaObjectAudioNode(QuickTimeAudioPlayer *player1, QuickTimeAudioPlayer *player2) : AudioNode(0, 1)
{
    m_mute = false;
    m_player1 = player1;
    m_player2 = player2;
    m_mixer = new AudioMixerAudioNode();

    m_connection1 = new AudioConnection(m_player1, 0, m_mixer, 0);
    m_connection2 = new AudioConnection(m_player2, 0, m_mixer, 1);

    m_fadeDuration = 0;
}

MediaObjectAudioNode::~MediaObjectAudioNode()
{
    setGraph(0);
    delete m_player1;
    delete m_player2;
    delete m_mixer;
    delete m_connection1;
    delete m_connection2;
}

void MediaObjectAudioNode::createAndConnectAUNodes()
{
    DEBUG_AUDIO_GRAPH("(MediaObjectAudioNode" << int(this) << "createAndConnectAUNodes called)" )
    m_player1->createAndConnectAUNodes();
    m_player2->createAndConnectAUNodes();
    m_mixer->createAndConnectAUNodes();

    m_connection1->connect(m_audioGraph);
    m_connection2->connect(m_audioGraph);
}

void MediaObjectAudioNode::createAudioUnits()
{
    DEBUG_AUDIO_GRAPH("(MediaObjectAudioNode" << int(this) << "createAudioUnits called)" )
    m_player1->createAudioUnits();
    m_player2->createAudioUnits();
    m_mixer->createAudioUnits();
}

void MediaObjectAudioNode::setGraph(AudioGraph *audioGraph)
{
    DEBUG_AUDIO_GRAPH("MediaObjectAudioNode" << int(this) << "is setting graph:" << int(audioGraph))
    m_audioGraph = audioGraph;
    m_player1->setGraph(audioGraph);
    m_player2->setGraph(audioGraph);
    m_mixer->setGraph(audioGraph);
}

AUNode MediaObjectAudioNode::getOutputAUNode()
{
    return m_mixer->getOutputAUNode();
}

bool MediaObjectAudioNode::fillInStreamSpecification(AudioConnection *connection, ConnectionSide side)
{
    if (side == Source){
        DEBUG_AUDIO_STREAM("(MediaObjectAudioNode" << int(this) << "fillInStreamSpecification called, role = source)")
        return m_mixer->fillInStreamSpecification(connection, side);
    } else {
        DEBUG_AUDIO_STREAM("(MediaObjectAudioNode" << int(this) << "fillInStreamSpecification called, role = sink)")
        return (m_connection2->updateStreamSpecification() && m_connection1->updateStreamSpecification());
    }
}

bool MediaObjectAudioNode::setStreamSpecification(AudioConnection *connection, ConnectionSide side)
{
    if (side == Source){
        DEBUG_AUDIO_STREAM("(MediaObjectAudioNode" << int(this) << "setStreamSpecification called, role = source)")
        return m_mixer->setStreamSpecification(connection, side);
    }
    return true;
}

void MediaObjectAudioNode::setMute(bool mute)
{
    m_mute = mute;
    m_mixer->setVolume(m_mute ? 0 : m_volume1, m_connection1->m_sinkInputBus);
    m_mixer->setVolume(m_mute ? 0 : m_volume2, m_connection2->m_sinkInputBus);
}

void MediaObjectAudioNode::updateVolume()
{
    if (m_mute)
        return;
        
    QuickTimeVideoPlayer *player1 = static_cast<QuickTimeAudioPlayer *>(m_connection1->m_sourceAudioNode)->videoPlayer();
    QuickTimeVideoPlayer *player2 = static_cast<QuickTimeAudioPlayer *>(m_connection2->m_sourceAudioNode)->videoPlayer();
    if (player1)
        player1->setRelativeVolume(m_volume1);
    if (player2)
        player2->setRelativeVolume(m_volume2);

    m_mixer->setVolume(m_volume1, m_connection1->m_sinkInputBus);
    m_mixer->setVolume(m_volume2, m_connection2->m_sinkInputBus);
}

void MediaObjectAudioNode::startCrossFade(qint64 duration)
{
    m_fadeDuration = duration;

    // Swap:
    AudioConnection *tmp = m_connection1;
    m_connection1 = m_connection2;
    m_connection2 = tmp;

    // Init volume:
    if (m_fadeDuration > 0){
        m_volume1 = 0;
        m_volume2 = 1;
    } else {
        m_volume1 = 1;
        m_volume2 = 0;
    }
    updateVolume();
}

float MediaObjectAudioNode::applyCurve(float volume)
{
    float newValue = 0;
    if (volume > 0)
        newValue = float(0.5f * (2 + log10(volume)));
    return newValue;
}

void MediaObjectAudioNode::updateCrossFade(qint64 currentTime)
{
    // Assume that currentTime starts at 0 and progress.
    if (m_fadeDuration > 0){
        float volume = float(currentTime) / float(m_fadeDuration);
        if (volume >= 1){
            volume = 1;
            m_fadeDuration = 0;
        }
        m_volume1 = applyCurve(volume);
        m_volume2 = 1 - volume;
        updateVolume();
    }
}

bool MediaObjectAudioNode::isCrossFading()
{
    return (m_fadeDuration > 0);
}

void MediaObjectAudioNode::cancelCrossFade()
{
    m_fadeDuration = 0;
    m_volume1 = 1;
    m_volume2 = 0;
    updateVolume();
}

void MediaObjectAudioNode::mediaNodeEvent(const MediaNodeEvent *event)
{
    switch (event->type()){
    case MediaNodeEvent::AudioGraphAboutToBeDeleted:
        m_connection1->invalidate();
        m_connection2->invalidate();
        break;
    case MediaNodeEvent::AudioGraphCannotPlay:
    case MediaNodeEvent::AudioGraphInitialized:
        updateVolume();
        break;
    default:
        break;
    }

    m_player1->mediaNodeEvent(event);
    m_player2->mediaNodeEvent(event);
    m_mixer->mediaNodeEvent(event);
}

}} //namespace Phonon::QT7

QT_END_NAMESPACE