UBAudioQueueRecorder.cpp 10.9 KB
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
/*
 * Copyright (C) 2015-2016 Département de l'Instruction Publique (DIP-SEM)
 *
 * Copyright (C) 2013 Open Education Foundation
 *
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
 *
 * This file is part of OpenBoard.
 *
 * OpenBoard 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, version 3 of the License,
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
 * OpenBoard 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 OpenBoard. If not, see <http://www.gnu.org/licenses/>.
 */




#include "UBAudioQueueRecorder.h"

#include "core/memcheck.h"

AudioStreamBasicDescription UBAudioQueueRecorder::sAudioFormat;

UBAudioQueueRecorder::UBAudioQueueRecorder(QObject* pParent)
    : QObject(pParent)
    , mQueue(0)
    , mIsRecording(false)
    , mBufferLengthInMs(500)
{

    int sampleSize = sizeof(float); // TODO: check if this is system/ microphone-dependant
    
    sAudioFormat.mSampleRate = 44100.0;
    sAudioFormat.mFormatID = kAudioFormatLinearPCM;
    sAudioFormat.mChannelsPerFrame = 1;

    sAudioFormat.mBytesPerFrame = sampleSize;
    sAudioFormat.mBitsPerChannel = 8 * sampleSize;
    sAudioFormat.mBytesPerPacket = sampleSize;
    sAudioFormat.mFramesPerPacket = 1;
    sAudioFormat.mFormatFlags = kAudioFormatFlagIsFloat | 
                                kAudioFormatFlagsNativeEndian | 
                                kAudioFormatFlagIsPacked;
    
    
}


UBAudioQueueRecorder::~UBAudioQueueRecorder()
{
    // NOOP
}


QStringList UBAudioQueueRecorder::waveInDevices()
{
    QStringList deviceNames;

    QList<AudioDeviceID> devices = inputDeviceIDs();

    for (int i = 0; i < devices.length(); i++)
    {
        QString name = deviceNameFromDeviceID(devices[i]);
        if (name.length() > 0)
                deviceNames << name;
    }

    return deviceNames;
}


QList<AudioDeviceID> UBAudioQueueRecorder::inputDeviceIDs()
{
    QList<AudioDeviceID> inputDeviceIDs;
    UInt32 deviceIDsArraySize(0);

    AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &deviceIDsArraySize, 0);

    AudioDeviceID deviceIDs[deviceIDsArraySize / sizeof(AudioDeviceID)];

    AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &deviceIDsArraySize, deviceIDs);

    int deviceIDsCount = deviceIDsArraySize / sizeof(AudioDeviceID);

    for (int i = 0; i < deviceIDsCount; i ++)
    {
        AudioStreamBasicDescription sf;
        UInt32 size = sizeof(AudioStreamBasicDescription);

        if (noErr == AudioDeviceGetProperty(deviceIDs[i], 0, true, kAudioDevicePropertyStreamFormat,  &size, &sf))
        {
                inputDeviceIDs << deviceIDs[i];
        }
    }

    /*
    foreach(AudioDeviceID id, inputDeviceIDs)
    {
            qDebug() << "Device" << id <<  deviceNameFromDeviceID(id) << deviceUIDFromDeviceID(id);
    }
    */

    return inputDeviceIDs;
}


AudioDeviceID UBAudioQueueRecorder::defaultInputDeviceID()
{
    AudioDeviceID deviceID;
    UInt32 size = sizeof(deviceID);

    if (noErr == AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &size, &deviceID))
    {
        return deviceID;
    }
    else
    {
        return 0;
    }
}


AudioDeviceID UBAudioQueueRecorder::deviceIDFromDeviceName(const QString& name)
{
    QList<AudioDeviceID> devices = inputDeviceIDs();

    for (int i = 0; i < devices.length(); i++)
    {
        QString deviceName = deviceNameFromDeviceID(devices[i]);
        if (deviceName == name)
                return devices[i];
    }

    if (devices.length() > 0)
        return devices[0];
    else
        return 0;
}


QString UBAudioQueueRecorder::deviceUIDFromDeviceID(AudioDeviceID id)
{
    CFStringRef name;
    UInt32 size = sizeof(CFStringRef);
    QString uid;

    if (noErr == AudioDeviceGetProperty(id, 0, true, kAudioDevicePropertyDeviceUID, &size, &name))
    {
        char *cname = new char[1024];

        CFStringGetCString (name, cname, 1024, kCFStringEncodingISOLatin1);
        int length = CFStringGetLength (name);

        uid = QString::fromLatin1(cname, length);

        delete cname;

    }

    CFRelease(name);

    return uid;
}


QString UBAudioQueueRecorder::deviceNameFromDeviceID(AudioDeviceID id)
{
    CFStringRef name;
    UInt32 size = sizeof(CFStringRef);
    QString deviceName;

    if (noErr == AudioDeviceGetProperty(id, 0, true, kAudioObjectPropertyName, &size, &name))
    {
        char cname[1024];
        memset(cname,0,1024);
        CFStringGetCString (name, cname, 1024, kCFStringEncodingUTF8);
        deviceName = QString::fromUtf8(cname);
    }

    CFRelease(name);

    return deviceName;

}


bool UBAudioQueueRecorder::init(const QString& waveInDeviceName)
{
    if(mIsRecording)
    {
        setLastErrorMessage("Already recording ...");
        return false;
    }

    OSStatus err = AudioQueueNewInput (&sAudioFormat, UBAudioQueueRecorder::audioQueueInputCallback,
                    this, 0, 0, 0, &mQueue);

    if (err)
    {
        setLastErrorMessage(QString("Cannot acquire audio input %1").arg(err));
        mQueue = 0;
        close();
        return false;
    }

    //qDebug() << "init with waveInDeviceName ..." << waveInDeviceName;

    if (waveInDeviceName.length() > 0 && waveInDeviceName != "Default")
    {
        AudioDeviceID deviceID = deviceIDFromDeviceName(waveInDeviceName);

        if (deviceID)
        {
            QString deviceUID = deviceUIDFromDeviceID(deviceID);
            if (deviceUID.length() > 0)
            {
                CFStringRef sr = CFStringCreateWithCString(0, deviceUID.toUtf8().constData(), kCFStringEncodingUTF8);

                err = AudioQueueSetProperty(mQueue, kAudioQueueProperty_CurrentDevice, &sr, sizeof(CFStringRef));
                if (err)
                {
                    setLastErrorMessage(QString("Cannot set audio input %1 (%2)").arg(waveInDeviceName).arg(err));
                }
                else
                {
                    qDebug() << "recording with input" << waveInDeviceName;
                }
            }
            else
            {
                setLastErrorMessage(QString("Cannot find audio input device UID with ID %1 (%2)").arg(deviceID).arg(err));
            }
        }
        else
        {
            setLastErrorMessage(QString("Cannot find audio input with name %1 (%2)").arg(waveInDeviceName).arg(err));
        }
    }

    UInt32 monitor = true;

    err = AudioQueueSetProperty(mQueue, kAudioQueueProperty_EnableLevelMetering , &monitor, sizeof(UInt32));
    if (err)
    {
        qWarning() << QString("Cannot set recording level monitoring %1").arg(err);
    }

    int nbBuffers = 6;
    mSampleBufferSize = sAudioFormat.mSampleRate *  sAudioFormat.mChannelsPerFrame
                * sAudioFormat.mBitsPerChannel / 8 * mBufferLengthInMs / 1000; 
        // BufferSize [bytes] = Length [s] * 44100 frames per second [Fr./s] * channels per frame [Ch./Fr.] * bytes per channel [bytes/Ch.]

    for (int i = 0; i < nbBuffers; i++)
    {
        AudioQueueBufferRef outBuffer;
        err = AudioQueueAllocateBuffer(mQueue, mSampleBufferSize, &outBuffer);

        if (err)
        {
            setLastErrorMessage(QString("Cannot allocate audio buffer %1").arg(err));
            close();
            return false;
        }

        mBuffers << outBuffer;
    }

    foreach(AudioQueueBufferRef buffer, mBuffers)
    {
        err = AudioQueueEnqueueBuffer(mQueue, buffer, 0, 0);
        if (err)
        {
            setLastErrorMessage(QString("Cannot enqueue audio buffer %1").arg(err));
            close();
            return false;
        }
    }

    mMsTimeStamp = 0;

    err = AudioQueueStart(mQueue, 0);

    if (err)
    {
        setLastErrorMessage(QString("Cannot starting audio queue %1").arg(err));
        close();
        return false;
    }

    mIsRecording = true;

    return true;

}



bool UBAudioQueueRecorder::close()
{
    if(mIsRecording)
    {
        mIsRecording = false;

        OSStatus err = AudioQueueStop(mQueue, true);

        if(err)
        {
            setLastErrorMessage(QString("Cannot stop audio queue %1").arg(err));
        }
    }

    foreach(AudioQueueBufferRef buffer, mBuffers)
    {
        OSStatus err = AudioQueueFreeBuffer(mQueue, buffer);
        if (err)
        {
            setLastErrorMessage(QString("Cannot free audio buffer %1").arg(err));
        }
    }

    OSStatus err = AudioQueueDispose(mQueue, true);
    mQueue = 0;

    if(err)
    {
        setLastErrorMessage(QString("Cannot dispose audio queue %1").arg(err));
        return false;
    }

    return true;
}


void UBAudioQueueRecorder::audioQueueInputCallback (void *inUserData, 
                                                    AudioQueueRef inAQ,
                                                    AudioQueueBufferRef inBuffer, 
                                                    const AudioTimeStamp *inStartTime,
                                                    UInt32 inNumberPacketDescriptions, 
                                                    const AudioStreamPacketDescription *inPacketDescs)
{
    Q_UNUSED(inAQ);
    Q_UNUSED(inStartTime)
    UBAudioQueueRecorder* recorder = (UBAudioQueueRecorder*)inUserData;

    if(recorder && recorder->isRecording() && inBuffer)
    {
        recorder->emitNewWaveBuffer(inBuffer, inNumberPacketDescriptions, inPacketDescs);

        OSStatus err = AudioQueueEnqueueBuffer(recorder->mQueue, inBuffer, 0, 0);

        if(err)
        {
            recorder->setLastErrorMessage(QString("Cannot reenqueue buffer %1").arg(err));
        }
    }
}


void UBAudioQueueRecorder::emitNewWaveBuffer(AudioQueueBufferRef pBuffer,
                                             int inNumberPacketDescriptions, 
                                             const AudioStreamPacketDescription *inPacketDescs)
{

    emit newWaveBuffer(pBuffer->mAudioData, pBuffer->mAudioDataByteSize);

    qreal level = 0;
    UInt32 size;

    if (noErr == AudioQueueGetPropertySize (mQueue, kAudioQueueProperty_CurrentLevelMeter, &size))
    {
        AudioQueueLevelMeterState levels[size / sizeof(AudioQueueLevelMeterState)];
        size = sizeof(levels);

        if (noErr == AudioQueueGetProperty(mQueue, kAudioQueueProperty_CurrentLevelMeter, &levels, &size))
        {
            if (size == 1)
                level = levels[0].mAveragePower;
            else if (size >= 2)
                level = (levels[0].mAveragePower + levels[1].mAveragePower) / 2;
        }
        else
        {
            qDebug() << "cannot retreive audio level";
        }
    }

    emit audioLevelChanged(level * 255);
}