Skip to content

Commit

Permalink
recording logic test v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pcgod committed Aug 7, 2010
1 parent cf2753a commit be8f819
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/mumble/AudioOutput.cpp
Expand Up @@ -36,6 +36,8 @@
#include "Message.h"
#include "Plugins.h"
#include "PacketDataStream.h"
#include "ServerHandler.h"
#include "VoiceRecorder.h"

// Remember that we cannot use static member classes that are not pointers, as the constructor
// for AudioOutputRegistrar() might be called before they are initialized, as the constructor
Expand Down Expand Up @@ -967,6 +969,7 @@ bool AudioOutput::mix(void *outbuff, unsigned int nsamp) {
const float adjustFactor = std::pow(10, -18. / 20);
const float mul = g.s.fVolume;
const unsigned int nchan = iChannels;
boost::shared_ptr<VoiceRecorder> recorder(g.sh->recorder);

qrwlOutputs.lockForRead();
bool needAdjustment = false;
Expand Down Expand Up @@ -994,6 +997,11 @@ bool AudioOutput::mix(void *outbuff, unsigned int nsamp) {

memset(output, 0, sizeof(float) * nsamp * iChannels);

boost::shared_array<float> recbuff;
if (recorder) {
recbuff = boost::shared_array<float>(new float[nsamp]);
}

for (unsigned int i=0;i<iChannels;++i)
svol[i] = mul * fSpeakerVolume[i];

Expand Down Expand Up @@ -1077,6 +1085,21 @@ bool AudioOutput::mix(void *outbuff, unsigned int nsamp) {
}
}

if (recorder) {
AudioOutputSpeech *aos = qobject_cast<AudioOutputSpeech *>(aop);

if (aos) {
for (unsigned int i = 0; i < nsamp; ++i) {
recbuff[i] = pfBuffer[i] * volumeAdjustment;
}

if (!recorder->getMixDown()) {
recorder->addBuffer(aos->p, recbuff, nsamp);
recbuff = boost::shared_array<float>(new float[nsamp]);
}
}
}

if (validListener && ((aop->fPos[0] != 0.0f) || (aop->fPos[1] != 0.0f) || (aop->fPos[2] != 0.0f))) {
float dir[3] = { aop->fPos[0] - g.p->fCameraPosition[0], aop->fPos[1] - g.p->fCameraPosition[1], aop->fPos[2] - g.p->fCameraPosition[2] };
float len = sqrtf(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
Expand Down Expand Up @@ -1118,13 +1141,19 @@ bool AudioOutput::mix(void *outbuff, unsigned int nsamp) {
}
}

if (recorder && recorder->getMixDown()) {
recorder->addBuffer(NULL, recbuff, nsamp);
}

// Clip
if (eSampleFormat == SampleFloat)
for (unsigned int i=0;i<nsamp*iChannels;i++)
output[i] = output[i] < -1.0f ? -1.0f : (output[i] > 1.0f ? 1.0f : output[i]);
else
for (unsigned int i=0;i<nsamp*iChannels;i++)
reinterpret_cast<short *>(outbuff)[i] = static_cast<short>(32768.f * (output[i] < -1.0f ? -1.0f : (output[i] > 1.0f ? 1.0f : output[i])));
} else if (recorder) {
recorder->addSilence(nsamp);
}

qrwlOutputs.unlock();
Expand Down
5 changes: 5 additions & 0 deletions src/mumble/ServerHandler.cpp
Expand Up @@ -42,6 +42,7 @@
#include "NetworkConfig.h"
#include "OSInfo.h"
#include "SSL.h"
#include "VoiceRecorder.h"

ServerHandlerMessageEvent::ServerHandlerMessageEvent(const QByteArray &msg, unsigned int mtype, bool flush) : QEvent(static_cast<QEvent::Type>(SERVERSEND_EVENT)) {
qbaMsg = msg;
Expand Down Expand Up @@ -85,6 +86,10 @@ ServerHandler::ServerHandler() {
cConnection.reset();
qusUdp = NULL;
bStrong = false;
// test code
recorder = boost::shared_ptr<VoiceRecorder>(new VoiceRecorder(this));
recorder->setSampleRate(48000);
recorder->start();

// For some strange reason, on Win32, we have to call supportsSsl before the cipher list is ready.
qWarning("OpenSSL Support: %d (%s)", QSslSocket::supportsSsl(), SSLeay_version(SSLEAY_VERSION));
Expand Down
2 changes: 2 additions & 0 deletions src/mumble/ServerHandler.h
Expand Up @@ -40,6 +40,7 @@

class Connection;
class Message;
class VoiceRecorder;

class ServerHandlerMessageEvent : public QEvent {
public:
Expand Down Expand Up @@ -81,6 +82,7 @@ class ServerHandler : public QThread {
QSslCipher qscCipher;
ConnectionPtr cConnection;
QByteArray qbaDigest;
boost::shared_ptr<VoiceRecorder> recorder;

unsigned int uiVersion;
QString qsRelease;
Expand Down
163 changes: 163 additions & 0 deletions src/mumble/VoiceRecorder.cpp
@@ -0,0 +1,163 @@
/* Copyright (C) 2005-2010, Thorvald Natvig <thorvald@natvig.com>
Copyright (C) 2010, Benjamin Jemlich <pcgod@users.sourceforge.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the Mumble Developers nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "VoiceRecorder.h"

#include "ClientUser.h"

VoiceRecorder::VoiceRecorder(QObject *p) : QThread(p), iSampleRate(0),
bRecording(false), bMixDown(false), uiRecordedSamples(0) {
}

VoiceRecorder::~VoiceRecorder() {
stop();
wait();
clearLists();
}

void VoiceRecorder::clearLists() {
while (!qhRecordBuffer.isEmpty()) {
delete qhRecordBuffer.takeFirst();
}

QHash<int, RecordInfo *>::iterator it = qhRecordInfo.begin();
while (it != qhRecordInfo.end()) {
RecordInfo *ri = it.value();
if (ri->sf) {
sf_close(ri->sf);
}
delete ri;
it = qhRecordInfo.erase(it);
}
}

void VoiceRecorder::run() {
Q_ASSERT(iSampleRate != 0);

if (iSampleRate == 0)
return;

// SF_INFO sfinfo = {0, sampleRate, 1, SF_FORMAT_FLAC | SF_FORMAT_PCM_24, 0, 0};
SF_INFO sfinfo = {0, iSampleRate, 1, SF_ENDIAN_CPU | SF_FORMAT_AU | SF_FORMAT_FLOAT, 0, 0};
Q_ASSERT(sf_format_check(&sfinfo));

bRecording = true;
forever {
qmSleepLock.lock();
qwcSleep.wait(&qmSleepLock);

if (!bRecording) {
qmSleepLock.unlock();
break;
}

while (!qhRecordBuffer.isEmpty()) {
RecordBuffer *rb;
{
QMutexLocker l(&qmBufferLock);
rb = qhRecordBuffer.takeFirst();
}

int index = bMixDown ? 0 : rb->cuUser->uiSession;
Q_ASSERT(qhRecordInfo.contains(index));

RecordInfo *ri = qhRecordInfo.value(index);
if (!ri->sf) {
QString fileName = QString::fromLatin1("test_%1.au").arg(index);
ri->sf = sf_open(qPrintable(fileName), SFM_WRITE, &sfinfo);
//sf_command(ri->sf, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE);
if (rb->cuUser)
sf_set_string(ri->sf, SF_STR_TITLE, qPrintable(rb->cuUser->qsName));
}

if (ri->uiLastPosition != uiRecordedSamples) {
// write silence until we reach our current sample value
float *buffer = new float[1024];
memset(buffer, 0, sizeof(float) * 1024);
int rest = (uiRecordedSamples - ri->uiLastPosition) % 1024;
quint64 steps = (uiRecordedSamples - ri->uiLastPosition) / 1024;
for (quint64 i = 0; i < steps; ++i) {
sf_write_float(ri->sf, buffer, 1024);
}
if (rest > 0)
sf_write_float(ri->sf, buffer, rest);
delete [] buffer;
}

sf_write_float(ri->sf, rb->fBuffer.get(), rb->iSamples);
uiRecordedSamples += rb->iSamples;
ri->uiLastPosition = uiRecordedSamples;
delete rb;
}

qmSleepLock.unlock();
}
}

void VoiceRecorder::stop() {
bRecording = false;
qwcSleep.wakeAll();
}

void VoiceRecorder::addBuffer(ClientUser *cu, boost::shared_array<float> buffer, int samples) {
Q_ASSERT(!bMixDown || cu == NULL);

{
QMutexLocker l(&qmBufferLock);
qhRecordBuffer << new RecordBuffer(cu, buffer, samples);
}
int index = bMixDown ? 0 : cu->uiSession;
if (!qhRecordInfo.contains(index)) {
qhRecordInfo.insert(index, new RecordInfo());
}
qwcSleep.wakeAll();
}

void VoiceRecorder::addSilence(int samples) {
// FIXME: locking?
uiRecordedSamples += samples;
}

void VoiceRecorder::setSampleRate(int sampleRate) {
Q_ASSERT(!bRecording);

iSampleRate = sampleRate;
}

void VoiceRecorder::setMixDown(bool mixDown) {
Q_ASSERT(!bRecording);

bMixDown = mixDown;
}

bool VoiceRecorder::getMixDown() {
return bMixDown;
}
82 changes: 82 additions & 0 deletions src/mumble/VoiceRecorder.h
@@ -0,0 +1,82 @@
/* Copyright (C) 2005-2010, Thorvald Natvig <thorvald@natvig.com>
Copyright (C) 2010, Benjamin Jemlich <pcgod@users.sourceforge.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the Mumble Developers nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _VOICERECORDER_H
#define _VOICERECORDER_H

class ClientUser;

class VoiceRecorder : public QThread {
private:
class RecordBuffer {
public:
ClientUser *cuUser;
boost::shared_array<float> fBuffer;
int iSamples;

RecordBuffer(ClientUser *cu, boost::shared_array<float> buffer, int samples) : cuUser(cu), fBuffer(buffer), iSamples(samples) {}
};

class RecordInfo {
public:
SNDFILE *sf;
quint64 uiLastPosition;

RecordInfo() : sf(NULL), uiLastPosition(0) {}
};

QHash<int, RecordInfo *> qhRecordInfo;
QList<RecordBuffer *> qhRecordBuffer;

QMutex qmBufferLock;
QMutex qmSleepLock;
QWaitCondition qwcSleep;

int iSampleRate;
bool bRecording;
bool bMixDown;
quint64 uiRecordedSamples;
void clearLists();

public:
VoiceRecorder(QObject *p);
~VoiceRecorder();

void run();
void stop();
void addBuffer(ClientUser *cu, boost::shared_array<float> buffer, int samples);
void addSilence(int samples);
void setSampleRate(int sampleRate);
void setMixDown(bool mixDown);
bool getMixDown();
};

#endif
4 changes: 2 additions & 2 deletions src/mumble/mumble.pro
Expand Up @@ -4,8 +4,8 @@ DEFINES *= MUMBLE
TEMPLATE = app
QT *= network sql opengl xml svg
TARGET = mumble
HEADERS *= BanEditor.h ACLEditor.h ConfigWidget.h Log.h AudioConfigDialog.h AudioStats.h AudioInput.h AudioOutput.h CustomElements.h MainWindow.h ServerHandler.h About.h ConnectDialog.h GlobalShortcut.h TextToSpeech.h Settings.h Database.h VersionCheck.h Global.h UserModel.h Audio.h ConfigDialog.h Plugins.h LookConfig.h Overlay.h SharedMemory.h AudioWizard.h ViewCert.h TextMessage.h NetworkConfig.h LCD.h Usage.h Cert.h ClientUser.h UserEdit.h Tokens.h UserView.h RichTextEditor.h UserInformation.h FileEngine.h SocketRPC.h
SOURCES *= BanEditor.cpp ACLEditor.cpp ConfigWidget.cpp Log.cpp AudioConfigDialog.cpp AudioStats.cpp AudioInput.cpp AudioOutput.cpp main.cpp CustomElements.cpp MainWindow.cpp ServerHandler.cpp About.cpp ConnectDialog.cpp Settings.cpp Database.cpp VersionCheck.cpp Global.cpp UserModel.cpp Audio.cpp ConfigDialog.cpp Plugins.cpp LookConfig.cpp Overlay.cpp SharedMemory.cpp AudioWizard.cpp ViewCert.cpp Messages.cpp TextMessage.cpp GlobalShortcut.cpp NetworkConfig.cpp LCD.cpp Usage.cpp Cert.cpp ClientUser.cpp UserEdit.cpp Tokens.cpp UserView.cpp RichTextEditor.cpp UserInformation.cpp FileEngine.cpp SocketRPC.cpp
HEADERS *= BanEditor.h ACLEditor.h ConfigWidget.h Log.h AudioConfigDialog.h AudioStats.h AudioInput.h AudioOutput.h CustomElements.h MainWindow.h ServerHandler.h About.h ConnectDialog.h GlobalShortcut.h TextToSpeech.h Settings.h Database.h VersionCheck.h Global.h UserModel.h Audio.h ConfigDialog.h Plugins.h LookConfig.h Overlay.h SharedMemory.h AudioWizard.h ViewCert.h TextMessage.h NetworkConfig.h LCD.h Usage.h Cert.h ClientUser.h UserEdit.h Tokens.h UserView.h RichTextEditor.h UserInformation.h FileEngine.h SocketRPC.h VoiceRecorder.h
SOURCES *= BanEditor.cpp ACLEditor.cpp ConfigWidget.cpp Log.cpp AudioConfigDialog.cpp AudioStats.cpp AudioInput.cpp AudioOutput.cpp main.cpp CustomElements.cpp MainWindow.cpp ServerHandler.cpp About.cpp ConnectDialog.cpp Settings.cpp Database.cpp VersionCheck.cpp Global.cpp UserModel.cpp Audio.cpp ConfigDialog.cpp Plugins.cpp LookConfig.cpp Overlay.cpp SharedMemory.cpp AudioWizard.cpp ViewCert.cpp Messages.cpp TextMessage.cpp GlobalShortcut.cpp NetworkConfig.cpp LCD.cpp Usage.cpp Cert.cpp ClientUser.cpp UserEdit.cpp Tokens.cpp UserView.cpp RichTextEditor.cpp UserInformation.cpp FileEngine.cpp SocketRPC.cpp VoiceRecorder.cpp
SOURCES *= smallft.cpp
DIST *= ../../icons/mumble.ico licenses.h smallft.h ../../icons/mumble.xpm murmur_pch.h mumble.plist
RESOURCES *= mumble.qrc mumble_flags.qrc
Expand Down
1 change: 1 addition & 0 deletions src/mumble/mumble_pch.hpp
Expand Up @@ -68,6 +68,7 @@
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/accumulators/statistics/extended_p_square.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/bind.hpp>
#ifdef Q_CC_GNU
Expand Down

0 comments on commit be8f819

Please sign in to comment.