Commit 71e2f28e authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Add sample rate & number of channels in audio track

parent 5a3de8e2
......@@ -10,6 +10,8 @@ class IAudioTrack
virtual unsigned int id() const = 0;
virtual const std::string& codec() const = 0;
virtual unsigned int bitrate() const = 0;
virtual unsigned int sampleRate() const = 0;
virtual unsigned int nbChannels() const = 0;
};
#endif // IAUDIOTRACK_H
......@@ -39,7 +39,8 @@ class IFile
virtual bool addVideoTrack( const std::string& codec, unsigned int width,
unsigned int height, float fps ) = 0;
virtual bool videoTracks( std::vector<VideoTrackPtr>& tracks ) = 0;
virtual bool addAudioTrack( const std::string& codec, unsigned int bitrate ) = 0;
virtual bool addAudioTrack( const std::string& codec, unsigned int bitrate,
unsigned int sampleRate, unsigned int nbChannels ) = 0;
virtual bool audioTracks( std::vector<AudioTrackPtr>& tracks ) = 0;
};
......
......@@ -9,13 +9,17 @@ AudioTrack::AudioTrack( DBConnection dbConnection, sqlite3_stmt* stmt )
, m_id( Traits<unsigned int>::Load( stmt, 0 ) )
, m_codec( Traits<std::string>::Load( stmt, 1 ) )
, m_bitrate( Traits<unsigned int>::Load( stmt, 2 ) )
, m_sampleRate( Traits<unsigned int>::Load( stmt, 3 ) )
, m_nbChannels( Traits<unsigned int>::Load( stmt, 4 ) )
{
}
AudioTrack::AudioTrack( const std::string& codec, unsigned int bitrate )
AudioTrack::AudioTrack(const std::string& codec, unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels )
: m_id( 0 )
, m_codec( codec )
, m_bitrate( bitrate )
, m_sampleRate( sampleRate )
, m_nbChannels( nbChannels )
{
}
......@@ -34,6 +38,16 @@ unsigned int AudioTrack::bitrate() const
return m_bitrate;
}
unsigned int AudioTrack::sampleRate() const
{
return m_sampleRate;
}
unsigned int AudioTrack::nbChannels() const
{
return m_nbChannels;
}
bool AudioTrack::createTable( DBConnection dbConnection )
{
static const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::AudioTrackTable::Name
......@@ -41,24 +55,28 @@ bool AudioTrack::createTable( DBConnection dbConnection )
policy::AudioTrackTable::CacheColumn + " INTEGER PRIMARY KEY AUTOINCREMENT,"
"codec TEXT,"
"bitrate UNSIGNED INTEGER,"
"samplerate UNSIGNED INTEGER,"
"nb_channels UNSIGNED INTEGER,"
"UNIQUE ( codec, bitrate ) ON CONFLICT FAIL"
")";
return SqliteTools::executeRequest( dbConnection, req );
}
AudioTrackPtr AudioTrack::fetch( DBConnection dbConnection, const std::string& codec, unsigned int bitrate )
AudioTrackPtr AudioTrack::fetch(DBConnection dbConnection, const std::string& codec,
unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels )
{
static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name
+ " WHERE codec = ? AND bitrate = ?";
return SqliteTools::fetchOne<AudioTrack>( dbConnection, req, codec, bitrate );
+ " WHERE codec = ? AND bitrate = ? AND samplerate = ? AND nb_channels = ?";
return SqliteTools::fetchOne<AudioTrack>( dbConnection, req, codec, bitrate, sampleRate, nbChannels );
}
AudioTrackPtr AudioTrack::create( DBConnection dbConnection, const std::string& codec, unsigned int bitrate )
AudioTrackPtr AudioTrack::create( DBConnection dbConnection, const std::string& codec,
unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels )
{
static const std::string req = "INSERT INTO " + policy::AudioTrackTable::Name
+ "(codec, bitrate) VALUES(?, ?)";
auto track = std::make_shared<AudioTrack>( codec, bitrate );
if ( _Cache::insert( dbConnection, track, req, codec, bitrate ) == false )
+ "(codec, bitrate, samplerate, nb_channels) VALUES(?, ?, ?, ?)";
auto track = std::make_shared<AudioTrack>( codec, bitrate, sampleRate, nbChannels );
if ( _Cache::insert( dbConnection, track, req, codec, bitrate, sampleRate, nbChannels ) == false )
return nullptr;
track->m_dbConnection = dbConnection;
return track;
......
......@@ -21,21 +21,26 @@ class AudioTrack : public IAudioTrack, public Cache<AudioTrack, IAudioTrack, pol
{
public:
AudioTrack( DBConnection dbConnection, sqlite3_stmt* stmt );
AudioTrack( const std::string& codec, unsigned int bitrate );
AudioTrack( const std::string& codec, unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels );
virtual unsigned int id() const;
virtual const std::string&codec() const;
virtual unsigned int bitrate() const;
virtual unsigned int sampleRate() const;
virtual unsigned int nbChannels() const;
static bool createTable( DBConnection dbConnection );
static AudioTrackPtr fetch( DBConnection dbConnection, const std::string& codec, unsigned int bitrate );
static AudioTrackPtr create( DBConnection dbConnection, const std::string& codec, unsigned int bitrate );
static AudioTrackPtr fetch( DBConnection dbConnection, const std::string& codec,
unsigned int bitrate, unsigned int sampleRate, unsigned int nbChannels );
static AudioTrackPtr create(DBConnection dbConnection, const std::string& codec, unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels);
private:
DBConnection m_dbConnection;
unsigned int m_id;
const std::string m_codec;
const unsigned int m_bitrate;
const unsigned int m_sampleRate;
const unsigned int m_nbChannels;
private:
......
......@@ -160,14 +160,15 @@ bool File::videoTracks(std::vector<VideoTrackPtr>& tracks)
return SqliteTools::fetchAll<VideoTrack>( m_dbConnection, req, tracks, m_id );
}
bool File::addAudioTrack(const std::string& codec, unsigned int bitrate)
bool File::addAudioTrack( const std::string& codec, unsigned int bitrate,
unsigned int sampleRate, unsigned int nbChannels )
{
static const std::string req = "INSERT INTO AudioTrackFileRelation VALUES(?, ?)";
auto track = AudioTrack::fetch( m_dbConnection, codec, bitrate );
auto track = AudioTrack::fetch( m_dbConnection, codec, bitrate, sampleRate, nbChannels );
if ( track == nullptr )
{
track = AudioTrack::create( m_dbConnection, codec, bitrate );
track = AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels );
if ( track == nullptr )
return false;
}
......
......@@ -61,7 +61,7 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy::
virtual bool addVideoTrack( const std::string& codec, unsigned int width,
unsigned int height, float fps );
virtual bool videoTracks( std::vector<VideoTrackPtr>& tracks );
virtual bool addAudioTrack( const std::string& codec, unsigned int bitrate );
virtual bool addAudioTrack(const std::string& codec, unsigned int bitrate , unsigned int sampleRate, unsigned int nbChannels);
virtual bool audioTracks( std::vector<AudioTrackPtr>& tracks );
private:
......
......@@ -28,15 +28,15 @@ std::unique_ptr<IMediaLibrary> AudioTracks::ml;
TEST_F( AudioTracks, AddTrack )
{
auto f = ml->addFile( "file" );
bool res = f->addAudioTrack( "PCM", 44100 );
bool res = f->addAudioTrack( "PCM", 44100, 128, 2 );
ASSERT_TRUE( res );
}
TEST_F( AudioTracks, FetchTracks )
{
auto f = ml->addFile( "file" );
f->addAudioTrack( "PCM", 44100 );
f->addAudioTrack( "WMA", 48000 );
f->addAudioTrack( "PCM", 44100, 128, 2 );
f->addAudioTrack( "WMA", 48000, 128, 2 );
std::vector<AudioTrackPtr> ts;
bool res = f->audioTracks( ts );
......@@ -47,10 +47,10 @@ TEST_F( AudioTracks, FetchTracks )
TEST_F( AudioTracks, CheckUnique )
{
auto f = ml->addFile( "file" );
f->addAudioTrack( "PCM", 44100 );
f->addAudioTrack( "PCM", 44100, 128, 2 );
auto f2 = ml->addFile( "file2" );
f2->addAudioTrack( "PCM", 44100 );
f2->addAudioTrack( "PCM", 44100, 128, 2 );
std::vector<AudioTrackPtr> ts;
f->audioTracks( ts );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment