Audio Processing Framework (APF) version 0.5.0
mimoprocessor_file_io.h
Go to the documentation of this file.
1/******************************************************************************
2 Copyright (c) 2012-2016 Institut für Nachrichtentechnik, Universität Rostock
3 Copyright (c) 2006-2012 Quality & Usability Lab
4 Deutsche Telekom Laboratories, TU Berlin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23*******************************************************************************/
24
25// https://AudioProcessingFramework.github.io/
26
29
30#include <sndfile.hh> // C++ interface to libsndfile
31#include <iostream>
32
33#include "stopwatch.h"
34#include "apf/container.h" // for fixed_matrix
35
36namespace apf
37{
38
43template<typename Processor>
44int mimoprocessor_file_io(Processor& processor
45 , const std::string& infilename
46 , const std::string& outfilename)
47{
48 std::cout << "Opening file \"" << infilename << "\" ..." << std::endl;
49
50 auto in = SndfileHandle(infilename, SFM_READ);
51
52 if (int err = in.error())
53 {
54 std::cout << in.strError() << std::endl;
55 return err;
56 }
57
58 if (in.samplerate() != static_cast<int>(processor.sample_rate()))
59 {
60 std::cout << "Samplerate mismatch!" << std::endl;
61 return 42;
62 }
63
64 if (in.channels() != processor.in_channels())
65 {
66 std::cout << "Input channel mismatch!" << std::endl;
67 return 666;
68 }
69
70 auto out = SndfileHandle(outfilename, SFM_WRITE
71 , in.format(), processor.out_channels(), in.samplerate());
72
73 if (int err = out.error())
74 {
75 std::cout << out.strError() << std::endl;
76 return err;
77 }
78
79 auto format_info = SF_FORMAT_INFO();
80 format_info.format = in.format();
81 in.command(SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
82
83 std::cout << "format: " << format_info.name << std::endl;
84
85 std::cout << "frames: " << in.frames()
86 << " (" << in.frames()/in.samplerate() << " seconds)" << std::endl;
87 std::cout << "channels: " << in.channels() << std::endl;
88 std::cout << "samplerate: " << in.samplerate() << std::endl;
89
90 auto blocksize = processor.block_size();
91
92 // these matrices are used for de-interleaving and interleaving
93 fixed_matrix<float> m_in(blocksize, size_t(in.channels()));
94 fixed_matrix<float> m_in_transpose(size_t(in.channels()), blocksize);
95 fixed_matrix<float> m_out(blocksize, size_t(processor.out_channels()));
96 fixed_matrix<float> m_out_transpose(
97 size_t(processor.out_channels()), blocksize);
98
99 processor.activate();
100
101 StopWatch watch("processing");
102
103 sf_count_t actual_frames = 0;
104 while ((actual_frames = in.readf(m_in.data()
105 , static_cast<sf_count_t>(blocksize))) != 0)
106 {
107 m_in_transpose.set_channels(m_in.slices);
108
109 processor.audio_callback(blocksize
110 , m_in_transpose.get_channel_ptrs()
111 , m_out_transpose.get_channel_ptrs());
112
113 m_out.set_channels(m_out_transpose.slices);
114
115 out.writef(m_out.data(), actual_frames);
116 }
117
118 //out.writeSync(); // write cache buffers to disk
119
120 processor.deactivate();
121
122 return 0;
123}
124
125} // namespace apf
A simple stopwatch.
Definition: stopwatch.h:44
Two-dimensional data storage for row- and column-wise access.
Definition: container.h:342
has_begin_and_end< slices_iterator > slices
Access to Slices; use slices.begin() and slices.end()
Definition: container.h:418
pointer const * get_channel_ptrs() const
Get array of pointers to the channels.
Definition: container.h:413
void set_channels(const Ch &ch)
Copy channels from another matrix.
Definition: container.h:589
Some containers.
Audio Processing Framework.
Definition: iterator.h:61
int mimoprocessor_file_io(Processor &processor, const std::string &infilename, const std::string &outfilename)
Use MimoProcessor-based object with multichannel audio file input and output.
A simple stopwatch.