Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* This file is part of the KDE project
2 Copyright (C) 2006 Matthias Kretz <kretz@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), Nokia Corporation
10 (or its successors, if any) and the KDE Free Qt Foundation, which shall
11 act as a proxy defined in Section 6 of version 3 of the license.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library. If not, see <http://www.gnu.org/licenses/>.
20
21*/
22
23
24#include "pcmoutput.h"
25
26#include <kaboutdata.h>
27#include <kcmdlineargs.h>
28#include <kapplication.h>
29#include <QtCore/QByteRef>
30#include <QtCore/QDataStream>
31#include <QtGui/QSlider>
32#include <QtGui/QBoxLayout>
33#include <phonon/audiooutput.h>
34#include <phonon/audiopath.h>
35#include <phonon/bytestream.h>
36#include <QtCore/QTimer>
37#include <QtCore/QFile>
38#include <QtGui/QLabel>
39
40static const int SAMPLE_RATE = 48000;
41
42PcmPlayer::PcmPlayer(QWidget *parent)
43 : QWidget(parent),
44 m_frequency(440)
45{
46 Phonon::AudioOutput *m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
47 Phonon::AudioPath *m_path = new Phonon::AudioPath(this);
48 m_path->addOutput(m_output);
49 m_stream = new Phonon::ByteStream(this);
50 m_stream->addAudioPath(m_path);
51 m_stream->setStreamSeekable(false);
52 m_stream->setStreamSize(0x7FFFFFFF);
53
54 QTimer *m_timer = new QTimer(this);
55 m_timer->setInterval(0);
56 connect(m_timer, SIGNAL(timeout()), SLOT(sendData()));
57
58 connect(m_stream, SIGNAL(needData()), m_timer, SLOT(start()));
59 connect(m_stream, SIGNAL(enoughData()), m_timer, SLOT(stop()));
60
61 m_stream->writeData(wavHeader());
62
63 setLayout(new QHBoxLayout);
64 QSlider *slider = new QSlider(this);
65 layout()->addWidget(slider);
66 slider->setRange(200, 4000);
67 slider->setValue(m_frequency);
68 connect(slider, SIGNAL(valueChanged(int)), SLOT(setFrequency(int)));
69
70 QTimer::singleShot(0, m_timer, SLOT(start()));
71 QTimer::singleShot(0, m_stream, SLOT(play()));
72
73 m_fLabel = new QLabel(this);
74 layout()->addWidget(m_fLabel);
75 m_fLabel->setText(QString::number(m_frequency));
76}
77
78void PcmPlayer::setFrequency(int f)
79{
80 m_frequency = f;
81 m_fLabel->setText(QString::number(m_frequency));
82}
83
84void PcmPlayer::sendData()
85{
86 m_stream->writeData(pcmBlock(m_frequency));
87}
88
89QByteArray PcmPlayer::wavHeader() const
90{
91 QByteArray data;
92 QDataStream stream(&data, QIODevice::WriteOnly);
93 stream.setByteOrder(QDataStream::LittleEndian);
94 stream
95 << 0x46464952 //"RIFF"
96 << static_cast<quint32>(0x7FFFFFFF)
97 << 0x45564157 //"WAVE"
98 << 0x20746D66 //"fmt " //Subchunk1ID
99 << static_cast<quint32>(16) //Subchunk1Size
100 << static_cast<quint16>(1) //AudioFormat
101 << static_cast<quint16>(2) //NumChannels
102 << static_cast<quint32>(SAMPLE_RATE) //SampleRate
103 << static_cast<quint32>(2 *2 *SAMPLE_RATE)//ByteRate
104 << static_cast<quint16>(2 *2) //BlockAlign
105 << static_cast<quint16>(16) //BitsPerSample
106 << 0x61746164 //"data" //Subchunk2ID
107 << static_cast<quint32>(0x7FFFFFFF-36)//Subchunk2Size
108 ;
109 return data;
110}
111
112QByteArray PcmPlayer::pcmBlock(int freq) const
113{
114 QByteArray data;
115 QDataStream stream(&data, QIODevice::WriteOnly);
116 stream.setByteOrder(QDataStream::LittleEndian);
117
118 const int samplesPerHalfWave = SAMPLE_RATE / (2 *freq);
119 const int sampleDiff = (1 << 16) / samplesPerHalfWave;
120 const int samplesPerQuarterWave = samplesPerHalfWave / 2;
121
122 for (int i = 0; i < samplesPerQuarterWave; ++i)
123 stream << static_cast<quint16>(i * sampleDiff)
124 << static_cast<quint16>(-i * sampleDiff);
125 for (int i = 0; i < samplesPerHalfWave; ++i)
126 stream << static_cast<quint16>(32767 - i * sampleDiff)
127 << static_cast<quint16>(-32768 + i * sampleDiff);
128 for (int i = 0; i < samplesPerQuarterWave; ++i)
129 stream << static_cast<quint16>(-32768 + i * sampleDiff)
130 << static_cast<quint16>(32767 - i * sampleDiff);
131
132 return data;
133}
134
135int main(int argc, char ** argv)
136{
137 KAboutData about("videoplayandforget", 0, ki18n("Phonon VideoPlayer Example"),
138 "1.0", KLocalizedString(),
139 KAboutData::License_LGPL);
140 about.addAuthor(ki18n("Matthias Kretz"), KLocalizedString(), "kretz@kde.org");
141 KCmdLineArgs::init(argc, argv, &about);
142 KApplication app;
143 PcmPlayer player;
144 player.show();
145 app.exec();
146}
147
148#include "pcmoutput.moc"
149

Warning: That file was not part of the compilation database. It may have many parsing errors.