Skip to content

Commit

Permalink
WIP audio: use record/play session interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nfeske committed Apr 12, 2024
1 parent e00dc70 commit f108232
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 8 deletions.
47 changes: 42 additions & 5 deletions run/audio_pinephone.run
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
#

build {
core lib/ld init timer lib/libc lib/vfs lib/posix lib/vfs_oss
core lib/ld init timer lib/libc lib/libm lib/vfs lib/posix
drivers/platform/a64 drivers/pin/a64
drivers/audio_control/pinephone
drivers/audio/a64
server/report_rom
test/audio_in test/oss
server/record_play_mixer
app/waveform_player
test/audio_in
lib/vfs_oss
test/oss
}

create_boot_directory
Expand All @@ -34,6 +38,7 @@ install_config {

<start name="timer">
<resource name="RAM" quantum="1M"/>
<resource name="CPU" quantum="20"/>
<route> <any-service> <parent/> </any-service> </route>
<provides> <service name="Timer"/> </provides>
</start>
Expand Down Expand Up @@ -91,7 +96,7 @@ install_config {
<config>
<mic volume="60"/>
<earpiece volume="100"/>
<speaker volume="0"/>
<speaker volume="20"/>
<codec target="soc"/>
</config>
<route>
Expand All @@ -100,19 +105,50 @@ install_config {
</route>
</start>

<start name="mixer">
<resource name="RAM" quantum="2M"/>
<resource name="CPU" quantum="20"/>
<binary name="record_play_mixer"/>
<provides> <service name="Record"/> <service name="Play"/> </provides>
<config jitter_ms="10">

<mix name="left"> <play label_suffix="left" /> </mix>
<mix name="right"> <play label_suffix="right"/> </mix>

<policy label_suffix="left" record="left" volume="0.6"/>
<policy label_suffix="right" record="right" volume="0.0"/>
</config>
</start>

<start name="waveform_player" priority="0">
<resource name="RAM" quantum="1M"/>
<config period_ms="11">
<play label="left" wave="sine" hz="1000" sample_rate_hz="44100"/>
<play label="right" wave="square" hz="1000" sample_rate_hz="44100"/>
</config>
<route>
<any-service> <parent/> <any-child/> </any-service>
</route>
</start>

<start name="audio_drv">
<binary name="a64_audio_drv"/>
<resource name="RAM" quantum="1M"/>
<provides>
<resource name="CPU" quantum="20"/>
<!-- <provides>
<service name="Audio_out"/>
<service name="Audio_in"/>
</provides>
</provides> -->
<config record_play="yes"/>
<route>
<service name="Platform"> <child name="platform_drv"/> </service>
<service name="Record"> <child name="mixer"/> </service>
<service name="Play"> <child name="mixer"/> </service>
<any-service> <parent/> </any-service>
</route>
</start>

<!--
<start name="test-oss">
<resource name="RAM" quantum="10M"/>
<config>
Expand All @@ -130,6 +166,7 @@ install_config {
<any-service> <parent/> <any-child/> </any-service>
</route>
</start>
-->

<!--
<start name="test-audio_in">
Expand Down
88 changes: 85 additions & 3 deletions src/drivers/audio/a64/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@

#include <audio_in_session/rpc_object.h>
#include <audio_out_session/rpc_object.h>
#include <record_session/connection.h>
#include <base/attached_rom_dataspace.h>
#include <base/session_label.h>
#include <base/component.h>
#include <base/heap.h>
#include <base/log.h>
#include <os/reporter.h>
#include <root/component.h>

#include "session.h"
Expand Down Expand Up @@ -451,9 +450,92 @@ struct Audio_aggregator : Audio::Session
};


struct Record_play_aggregator : Audio::Session
{
static constexpr unsigned SAMPLES_PER_PERIOD = Audio_in::PERIOD;
static constexpr unsigned CHANNELS = 2;

Env &_env;

Record::Connection _left { _env, "left" };
Record::Connection _right { _env, "right" };

struct Recording : private Noncopyable
{
bool depleted = false;

/* 16 bit per sample, interleaved left and right */
int16_t data[SAMPLES_PER_PERIOD*CHANNELS] { };

void from_record_sessions(Record::Connection &left, Record::Connection &right)
{
using Samples_ptr = Record::Connection::Samples_ptr;

bool const orig_depleted = depleted;

Record::Num_samples const num_samples { SAMPLES_PER_PERIOD };

auto clamped = [&] (float v)
{
return (v > 1.0) ? 1.0
: (v < -1.0) ? -1.0
: v;
};

auto float_to_s16 = [&] (float v) { return int16_t(clamped(v)*32767); };

left.record(num_samples,
[&] (Record::Time_window const tw, Samples_ptr const &samples) {
depleted = false;

for (unsigned i = 0; i < SAMPLES_PER_PERIOD; i++)
data[i*CHANNELS] = float_to_s16(samples.start[i]);

right.record_at(tw, num_samples,
[&] (Samples_ptr const &samples) {
for (unsigned i = 0; i < SAMPLES_PER_PERIOD; i++)
data[i*CHANNELS + 1] = float_to_s16(samples.start[i]);
});
},
[&] { depleted = true; }
);

if (orig_depleted != depleted && depleted)
log("recording depleted");
}
};

Recording _recording { };

Record_play_aggregator(Env &env) : _env(env) { }

unsigned _count = 0;

Packet play_packet() override
{
_recording.from_record_sessions(_left, _right);

return { _recording.data, sizeof(Recording::data) };
}

void record_packet(Packet packet) override
{
(void)packet;
}
};


Audio::Session &Audio::Session::construct(Env &env, Allocator &alloc)
{
static Audio_aggregator _audio { env, alloc };
bool const use_record_play_interface =
Attached_rom_dataspace(env, "config").xml().attribute_value("record_play", false);

if (!use_record_play_interface) {
static Audio_aggregator _audio { env, alloc };
return _audio;
}

static Record_play_aggregator _audio { env };
return _audio;
}

0 comments on commit f108232

Please sign in to comment.