Initial commit
[camargo/neiasound.git] / src / qt / qtaudiostream.cpp
1 #include "qtaudiostream.h"
2
3 #include <QIODevice>
4 #include <QAudioDecoder>
5 #include <QCoreApplication>
6 #include <QThread>
7 #include <QtMath>
8
9 nQtAudioStream::nQtAudioStream(QIODevice * dev, QObject *parent) :
10     nSoundStream(parent),
11     _device(dev),
12     _totalFrames(0),
13     _channels(0),
14     _frequency(0),
15     _format(SF_16BIT_STEREO)
16 {
17     _decoder = 0;
18     _lastBufConsumedFrames = 0;
19
20     if(!dev->isOpen())
21         dev->open(QIODevice::ReadOnly);
22     _deviceBuf.buffer() = dev->readAll();
23     dev->close();
24
25     _decoder = new QAudioDecoder(this);
26     _decoder->setSourceDevice(&_deviceBuf);
27
28     _decoder->start();
29     QCoreApplication::processEvents();
30     while( !_decoder->state() == QAudioDecoder::StoppedState && !_decoder->bufferAvailable()) { QThread::msleep(1); }
31     _qtFormat = _decoder->audioFormat();
32     if(_qtFormat.sampleType() != QAudioFormat::UnSignedInt || _qtFormat.sampleSize() != 16)
33     {
34         int a = _qtFormat.sampleType(), b = _qtFormat.sampleSize();
35         QString codec = _qtFormat.codec();
36         _qtFormat.setSampleType(QAudioFormat::UnSignedInt);
37         _qtFormat.setSampleSize(16);
38         _decoder->stop();
39         _decoder->setAudioFormat(_qtFormat);
40         _decoder->start();
41     }
42
43     _qtFormat = _decoder->audioFormat();
44
45     _channels = _qtFormat.channelCount();
46     _frequency = _qtFormat.sampleRate();
47     _totalFrames = (_decoder->duration()) / _qtFormat.durationForFrames(1);
48
49     qDebug(QString("[nQtAudioStream] sample type ").arg(_qtFormat.sampleType()).toLocal8Bit());
50
51     switch (_channels) {
52     case 1:
53         _format = SF_16BIT_MONO;
54         break;
55     case 2:
56         _format = SF_16BIT_STEREO;
57         break;
58     default:
59         _format = SF_UNDEFINED;
60         break;
61     }
62
63
64 }
65
66 nQtAudioStream::~nQtAudioStream()
67 {
68     _decoder->stop();
69     delete _decoder;
70
71 }
72
73 quint64 nQtAudioStream::read(void *data, unsigned long frames)
74 {
75     quint64 readFrames = 0;
76
77     while (readFrames < frames)
78     {
79         bool finished = false;
80
81         if( (!_lastBuf.isValid()) || _lastBufConsumedFrames >= _lastBuf.frameCount() )
82         {
83             // get a new buffer
84
85             while(true)
86             {
87                 if(_decoder->bufferAvailable())
88                 {
89                     _lastBuf = _decoder->read();
90                     _lastBufConsumedFrames = 0;
91                     break;
92                 }
93                 else if(_decoder->state() == QAudioDecoder::StoppedState)
94                 {
95                     finished = true;
96                     break;
97                 }
98
99                 QThread::msleep(1);
100             }
101
102         }
103
104         if(!finished)
105         {
106             int framesToConsume = qMin(frames - readFrames, (quint64) (_lastBuf.frameCount() - _lastBufConsumedFrames) );
107             const char* src = ((const char*) _lastBuf.data()) + _qtFormat.bytesForFrames(_lastBufConsumedFrames);
108             char* dst = ((char*) data) + _qtFormat.bytesForFrames(_lastBufConsumedFrames);
109
110             _lastBufConsumedFrames += framesToConsume;
111             readFrames += framesToConsume;
112         }
113     }
114
115     return readFrames;
116 }
117
118 void nQtAudioStream::rewind()
119 {
120
121 }
122