Skip to content

Commit

Permalink
LibAudio+aplay: Make the aplay output look a little funner.
Browse files Browse the repository at this point in the history
Show some information about the file we're playing, and display how many
samples we've played out of how many total.

This might be a bit buggy as I haven't tested it with many different files,
but it's a start. :^)
  • Loading branch information
awesomekling committed Jul 28, 2019
1 parent b44d3fa commit ae4a9e0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
8 changes: 7 additions & 1 deletion Libraries/LibAudio/AWavLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ RefPtr<ABuffer> AWavLoader::get_more_samples()
auto raw_samples = m_file.read(128 * KB);
if (raw_samples.is_empty())
return nullptr;
return ABuffer::from_pcm_data(raw_samples, m_num_channels, m_bits_per_sample, m_sample_rate);

auto buffer = ABuffer::from_pcm_data(raw_samples, m_num_channels, m_bits_per_sample, m_sample_rate);
m_loaded_samples += buffer->sample_count();
return buffer;
}

bool AWavLoader::parse_header()
Expand Down Expand Up @@ -125,6 +128,9 @@ bool AWavLoader::parse_header()
ok = ok && data_sz < INT32_MAX;
CHECK_OK("Data was too large");

int bytes_per_sample = (m_bits_per_sample / 8) * m_num_channels;
m_total_samples = data_sz / bytes_per_sample;

// Just make sure we're good before we read the data...
ASSERT(!stream.handle_read_failure());

Expand Down
9 changes: 9 additions & 0 deletions Libraries/LibAudio/AWavLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class AWavLoader {

RefPtr<ABuffer> get_more_samples();

int loaded_samples() const { return m_loaded_samples; }
int total_samples() const { return m_total_samples; }
u32 sample_rate() const { return m_sample_rate; }
u16 num_channels() const { return m_num_channels; }
u16 bits_per_sample() const { return m_bits_per_sample; }

private:
bool parse_header();
CFile m_file;
Expand All @@ -28,4 +34,7 @@ class AWavLoader {
u32 m_sample_rate { 0 };
u16 m_num_channels { 0 };
u16 m_bits_per_sample { 0 };

int m_loaded_samples { 0 };
int m_total_samples { 0 };
};
27 changes: 15 additions & 12 deletions Userland/aplay.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
#include <LibCore/CEventLoop.h>
#include <LibAudio/AWavLoader.h>
#include <LibAudio/AClientConnection.h>
#include <LibAudio/ABuffer.h>
#include <LibAudio/AClientConnection.h>
#include <LibAudio/AWavLoader.h>
#include <LibCore/CEventLoop.h>
#include <cstdio>

int main(int argc, char **argv)
int main(int argc, char** argv)
{
CEventLoop loop;
if (argc < 2) {
fprintf(stderr, "Need a WAV to play\n");
return 1;
}

printf("Establishing connection\n");
AClientConnection a_conn;
a_conn.handshake();
printf("Established connection\n");
AWavLoader loader(argv[1]);
printf("Loaded WAV\n");

printf("\033[34;1mPlaying\033[0m: %s\n", argv[1]);
printf("\033[34;1m Format\033[0m: %u Hz, %u-bit, %s\n",
loader.sample_rate(),
loader.bits_per_sample(),
loader.num_channels() == 1 ? "Mono" : "Stereo");
printf("\033[34;1m Sample\033[0m: \033[s");
for (;;) {
auto samples = loader.get_more_samples();
if (!samples) {
if (!samples)
break;
}
printf("Playing %d sample(s)\n", samples->sample_count());
printf("\033[u");
printf("%d/%d", loader.loaded_samples(), loader.total_samples());
fflush(stdout);
a_conn.enqueue(*samples);
}

printf("Exiting! :)\n");
printf("\n");
return 0;
}

0 comments on commit ae4a9e0

Please sign in to comment.