AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AudioGenerator.cpp
Go to the documentation of this file.
1 #include "AudioGenerator.hpp"
2 
3 #include <AvTranscoder/util.hpp>
4 
5 #include <sstream>
6 
7 namespace avtranscoder
8 {
9 
11  : _inputFrame(NULL)
12  , _silent(NULL)
13  , _frameDesc(frameDesc)
14 {
15 }
16 
18 {
19  delete _silent;
20 }
21 
23 {
24  // check the given frame
25  if(!frameBuffer.isAudioFrame())
26  {
27  LOG_WARN("The given frame is not a valid audio frame: allocate a new AVSample to put generated data into it.");
28  frameBuffer.clear();
29  static_cast<AudioFrame&>(frameBuffer).allocateAVSample(_frameDesc);
30  }
31 
32  // Check channel layout of the given frame to be able to copy audio data to it.
33  // @see Frame.copyData method
34  if(frameBuffer.getAVFrame().channel_layout == 0)
35  {
36  const size_t channelLayout = av_get_default_channel_layout(frameBuffer.getAVFrame().channels);
37  LOG_WARN("Channel layout en the audio frame is not set. Set it to '" << channelLayout << "' to be able to copy silence data.")
38  av_frame_set_channel_layout(&frameBuffer.getAVFrame(), channelLayout);
39  }
40 
41  // Generate silent
42  if(!_inputFrame)
43  {
44  // Generate the silent only once
45  if(!_silent)
46  {
47  AudioFrame& audioBuffer = static_cast<AudioFrame&>(frameBuffer);
48  _silent = new AudioFrame(audioBuffer.desc());
49 
50  std::stringstream msg;
51  msg << "Generate a silence with the following features:" << std::endl;
52  msg << "sample rate = " << _silent->getSampleRate() << std::endl;
53  msg << "number of channels = " << _silent->getNbChannels() << std::endl;
54  msg << "sample format = " << getSampleFormatName(_silent->getSampleFormat()) << std::endl;
55  msg << "number of samples per channel = " << _silent->getNbSamplesPerChannel() << std::endl;
56  LOG_INFO(msg.str())
57 
58  // Set the number of samples of silence to the number of samples of the given frame
59  // (which was allocated to expect this number of samples).
60  _silent->setNbSamplesPerChannel(frameBuffer.getAVFrame().nb_samples);
61  }
62  LOG_DEBUG("Copy data of the silence when decode next frame")
63  frameBuffer.copyData(*_silent);
64  }
65  // Take audio frame from _inputFrame
66  else
67  {
68  LOG_DEBUG("Copy data of the audio specified when decode next frame")
69  frameBuffer.copyData(*_inputFrame);
70  }
71  return true;
72 }
73 
74 bool AudioGenerator::decodeNextFrame(Frame& frameBuffer, const size_t subStreamIndex)
75 {
76  return decodeNextFrame(frameBuffer);
77 }
78 }
AudioFrameDesc desc() const
Definition: AudioFrame.hpp:50
void clear()
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: Frame.cpp:71
AVSampleFormat getSampleFormat() const
Definition: AudioFrame.hpp:48
AudioGenerator(const AudioGenerator &audioGenerator)
bool isAudioFrame() const
Definition: Frame.cpp:89
void setNbSamplesPerChannel(const size_t nbSamples)
Definition: AudioFrame.hpp:54
#define LOG_INFO(...)
Definition: log.hpp:23
const AudioFrameDesc _frameDesc
The description of the silence (sampleRate, channels...)
#define LOG_WARN(...)
Definition: log.hpp:29
Description to create an audio frame. This corresponds to the number of samples, which corresponds to...
Definition: AudioFrame.hpp:14
bool decodeNextFrame(Frame &frameBuffer)
Decode next frame.
size_t getSampleRate() const
Definition: AudioFrame.hpp:45
size_t getNbChannels() const
Definition: AudioFrame.hpp:46
This class describes decoded audio data.
Definition: AudioFrame.hpp:36
This class describes decoded (raw) audio or video data.
Definition: Frame.hpp:16
#define LOG_DEBUG(...)
Definition: log.hpp:17
void copyData(const Frame &frameToRef)
Copy the data of the given Frame.
Definition: Frame.cpp:48
std::string getSampleFormatName(const AVSampleFormat sampleFormat)
Definition: util.cpp:106
AVFrame & getAVFrame()
Definition: Frame.hpp:88
Frame * _inputFrame
Has link (no ownership)
size_t getNbSamplesPerChannel() const
Definition: AudioFrame.hpp:49
AudioFrame * _silent
The generated silent (has ownership)