Aux input test code.

This commit is contained in:
Robin E. R. Davies
2025-05-12 11:22:03 -04:00
parent 621443d19c
commit 21e4b2864c
5 changed files with 260 additions and 1 deletions
+188
View File
@@ -0,0 +1,188 @@
#include "AuxIn.hpp"
#include <stdexcept>
#include <thread>
#include "ss.hpp"
#include "Finally.hpp"
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <iostream>
#include <atomic>
#include <poll.h>
#include <sys/types.h>
namespace fs = std::filesystem;
namespace pipedal
{
class AuxInAlsaDeviceImpl : public AuxInAlsaDevice
{
public:
AuxInAlsaDeviceImpl(const std::filesystem::path &path);
virtual ~AuxInAlsaDeviceImpl() override;
void Open(const fs::path &path);
void Close();
private:
fs::path path;
void ThreadProc();
std::unique_ptr<std::thread> thread;
int fd = -1;
int cancelWrite = -1;
int cancelRead = -1;
size_t total_read = 0;
};
AuxInAlsaDevice::ptr AuxInAlsaDevice::Create(const std::filesystem::path &path)
{
return std::make_shared<AuxInAlsaDeviceImpl>(path);
}
}
using namespace pipedal;
AuxInAlsaDeviceImpl::AuxInAlsaDeviceImpl(const fs::path &path)
{
this->path = path;
Open(path);
this->thread = std::make_unique<std::thread>([this]()
{ ThreadProc(); });
}
void AuxInAlsaDeviceImpl::Close()
{
if (thread)
{
char buff[1];
int nWritten = write(cancelWrite, buff, 1);
(void)nWritten; // ignore return.
thread->join();
thread = nullptr;
}
if (cancelWrite != -1)
{
close(cancelWrite);
cancelWrite = 1;
}
if (cancelRead != -1)
{
close(cancelRead);
cancelRead = -1;
}
if (fd != -1)
{
close(fd);
fd = -1;
}
}
AuxInAlsaDeviceImpl::~AuxInAlsaDeviceImpl()
{
Close();
}
AuxInAlsaDevice::AuxInAlsaDevice()
{
}
AuxInAlsaDevice::~AuxInAlsaDevice()
{
}
static void setNonBlocking(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
static void setBlocking(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags & (~O_NONBLOCK));
}
void AuxInAlsaDeviceImpl::Open(const fs::path &path)
{
total_read = 0;
fd = open(path.c_str(), O_RDONLY | O_NONBLOCK); // non-blocking so we can open it
if (fd == -1)
{
throw std::runtime_error(SS("Can't open file " << path));
}
setBlocking(fd); // but use blocking semantics when reading.
int cancel_pipe[2];
// create a socket pair to use when cancelling.
if (pipe(cancel_pipe) == -1)
{
throw std::runtime_error("Can't create pipe.");
}
this->cancelWrite = cancel_pipe[1];
this->cancelRead = cancel_pipe[0];
setNonBlocking(cancelRead);
}
void AuxInAlsaDeviceImpl::ThreadProc()
{
constexpr size_t BUFFER_SIZE = 65536;
ssize_t bytes_read;
int16_t samples[BUFFER_SIZE];
while (1)
{
// Read audio data from the FIFO
struct pollfd poll_fds[2];
poll_fds[0].fd = fd;
poll_fds[0].events = POLLIN;
poll_fds[1].fd = cancelRead;
poll_fds[1].events = POLLIN;
int rc = poll(poll_fds, 2, -1);
if (rc < 00)
{
throw std::runtime_error("select failed.");
}
if (poll_fds[1].revents)
{
break;
}
if (poll_fds[0].revents)
{
ssize_t bytes_read = read(fd, &samples, sizeof(samples));
if (bytes_read < 0)
{
throw std::runtime_error("Auxin read failed.");
}
if (poll_fds[0].revents & POLLHUP)
{
std::cout << " End of stream. " << total_read << std::endl;
close(fd);
fd = -1;
// reopen the fifo.
total_read = 0;
fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
if (fd == -1)
{
throw std::runtime_error(SS("Can't open file " << path));
}
setBlocking(fd);
}
else if (bytes_read > 0)
{
total_read += bytes_read;
std::cout << " Read " << bytes_read << std::endl;
sleep(1);
// Process the audio data, mix it with your main input
// Your mixing code here...
// For example, if main_buffer has your main input:
// for (int i = 0; i < bytes_read/sizeof(int16_t); i++) {
// mixed_buffer[i] = main_buffer[i] + buffer[i];
// }
}
}
}
}
+38
View File
@@ -0,0 +1,38 @@
// Copyright (c) 2025 Robin Davies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once
#include <memory>
#include <filesystem>
namespace pipedal {
class AuxInAlsaDevice {
protected:
AuxInAlsaDevice();
virtual ~AuxInAlsaDevice();
public:
using self=AuxInAlsaDevice;
using ptr = std::shared_ptr<self>;
static ptr Create(const std::filesystem::path&path);
};
}
+28
View File
@@ -0,0 +1,28 @@
// Copyright (c) 2025 Robin Davies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "AuxIn.hpp"
using namespace pipedal;
int main(int argc, char**argv) {
AuxInAlsaDevice::ptr auxIn = AuxInAlsaDevice::Create("/var/pipedal/pipedal_aux_input_fifo");
sleep(10000);
}
+5
View File
@@ -376,6 +376,11 @@ target_link_libraries(hotspotManagerTest PRIVATE ${PIPEDAL_LIBS})
set_target_properties(hotspotManagerTest PROPERTIES EXCLUDE_FROM_ALL true)
add_executable(AuxInTest
AuxIn.hpp
AuxIn.cpp
AuxInTest.cpp
)
add_executable(pipedaltest
testMain.cpp