|
| 1 | +/* Copyright 2016 Artem Sharganov and Iakov Kirilenko |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. */ |
| 14 | + |
| 15 | +#include "audioSynthDevices.h" |
| 16 | + |
| 17 | +#include <QtMultimedia/QAudioDeviceInfo> |
| 18 | + |
| 19 | +AudioSynthDevice::AudioSynthDevice(QObject *parent, int sampleRate, int sampleSize) |
| 20 | + : QIODevice(parent) |
| 21 | + , mBuffer(0) |
| 22 | + , mPos(0) |
| 23 | + , mSampleRate(sampleRate) |
| 24 | + , mSampleSize(sampleSize) |
| 25 | +{ |
| 26 | + |
| 27 | +} |
| 28 | + |
| 29 | +AudioSynthDevice::~AudioSynthDevice() |
| 30 | +{ |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | +void AudioSynthDevice::start(int hzFreq) |
| 35 | +{ |
| 36 | + open(QIODevice::ReadOnly); |
| 37 | + if(mBuffered) { |
| 38 | + qint64 length = (mSampleRate * (mSampleSize / 8)); |
| 39 | + mBuffer.resize(length); |
| 40 | + generate(mBuffer.data(), length, hzFreq); |
| 41 | + } |
| 42 | + else mHzFreq = hzFreq; |
| 43 | + |
| 44 | +} |
| 45 | + |
| 46 | +void AudioSynthDevice::stop() |
| 47 | +{ |
| 48 | + newCall = true; |
| 49 | + mPos = 0; |
| 50 | + close(); |
| 51 | +} |
| 52 | + |
| 53 | +// Modefied coupled first-order form algorithm with fixed point arithmetic |
| 54 | +int AudioSynthDevice::generate(char *data, int length, int hzFreq) |
| 55 | +{ |
| 56 | + const int channelBytes = mSampleSize / 8; |
| 57 | + |
| 58 | + qint64 maxlen = length/channelBytes; |
| 59 | + |
| 60 | + static const int M = 1 << 30; |
| 61 | + const auto w = hzFreq * M_PI / mSampleRate; |
| 62 | + const long long b1 = 2.0 * cos(w)*M; |
| 63 | + static const int AMPLITUDE = (1 << (mSampleSize - 1)) - 1; |
| 64 | + |
| 65 | + unsigned char *ptr = reinterpret_cast<unsigned char *>(data); |
| 66 | + |
| 67 | + |
| 68 | + // Need to save values between readData(...) calls, so static |
| 69 | + static long long y0 = 0; |
| 70 | + static decltype(y0) y1 = 0; |
| 71 | + static decltype(y0) y2 = 0; |
| 72 | + |
| 73 | + if(newCall) |
| 74 | + { |
| 75 | + y1 = M * std::sin(-w); |
| 76 | + y2 = M * std::sin(-2*w); |
| 77 | + newCall = false; |
| 78 | + } |
| 79 | + |
| 80 | + int i = 0; |
| 81 | + |
| 82 | + for(i = 0; i < maxlen; ++i){ |
| 83 | + |
| 84 | + y0 = b1*y1 / M - y2; |
| 85 | + y2 = b1*y0 / M - y1; |
| 86 | + y1 = b1*y2 / M - y0; |
| 87 | + |
| 88 | + if(mSampleSize == 8) { |
| 89 | + const qint8 val = static_cast<qint8>(y0*AMPLITUDE/M); |
| 90 | + *reinterpret_cast<quint8*>(ptr) = val; |
| 91 | + } |
| 92 | + if(mSampleSize == 16) { |
| 93 | + const qint16 val = static_cast<qint16>(y0*AMPLITUDE/M); |
| 94 | + *reinterpret_cast<quint16*>(ptr) = val; |
| 95 | + } |
| 96 | + |
| 97 | + ptr+=channelBytes; |
| 98 | + } |
| 99 | + |
| 100 | + return i*channelBytes; |
| 101 | +} |
| 102 | + |
| 103 | +qint64 AudioSynthDevice::readData(char *data, qint64 len) |
| 104 | +{ |
| 105 | + if(mBuffered) { |
| 106 | + qint64 total = 0; |
| 107 | + while (len - total > 0) { |
| 108 | + const qint64 chunk = qMin((mBuffer.size() - mPos), len - total); |
| 109 | + memcpy(data + total, mBuffer.constData() + mPos, chunk); |
| 110 | + mPos = (mPos + chunk) % mBuffer.size(); |
| 111 | + total += chunk; |
| 112 | + } |
| 113 | + return total; |
| 114 | + } else |
| 115 | + return generate(data, len, mHzFreq); |
| 116 | +} |
| 117 | + |
| 118 | +qint64 AudioSynthDevice::writeData(const char *data, qint64 len) |
| 119 | +{ |
| 120 | + Q_UNUSED(data); |
| 121 | + Q_UNUSED(len); |
| 122 | + |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +qint64 AudioSynthDevice::bytesAvailable() const |
| 127 | +{ |
| 128 | + return mBuffer.size() + QIODevice::bytesAvailable(); |
| 129 | +} |
0 commit comments