~ubuntu-branches/ubuntu/wily/sflphone/wily-proposed

« back to all changes in this revision

Viewing changes to daemon/src/audio/audiorecord.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#endif
34
34
 
35
35
#include "audiorecord.h"
 
36
#include "logger.h"
 
37
#include "fileutils.h"
 
38
 
36
39
#include <sndfile.hh>
37
 
#include <unistd.h>
 
40
 
 
41
#include <algorithm>
38
42
#include <sstream> // for stringstream
39
 
#include <algorithm>
40
43
#include <cstdio>
41
 
#include "logger.h"
42
 
#include "fileutils.h"
 
44
#include <unistd.h>
43
45
 
44
 
namespace {
45
 
std::string
 
46
static std::string
46
47
createFilename()
47
48
{
48
49
    time_t rawtime = time(NULL);
86
87
    out << timeinfo->tm_sec;
87
88
    return out.str();
88
89
}
89
 
}
90
 
 
91
 
 
92
 
AudioRecord::AudioRecord() : fileHandle_(0)
93
 
    , channels_(1)
94
 
    , sndSmplRate_(8000)
 
90
 
 
91
AudioRecord::AudioRecord() : fileHandle_(nullptr)
 
92
    , sndFormat_(AudioFormat::MONO)
95
93
    , recordingEnabled_(false)
96
94
    , filename_(createFilename())
97
95
    , savePath_()
104
102
    delete fileHandle_;
105
103
}
106
104
 
107
 
void AudioRecord::setSndSamplingRate(int smplRate)
 
105
void AudioRecord::setSndFormat(AudioFormat format)
108
106
{
109
 
    sndSmplRate_ = smplRate;
 
107
    sndFormat_ = format;
110
108
}
111
109
 
112
 
void AudioRecord::setRecordingOptions(int sndSmplRate, const std::string &path)
 
110
void AudioRecord::setRecordingOptions(AudioFormat format, const std::string &path)
113
111
{
114
112
    std::string filePath;
115
113
 
120
118
        filePath = path;
121
119
    }
122
120
 
123
 
    channels_ = 1;
124
 
    sndSmplRate_ = sndSmplRate;
 
121
    sndFormat_ = format;
125
122
    savePath_ = (*filePath.rbegin() == DIR_SEPARATOR_CH) ? filePath : filePath + DIR_SEPARATOR_STR;
126
123
}
127
124
 
128
 
namespace {
129
 
bool
 
125
static bool
130
126
nonFilenameCharacter(char c)
131
127
{
132
128
    return not(std::isalnum(c) or c == '_' or c == '.');
133
129
}
134
130
 
135
131
// Replace any character that is inappropriate for a filename with '_'
136
 
std::string
 
132
static std::string
137
133
sanitize(std::string s)
138
134
{
139
135
    std::replace_if(s.begin(), s.end(), nonFilenameCharacter, '_');
140
136
    return s;
141
137
}
142
 
}
143
138
 
144
139
void AudioRecord::initFilename(const std::string &peerNumber)
145
140
{
166
161
    const bool doAppend = fileExists();
167
162
    const int access = doAppend ? SFM_RDWR : SFM_WRITE;
168
163
 
169
 
    fileHandle_ = new SndfileHandle(savePath_.c_str(), access, SF_FORMAT_WAV | SF_FORMAT_PCM_16, channels_, sndSmplRate_);
 
164
    DEBUG("Opening file %s with format %s", savePath_.c_str(), sndFormat_.toString().c_str());
 
165
    fileHandle_ = new SndfileHandle(savePath_.c_str(), access, SF_FORMAT_WAV | SF_FORMAT_PCM_16, sndFormat_.nb_channels, sndFormat_.sample_rate);
170
166
 
171
167
    // check overloaded boolean operator
172
168
    if (!*fileHandle_) {
231
227
        return;
232
228
    }
233
229
 
234
 
    const int nSamples = buffer.frames();
235
 
 
236
 
    // FIXME: mono only
237
 
    if (fileHandle_->write(buffer.getChannel(0)->data(), nSamples) != nSamples) {
 
230
    auto interleaved = buffer.interleave();
 
231
    const int nSamples = interleaved.size();
 
232
    if (fileHandle_->write(interleaved.data(), nSamples) != nSamples) {
238
233
        WARN("Could not record data!");
239
234
    } else {
240
235
        fileHandle_->writeSync();