Skip to content

Commit

Permalink
Specify the audio stream to analyse
Browse files Browse the repository at this point in the history
References alexkay#18.
  • Loading branch information
alexkay committed Apr 25, 2016
1 parent ac9c92d commit 6cda85e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
29 changes: 17 additions & 12 deletions src/spek-audio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AudioFileImpl : public AudioFile
AudioFileImpl(
AudioError error, AVFormatContext *format_context, int audio_stream,
const std::string& codec_name, int bit_rate, int sample_rate, int bits_per_sample,
int channels, double duration
int streams, int channels, double duration
);
~AudioFileImpl() override;
void start(int channel, int samples) override;
Expand All @@ -27,6 +27,7 @@ class AudioFileImpl : public AudioFile
int get_bit_rate() const override { return this->bit_rate; }
int get_sample_rate() const override { return this->sample_rate; }
int get_bits_per_sample() const override { return this->bits_per_sample; }
int get_streams() const override { return this->streams; }
int get_channels() const override { return this->channels; }
double get_duration() const override { return this->duration; }
const float *get_buffer() const override { return this->buffer; }
Expand All @@ -42,6 +43,7 @@ class AudioFileImpl : public AudioFile
int bit_rate;
int sample_rate;
int bits_per_sample;
int streams;
int channels;
double duration;

Expand Down Expand Up @@ -70,7 +72,7 @@ Audio::~Audio()
av_lockmgr_register(nullptr);
}

std::unique_ptr<AudioFile> Audio::open(const std::string& file_name)
std::unique_ptr<AudioFile> Audio::open(const std::string& file_name, int stream)
{
AudioError error = AudioError::OK;

Expand All @@ -88,24 +90,27 @@ std::unique_ptr<AudioFile> Audio::open(const std::string& file_name)
}

int audio_stream = -1;
int streams = 0;
if (!error) {
for (unsigned int i = 0; i < format_context->nb_streams; i++) {
if (format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream = i;
break;
if (stream == streams) {
audio_stream = i;
}
streams++;
}
}
if (audio_stream == -1) {
error = AudioError::NO_AUDIO;
}
}

AVStream *stream = nullptr;
AVStream *avstream = nullptr;
AVCodecContext *codec_context = nullptr;
AVCodec *codec = nullptr;
if (!error) {
stream = format_context->streams[audio_stream];
codec_context = stream->codec;
avstream = format_context->streams[audio_stream];
codec_context = avstream->codec;
codec = avcodec_find_decoder(codec_context->codec_id);
if (!codec) {
error = AudioError::NO_DECODER;
Expand Down Expand Up @@ -140,8 +145,8 @@ std::unique_ptr<AudioFile> Audio::open(const std::string& file_name)
}
channels = codec_context->channels;

if (stream->duration != AV_NOPTS_VALUE) {
duration = stream->duration * av_q2d(stream->time_base);
if (avstream->duration != AV_NOPTS_VALUE) {
duration = avstream->duration * av_q2d(avstream->time_base);
} else if (format_context->duration != AV_NOPTS_VALUE) {
duration = format_context->duration / (double) AV_TIME_BASE;
} else {
Expand Down Expand Up @@ -170,19 +175,19 @@ std::unique_ptr<AudioFile> Audio::open(const std::string& file_name)
return std::unique_ptr<AudioFile>(new AudioFileImpl(
error, format_context, audio_stream,
codec_name, bit_rate, sample_rate, bits_per_sample,
channels, duration
streams, channels, duration
));
}

AudioFileImpl::AudioFileImpl(
AudioError error, AVFormatContext *format_context, int audio_stream,
const std::string& codec_name, int bit_rate, int sample_rate, int bits_per_sample,
int channels, double duration
int streams, int channels, double duration
) :
error(error), format_context(format_context), audio_stream(audio_stream),
codec_name(codec_name), bit_rate(bit_rate),
sample_rate(sample_rate), bits_per_sample(bits_per_sample),
channels(channels), duration(duration)
streams(streams), channels(channels), duration(duration)
{
av_init_packet(&this->packet);
this->packet.data = nullptr;
Expand Down
3 changes: 2 additions & 1 deletion src/spek-audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Audio
Audio();
~Audio();

std::unique_ptr<AudioFile> open(const std::string& file_name);
std::unique_ptr<AudioFile> open(const std::string& file_name, int stream);
};

class AudioFile
Expand All @@ -29,6 +29,7 @@ class AudioFile
virtual int get_bit_rate() const = 0;
virtual int get_sample_rate() const = 0;
virtual int get_bits_per_sample() const = 0;
virtual int get_streams() const = 0;
virtual int get_channels() const = 0;
virtual double get_duration() const = 0;
virtual const float *get_buffer() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/spek-spectrogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ void SpekSpectrogram::start()
if (samples > 0) {
this->image.Create(samples, bits_to_bands(this->fft_bits));
this->pipeline = spek_pipeline_open(
this->audio->open(std::string(this->path.utf8_str())),
this->audio->open(std::string(this->path.utf8_str()), 0),
this->fft->create(this->fft_bits),
this->channel,
this->window_function,
Expand Down
2 changes: 1 addition & 1 deletion tests/test-audio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void test_audio()
for (const auto& item : files) {
auto name = item.first;
auto info = item.second;
auto file = audio.open(SAMPLES_DIR "/" + name);
auto file = audio.open(SAMPLES_DIR "/" + name, 0);
run(
"audio info: " + name,
[&] () { test_info(file.get(), info); }
Expand Down

0 comments on commit 6cda85e

Please sign in to comment.