Essentia  2.1-beta5-dev
audiocontext.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_AUDIOWRITERTOOL_H
21 #define ESSENTIA_AUDIOWRITERTOOL_H
22 
23 #include <string>
24 #include <vector>
25 #include "types.h"
26 #include "ffmpegapi.h"
27 
28 #define MAX_AUDIO_FRAME_SIZE 192000 // the same value as in AudioLoader
29 
30 namespace essentia {
31 
35 class AudioContext {
36  protected:
37  bool _isOpen;
38  std::string _filename;
39 
40  AVStream* _avStream;
41  AVFormatContext* _muxCtx;
42  AVCodecContext* _codecCtx;
43 
44  int _inputBufSize; // input buffer size
45  float* _buffer; // input FLT buffer interleaved
46  uint8_t* _buffer_test; // input buffer in converted to codec sample format
47 
48  struct AVAudioResampleContext* _convertCtxAv;
49 
50  //const static int FFMPEG_BUFFER_SIZE = MAX_AUDIO_FRAME_SIZE * 2;
51  // MAX_AUDIO_FRAME_SIZE is in bytes, multiply it by 2 to get some margin
52 
53 
54  public:
55  AudioContext();
57  int create(const std::string& filename, const std::string& format,
58  int nChannels, int sampleRate, int bitrate);
59  void open();
60  bool isOpen() const { return _isOpen; }
61  void write(const std::vector<AudioSample>& monoData);
62  void write(const std::vector<StereoSample>& stereoData);
63  void close();
64 
65  protected:
66  int16_t scale(Real value);
67  void encodePacket(int size);
68  void writeEOF();
69 };
70 
71 } // namespace essentia
72 
73 #endif // ESSENTIA_AUDIOWRITERTOOL_H
uint8_t * _buffer_test
Definition: audiocontext.h:46
int _inputBufSize
Definition: audiocontext.h:44
AVCodecContext * _codecCtx
Definition: audiocontext.h:42
~AudioContext()
Definition: audiocontext.h:56
int16_t scale(Real value)
float Real
Definition: types.h:68
struct AVAudioResampleContext * _convertCtxAv
Definition: audiocontext.h:48
bool isOpen() const
Definition: audiocontext.h:60
std::string _filename
Definition: audiocontext.h:38
Definition: algorithm.h:28
AVFormatContext * _muxCtx
Definition: audiocontext.h:41
AVStream * _avStream
Definition: audiocontext.h:40
float * _buffer
Definition: audiocontext.h:45
void encodePacket(int size)
bool _isOpen
Definition: audiocontext.h:37
int create(const std::string &filename, const std::string &format, int nChannels, int sampleRate, int bitrate)
void write(const std::vector< AudioSample > &monoData)
Definition: audiocontext.h:35