-
Notifications
You must be signed in to change notification settings - Fork 50
Add AudioTransform: convert samples #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36de2c6
e21acb7
f085e6e
6f68e2f
887df9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ audioRewrapper = env.Program( | |
'avformat', | ||
'avcodec', | ||
'swscale', | ||
'swresample', | ||
] | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "AudioTransform.hpp" | ||
#include "DatasStructures/AudioFrame.hpp" | ||
#include "common.hpp" | ||
|
||
extern "C" { | ||
#ifndef __STDC_CONSTANT_MACROS | ||
#define __STDC_CONSTANT_MACROS | ||
#endif | ||
#include <libavcodec/avcodec.h> | ||
#include <libavutil/opt.h> | ||
#include <libswresample/swresample.h> | ||
#if LIBAVCODEC_VERSION_MAJOR > 54 | ||
#include <libavutil/frame.h> | ||
#endif | ||
} | ||
|
||
#include <stdexcept> | ||
|
||
namespace avtranscoder | ||
{ | ||
|
||
AudioTransform::AudioTransform() | ||
: m_audioConvertContext( NULL ) | ||
, m_isInit ( false ) | ||
{ | ||
} | ||
|
||
bool AudioTransform::init( const AudioFrame& src, const AudioFrame& dst ) | ||
{ | ||
m_audioConvertContext = swr_alloc(); | ||
|
||
if( !m_audioConvertContext ) | ||
{ | ||
throw std::runtime_error( "unable to create audio convert context" ); | ||
} | ||
|
||
swr_alloc_set_opts( m_audioConvertContext, | ||
av_get_default_channel_layout( dst.desc().getChannels() ), dst.desc().getSampleFormat(), av_get_default_channel_layout( dst.desc().getSampleRate() ), | ||
av_get_default_channel_layout( src.desc().getChannels() ), src.desc().getSampleFormat(), av_get_default_channel_layout( src.desc().getSampleRate() ), | ||
0, NULL); | ||
|
||
if( swr_init( m_audioConvertContext ) < 0 ) | ||
{ | ||
swr_free( &m_audioConvertContext ); | ||
throw std::runtime_error( "unable to open audio convert context" ); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void AudioTransform::convert( const AudioFrame& src, AudioFrame& dst ) | ||
{ | ||
if( ! m_isInit ) | ||
{ | ||
m_isInit = init( src, dst ); | ||
m_isInit = true; | ||
} | ||
|
||
if( dst.getSize() != src.getSize() ) | ||
dst.getBuffer().resize( src.getSize(), 0 ); | ||
|
||
const unsigned char* srcData = src.getPtr(); | ||
unsigned char* dstData = dst.getPtr(); | ||
|
||
swr_convert( m_audioConvertContext, &dstData, dst.getSize(), &srcData, src.getSize() ); | ||
|
||
dst.setNbSamples( src.getNbSamples() ); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef _AV_TRANSCODER_AUDIO_TRANSFORM_HPP | ||
#define _AV_TRANSCODER_AUDIO_TRANSFORM_HPP | ||
|
||
#include "common.hpp" | ||
|
||
class SwrContext; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. forward declaration outside of the namespace? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SwrContext is a LibAV class, so can't declare it inside avTranscoder namespace. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, it's private and doesn't appear in the API. So it's a good thing to use forward declaration. |
||
|
||
namespace avtranscoder | ||
{ | ||
|
||
class AudioFrame; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used in the public API, so should not use forward declaration. |
||
|
||
class AvExport AudioTransform | ||
{ | ||
public: | ||
AudioTransform(); | ||
|
||
void convert( const AudioFrame& src, AudioFrame& dst ); | ||
|
||
private: | ||
bool init( const AudioFrame& src, const AudioFrame& dst ); | ||
|
||
SwrContext* m_audioConvertContext; | ||
|
||
bool m_isInit; | ||
}; | ||
|
||
} | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no magick number without explanation.