Mercurial > hg > svapp
comparison audioio/ClipMixer.cpp @ 305:9716c75499ef tonioni
Toward using a sample mixer (with arbitrary frequency target) instead of dssi player plugin
author | Chris Cannam |
---|---|
date | Tue, 07 Jan 2014 10:58:10 +0000 |
parents | |
children | 6eb15c3aee0a |
comparison
equal
deleted
inserted
replaced
303:b6358ba5ebc6 | 305:9716c75499ef |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006 Chris Cannam, 2006-2014 QMUL. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "ClipMixer.h" | |
17 | |
18 #include <sndfile.h> | |
19 | |
20 #include "base/Debug.h" | |
21 | |
22 ClipMixer::ClipMixer(int channels, int sampleRate, int blockSize) : | |
23 m_channels(channels), | |
24 m_sampleRate(sampleRate), | |
25 m_blockSize(blockSize), | |
26 m_clipData(0) | |
27 { | |
28 } | |
29 | |
30 ClipMixer::~ClipMixer() | |
31 { | |
32 delete[] m_clipData; | |
33 } | |
34 | |
35 bool | |
36 ClipMixer::loadClipData(QString path, float f0) | |
37 { | |
38 if (m_clipData) { | |
39 cerr << "ClipPlayer::loadClipData: Already have clip loaded" << endl; | |
40 return false; | |
41 } | |
42 | |
43 SF_INFO info; | |
44 SNDFILE *file; | |
45 int sampleCount = 0; | |
46 float *tmpFrames; | |
47 size_t i; | |
48 | |
49 info.format = 0; | |
50 file = sf_open(path.toLocal8Bit().data(), SFM_READ, &info); | |
51 if (!file) { | |
52 cerr << "ClipPlayer::loadClipData: Failed to open file " | |
53 << path << ": " << sf_strerror(file) << endl; | |
54 return false; | |
55 } | |
56 | |
57 tmpFrames = (float *)malloc(info.frames * info.channels * sizeof(float)); | |
58 if (!tmpFrames) { | |
59 cerr << "ClipPlayer::loadClipData: malloc(" << info.frames * info.channels * sizeof(float) << ") failed" << endl; | |
60 return false; | |
61 } | |
62 | |
63 sf_readf_float(file, tmpFrames, info.frames); | |
64 sf_close(file); | |
65 | |
66 m_clipData = (float *)malloc(info.frames * sizeof(float)); | |
67 if (!m_clipData) { | |
68 cerr << "ClipPlayer::loadClipData: malloc(" << info.frames * sizeof(float) << ") failed" << endl; | |
69 free(tmpFrames); | |
70 return false; | |
71 } | |
72 | |
73 for (i = 0; i < info.frames; ++i) { | |
74 int j; | |
75 m_clipData[i] = 0.0f; | |
76 for (j = 0; j < info.channels; ++j) { | |
77 m_clipData[i] += tmpFrames[i * info.channels + j]; | |
78 } | |
79 } | |
80 | |
81 free(tmpFrames); | |
82 | |
83 m_clipLength = info.frames; | |
84 m_clipF0 = f0; | |
85 m_clipRate = info.samplerate; | |
86 } |