Essentia  2.1-beta6-dev
audiocontext.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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 SwrContext* _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:
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
Definition: audiocontext.h:35
bool isOpen() const
Definition: audiocontext.h:60
void write(const std::vector< AudioSample > &monoData)
void write(const std::vector< StereoSample > &stereoData)
float * _buffer
Definition: audiocontext.h:45
AVFormatContext * _muxCtx
Definition: audiocontext.h:41
AVStream * _avStream
Definition: audiocontext.h:40
int create(const std::string &filename, const std::string &format, int nChannels, int sampleRate, int bitrate)
int _inputBufSize
Definition: audiocontext.h:44
std::string _filename
Definition: audiocontext.h:38
void encodePacket(int size)
~AudioContext()
Definition: audiocontext.h:56
uint8_t * _buffer_test
Definition: audiocontext.h:46
struct SwrContext * _convertCtxAv
Definition: audiocontext.h:48
int16_t scale(Real value)
AVCodecContext * _codecCtx
Definition: audiocontext.h:42
bool _isOpen
Definition: audiocontext.h:37
Definition: algorithm.h:28
float Real
Definition: types.h:69