PulseAudio Cat

This commit is contained in:
2024-08-14 15:19:50 -04:00
parent b70638479b
commit 45e3ea1a5e
7 changed files with 105 additions and 6 deletions

View File

@@ -48,3 +48,6 @@ if(WIN32)
endif()
target_link_libraries(ReMixer-Test PUBLIC ReMixer)
add_executable(pacatsimple pacat_simple.cpp)
target_link_libraries(pacatsimple pulse-simple pulse)

BIN
cmake-build-debug/song.ogg Normal file

Binary file not shown.

View File

@@ -32,7 +32,7 @@ public:
class SFX : public Sound {
public:
SFX() = default;
SFX(const std::string& sfx_file_path)
SFX(const std::string& sfx_file_path);
};
/// Longer (>10 second) audio via WAV format.

View File

@@ -17,6 +17,8 @@
#include <iostream>
#include <fstream>
void decodeVorbis(const char* inputFile, const char* outputFile)
{
OggVorbis_File vf;
@@ -54,7 +56,7 @@ void decodeVorbis(const char* inputFile, const char* outputFile)
}
[[noreturn]] int main() {
const char* inputFile = "click.ogg";
const char* inputFile = "song.ogg";
const char* outputFile = "output.raw";
decodeVorbis(inputFile, outputFile);
@@ -67,10 +69,12 @@ void decodeVorbis(const char* inputFile, const char* outputFile)
if (!rawData) {
std::cerr << "Error opening input file." << std::endl; return -1;
}
signed char raw[4096];
fread(raw, sizeof(signed char), stream.bufferSize, rawData);
signed char raw[65535];
int bytes_read = fread(raw, sizeof(signed char), 65535, rawData);
std::cout << "Read " << bytes_read << " bytes..." << std::endl;
int* returned_err;
stream.test_play(raw, stream.bufferSize, returned_err);
stream.test_play(raw, 65535, returned_err);
std::cout << "Something happened!!" << std::endl;
bool keep_runnin = true;

89
pacat_simple.cpp Normal file
View File

@@ -0,0 +1,89 @@
extern "C"
{
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 1024
int main(int argc, char* argv[])
{
static const pa_sample_spec ss = {
.format = PA_SAMPLE_S16LE,
.rate = 44100,
.channels = 2
};
pa_simple *s = NULL;
int ret = 1;
int error;
/* Replace STDIN with the specified file if needed. */
if (argc > 1) {
int fd;
if ((fd = open(argv[1], O_RDONLY)) < 0) {
fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno));
goto finish;
}
if (dup2(fd, STDIN_FILENO) < 0) {
fprintf(stderr, __FILE__": dup2() failed: %s\n", strerror(errno));
goto finish;
}
close(fd);
}
/* Create a new playback stream */
if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
goto finish;
}
for (;;) {
uint8_t buf[BUFSIZE];
ssize_t r;
#if 0
pa_usec_t latency;
if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t)-1)
{
fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
return;
}
fprintf(stderr, "%0.0f usec \r", (float)latency);
#endif
/* Read some data */
if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) {
if (r == 0) /* EOF */
break;
fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
goto finish;
}
/* and play it */
if (pa_simple_write(s, buf, (size_t)r, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
goto finish;
}
}
/* Make sure that every single sample was played */
if (pa_simple_drain(s, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
goto finish;
}
ret = 0;
finish:
if (s)
pa_simple_free(s);
return ret;
}
}

BIN
song18.mp3 Normal file

Binary file not shown.

View File

@@ -3,6 +3,9 @@
#include <uuid/uuid.h>
#include <iostream>
Stream::Stream(const std::string& name, const std::string& stream_name, SampleRate sample_rate, StreamMode mode) {
this->application_name = name;
this->stream_name = stream_name;
@@ -33,7 +36,7 @@ void Stream::test_play(const void* data, size_t size, int* err)
int return_code = pa_simple_write(stream, data, size, err);
if (return_code != 0) // success
std::cout << "Some Problem" << std::endl;
std::cerr << "A problem occurred pushing sound data to the buffer. Err code: " << return_code << std::endl;
}
unsigned int Stream::numChannels() {