0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-27 03:23:54 +01:00

add alsa output to pstcode, scamod

This commit is contained in:
2025-01-25 19:58:14 +01:00
parent efd83adf39
commit 634ba05d61
7 changed files with 89 additions and 448 deletions

View File

@@ -1,5 +1,5 @@
{ {
"port": 13452, "port": 13452,
"time": 1737738374932, "time": 1737831115102,
"version": "0.0.3" "version": "0.0.3"
} }

View File

@@ -1,197 +0,0 @@
#include <stdio.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include "../lib/constants.h"
#include "../lib/oscillator.h"
#include "../lib/filters.h"
#include "options.h"
#define SAMPLE_RATE 192000 // Don't go lower than 108 KHz, becuase it (53000*2) and (38000+15000)
#define INPUT_DEVICE "real_real_tx_audio_input.monitor"
#define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback"
#define BUFFER_SIZE 512
#define CLIPPER_THRESHOLD 0.525 // Adjust this as needed
#define MONO_VOLUME 0.5f // L+R Signal
#define STEREO_VOLUME_AUDIO 1.0f // L-R signal
#define STEREO_VOLUME_MODULATION 0.5f // L-R signal
#ifdef PREEMPHASIS
#define PREEMPHASIS_TAU 0.00005 // 50 microseconds, use 0.000075 if in america
#endif
#ifdef LPF
#define LPF_CUTOFF 15000
#endif
volatile sig_atomic_t to_run = 1;
float clip(float sample) {
if (sample > CLIPPER_THRESHOLD) {
return CLIPPER_THRESHOLD; // Clip to the upper threshold
} else if (sample < -CLIPPER_THRESHOLD) {
return -CLIPPER_THRESHOLD; // Clip to the lower threshold
} else {
return sample; // No clipping
}
}
void uninterleave(const float *input, float *left, float *right, size_t num_samples) {
// For stereo, usually it is like this: LEFT RIGHT LEFT RIGHT LEFT RIGHT so this is used to get LEFT LEFT LEFT and RIGHT RIGHT RIGHT
for (size_t i = 0; i < num_samples/2; i++) {
left[i] = input[i * 2];
right[i] = input[i * 2 + 1];
}
}
static void stop(int signum) {
(void)signum;
printf("\nReceived stop signal. Cleaning up...\n");
to_run = 0;
}
int main() {
printf("CrosbySTCode : Stereo encoder (using the crosby system) made by radio95 (with help of ChatGPT and Claude, thanks!)\n");
// Define formats and buffer atributes
pa_sample_spec stereo_format = {
.format = PA_SAMPLE_FLOAT32NE, //Float32 NE, or Float32 Native Endian, the float in c uses the endianess of your pc, or native endian, and float is float32, and double is float64
.channels = 2,
.rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better
};
pa_sample_spec mono_format = {
.format = PA_SAMPLE_FLOAT32NE,
.channels = 1,
.rate = SAMPLE_RATE
};
pa_buffer_attr input_buffer_atr = {
.maxlength = buffer_maxlength,
.fragsize = buffer_tlength_fragsize
};
pa_buffer_attr output_buffer_atr = {
.maxlength = buffer_maxlength,
.tlength = buffer_tlength_fragsize,
.prebuf = buffer_prebuf
};
printf("Connecting to input device... (%s)\n", INPUT_DEVICE);
pa_simple *input_device = pa_simple_new(
NULL,
"CrosbyStereoEncoder",
PA_STREAM_RECORD,
INPUT_DEVICE,
"Audio Input",
&stereo_format,
NULL,
&input_buffer_atr,
NULL
);
if (!input_device) {
fprintf(stderr, "Error: cannot open input device.\n");
return 1;
}
printf("Connecting to output device... (%s)\n", OUTPUT_DEVICE);
pa_simple *output_device = pa_simple_new(
NULL,
"CrosbyStereoEncoder",
PA_STREAM_PLAYBACK,
OUTPUT_DEVICE,
"MPX",
&mono_format,
NULL,
&output_buffer_atr,
NULL
);
if (!output_device) {
fprintf(stderr, "Error: cannot open output device.\n");
pa_simple_free(input_device);
return 1;
}
Oscillator osc;
init_oscillator(&osc, 50000.0, SAMPLE_RATE);
#ifdef PREEMPHASIS
ResistorCapacitor preemp_l, preemp_r;
init_rc_tau(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
init_rc_tau(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
#endif
#ifdef LPF
ResistorCapacitor lpf_l, lpf_r;
init_low_pass_filter(&lpf_l, LPF_CUTOFF, SAMPLE_RATE);
init_low_pass_filter(&lpf_r, LPF_CUTOFF, SAMPLE_RATE);
#endif
signal(SIGINT, stop);
signal(SIGTERM, stop);
int pulse_error;
float input[BUFFER_SIZE*2]; // Input from device, interleaved stereo
float left[BUFFER_SIZE+64], right[BUFFER_SIZE+64]; // Audio, same thing as in input but ininterleaved, ai told be there could be a buffer overflow here
float mpx[BUFFER_SIZE]; // MPX, this goes to the output
while (to_run) {
if (pa_simple_read(input_device, input, sizeof(input), &pulse_error) < 0) {
fprintf(stderr, "Error reading from input device: %s\n", pa_strerror(pulse_error));
to_run = 0;
break;
}
uninterleave(input, left, right, BUFFER_SIZE*2);
for (int i = 0; i < BUFFER_SIZE; i++) {
float l_in = left[i];
float r_in = right[i];
#ifdef PREEMPHASIS
#ifdef LPF
float lowpassed_left = apply_low_pass_filter(&lpf_l, l_in);
float lowpassed_right = apply_low_pass_filter(&lpf_r, r_in);
float preemphasized_left = apply_pre_emphasis(&preemp_l, lowpassed_left);
float preemphasized_right = apply_pre_emphasis(&preemp_r, lowpassed_right);
float current_left_input = clip(preemphasized_left);
float current_right_input = clip(preemphasized_right);
#else
float preemphasized_left = apply_pre_emphasis(&preemp_l, l_in);
float preemphasized_right = apply_pre_emphasis(&preemp_r, r_in);
float current_left_input = clip(preemphasized_left);
float current_right_input = clip(preemphasized_right);
#endif
#else
#ifdef LPF
float lowpassed_left = apply_low_pass_filter(&lpf_l, l_in);
float lowpassed_right = apply_low_pass_filter(&lpf_r, r_in);
float current_left_input = clip(lowpassed_left);
float current_right_input = clip(lowpassed_right);
#else
float current_left_input = clip(l_in);
float current_right_input = clip(r_in);
#endif
#endif
float mono = (current_left_input + current_right_input) / 2.0f; // Stereo to Mono
float stereo = (current_left_input - current_right_input) / 2.0f; // Also Stereo to Mono but a bit diffrent
change_oscillator_frequency(&osc, (50000+((stereo*STEREO_VOLUME_AUDIO)*15000)));
mpx[i] = mono * MONO_VOLUME +
get_oscillator_sin_sample(&osc) * STEREO_VOLUME_MODULATION;
}
if (pa_simple_write(output_device, mpx, sizeof(mpx), &pulse_error) < 0) {
fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error));
to_run = 0;
break;
}
}
printf("Cleaning up...\n");
pa_simple_free(input_device);
pa_simple_free(output_device);
return 0;
}

View File

@@ -1,198 +0,0 @@
#include <stdio.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include "../lib/constants.h"
#include "../lib/oscillator.h"
#include "../lib/filters.h"
#include "../lib/fm_modulator.h"
#include "options.h"
#define SAMPLE_RATE 192000
#define INPUT_DEVICE "SCA.monitor"
#define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback"
#define BUFFER_SIZE 512
#define CLIPPER_THRESHOLD 0.75 // Adjust this as needed, this also limits deviation, so if you set this to 0.5 then the deviation will be limited to half
#define MONO_VOLUME 0.075f // Mono Volume
#define STEREO_VOLUME 0.025f // Stereo Volume
#ifdef PREEMPHASIS
#define PREEMPHASIS_TAU 0.00005 // 50 microseconds, use 0.000075 if in america
#endif
#ifdef LPF
#define LPF_CUTOFF 8000
#endif
volatile sig_atomic_t to_run = 1;
void uninterleave(const float *input, float *left, float *right, size_t num_samples) {
// For stereo, usually it is like this: LEFT RIGHT LEFT RIGHT LEFT RIGHT so this is used to get LEFT LEFT LEFT and RIGHT RIGHT RIGHT
for (size_t i = 0; i < num_samples/2; i++) {
left[i] = input[i * 2];
right[i] = input[i * 2 + 1];
}
}
float clip(float sample) {
if (sample > CLIPPER_THRESHOLD) {
return CLIPPER_THRESHOLD; // Clip to the upper threshold
} else if (sample < -CLIPPER_THRESHOLD) {
return -CLIPPER_THRESHOLD; // Clip to the lower threshold
} else {
return sample; // No clipping
}
}
static void stop(int signum) {
(void)signum;
printf("\nReceived stop signal. Cleaning up...\n");
to_run = 0;
}
int main() {
printf("StereoSCAMod : Stereo SCA Modulator (based on the SCA encoder SCAMod) made by radio95 (with help of ChatGPT and Claude, thanks!)\n");
// Define formats and buffer atributes
pa_sample_spec mono_audio_format = {
.format = PA_SAMPLE_FLOAT32LE,
.channels = 1,
.rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better
};
pa_sample_spec stereo_audio_format = {
.format = PA_SAMPLE_FLOAT32LE,
.channels = 2,
.rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better
};
pa_buffer_attr input_buffer_atr = {
.maxlength = buffer_maxlength,
.fragsize = buffer_tlength_fragsize
};
pa_buffer_attr output_buffer_atr = {
.maxlength = buffer_maxlength,
.tlength = buffer_tlength_fragsize,
.prebuf = buffer_prebuf
};
printf("Connecting to input device... (%s)\n", INPUT_DEVICE);
pa_simple *input_device = pa_simple_new(
NULL,
"StereoSCAMod",
PA_STREAM_RECORD,
INPUT_DEVICE,
"Audio Input",
&stereo_audio_format,
NULL,
&input_buffer_atr,
NULL
);
if (!input_device) {
fprintf(stderr, "Error: cannot open input device.\n");
return 1;
}
printf("Connecting to output device... (%s)\n", OUTPUT_DEVICE);
pa_simple *output_device = pa_simple_new(
NULL,
"StereoSCAMod",
PA_STREAM_PLAYBACK,
OUTPUT_DEVICE,
"Signal",
&mono_audio_format,
NULL,
&output_buffer_atr,
NULL
);
if (!output_device) {
fprintf(stderr, "Error: cannot open output device.\n");
pa_simple_free(input_device);
return 1;
}
FMModulator mod_mono, mod_stereo;
init_fm_modulator(&mod_mono, 67000, 6000, SAMPLE_RATE);
init_fm_modulator(&mod_stereo, 80000, 6000, SAMPLE_RATE);
#ifdef PREEMPHASIS
ResistorCapacitor preemp_l, preemp_r;
init_rc_tau(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
init_rc_tau(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE);
#endif
#ifdef LPF
ResistorCapacitor lpf_l, lpf_r;
init_low_pass_filter(&lpf_l, LPF_CUTOFF, SAMPLE_RATE);
init_low_pass_filter(&lpf_r, LPF_CUTOFF, SAMPLE_RATE);
#endif
signal(SIGINT, stop);
signal(SIGTERM, stop);
int pulse_error;
float input[BUFFER_SIZE*2]; // Input from device
float left[BUFFER_SIZE+64], right[BUFFER_SIZE+64]; // Audio, same thing as in input but ininterleaved, ai told be there could be a buffer overflow here
float signal[BUFFER_SIZE]; // this goes to the output
while (to_run) {
if (pa_simple_read(input_device, input, sizeof(input), &pulse_error) < 0) {
fprintf(stderr, "Error reading from input device: %s\n", pa_strerror(pulse_error));
to_run = 0;
break;
}
uninterleave(input, left, right, BUFFER_SIZE*2);
for (int i = 0; i < BUFFER_SIZE; i++) {
float l_in = left[i];
float r_in = right[i];
#ifdef PREEMPHASIS
#ifdef LPF
float lowpassed_left = apply_low_pass_filter(&lpf_l, l_in);
float lowpassed_right = apply_low_pass_filter(&lpf_r, r_in);
float preemphasized_left = apply_pre_emphasis(&preemp_l, lowpassed_left);
float preemphasized_right = apply_pre_emphasis(&preemp_r, lowpassed_right);
float current_left_input = clip(preemphasized_left);
float current_right_input = clip(preemphasized_right);
#else
float preemphasized_left = apply_pre_emphasis(&preemp_l, l_in);
float preemphasized_right = apply_pre_emphasis(&preemp_r, r_in);
float current_left_input = clip(preemphasized_left);
float current_right_input = clip(preemphasized_right);
#endif
#else
#ifdef LPF
float lowpassed_left = apply_low_pass_filter(&lpf_l, l_in);
float lowpassed_right = apply_low_pass_filter(&lpf_r, r_in);
float current_left_input = clip(lowpassed_left);
float current_right_input = clip(lowpassed_right);
#else
float current_left_input = clip(l_in);
float current_right_input = clip(r_in);
#endif
#endif
float mono = (current_left_input+current_right_input)/2.0f;
float stereo = (current_left_input-current_right_input)/2.0f;
signal[i] = modulate_fm(&mod_mono, mono)*MONO_VOLUME+
modulate_fm(&mod_stereo, stereo)*STEREO_VOLUME;
}
if (pa_simple_write(output_device, signal, sizeof(signal), &pulse_error) < 0) {
fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error));
to_run = 0;
break;
}
}
printf("Cleaning up...\n");
pa_simple_free(input_device);
pa_simple_free(output_device);
return 0;
}

View File

@@ -17,6 +17,7 @@
#define INPUT_DEVICE "real_real_tx_audio_input.monitor" #define INPUT_DEVICE "real_real_tx_audio_input.monitor"
#define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback" #define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback"
// #define ALSA_OUTPUT // Output, not input or both
#define BUFFER_SIZE 512 #define BUFFER_SIZE 512
#define CLIPPER_THRESHOLD 0.525 // Adjust this as needed #define CLIPPER_THRESHOLD 0.525 // Adjust this as needed
@@ -75,11 +76,13 @@ int main() {
.maxlength = buffer_maxlength, .maxlength = buffer_maxlength,
.fragsize = buffer_tlength_fragsize .fragsize = buffer_tlength_fragsize
}; };
#ifndef ALSA_OUTPUT
pa_buffer_attr output_buffer_atr = { pa_buffer_attr output_buffer_atr = {
.maxlength = buffer_maxlength, .maxlength = buffer_maxlength,
.tlength = buffer_tlength_fragsize, .tlength = buffer_tlength_fragsize,
.prebuf = buffer_prebuf .prebuf = buffer_prebuf
}; };
#endif
printf("Connecting to input device... (%s)\n", INPUT_DEVICE); printf("Connecting to input device... (%s)\n", INPUT_DEVICE);
@@ -101,6 +104,7 @@ int main() {
printf("Connecting to output device... (%s)\n", OUTPUT_DEVICE); printf("Connecting to output device... (%s)\n", OUTPUT_DEVICE);
#ifndef ALSA_OUTPUT
pa_simple *output_device = pa_simple_new( pa_simple *output_device = pa_simple_new(
NULL, NULL,
"PolarStereoEncoder", "PolarStereoEncoder",
@@ -117,6 +121,33 @@ int main() {
pa_simple_free(input_device); pa_simple_free(input_device);
return 1; return 1;
} }
#else
snd_pcm_hw_params_t *output_params;
snd_pcm_t *output_handle;
int output_error = snd_pcm_open(&output_handle, OUTPUT_DEVICE, SND_PCM_STREAM_PLAYBACK, 0);
if(output_error < 0) {
fprintf(stderr, "Error: cannot open output device: %s\n", snd_strerror(output_error));
pa_simple_free(input_device);
return 1;
}
snd_pcm_hw_params_malloc(&output_params);
snd_pcm_hw_params_any(output_handle, output_params);
snd_pcm_hw_params_set_access(output_handle, output_params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(output_handle, output_params, SND_PCM_FORMAT_FLOAT); // Same as pulse's Float32NE
snd_pcm_hw_params_set_channels(output_handle, output_params, 1);
unsigned int rate = SAMPLE_RATE;
int dir;
snd_pcm_hw_params_set_rate_near(output_handle, output_params, &rate, &dir);
snd_pcm_uframes_t frames = BUFFER_SIZE;
snd_pcm_hw_params_set_period_size_near(output_handle, output_params, &frames, &dir); // i don't have a clue why like this
output_error = snd_pcm_hw_params(output_handle, output_params);
if(output_error < 0) {
fprintf(stderr, "Error: cannot open output device: %s\n", snd_strerror(output_error));
snd_pcm_close(output_handle);
pa_simple_free(input_device);
return 1;
}
#endif
Oscillator stereo_osc; Oscillator stereo_osc;
init_oscillator(&stereo_osc, 31250.0, SAMPLE_RATE); init_oscillator(&stereo_osc, 31250.0, SAMPLE_RATE);
@@ -186,14 +217,22 @@ int main() {
((stereo+0.2) * stereo_carrier)*STEREO_VOLUME; // the 0.2 add DC, you know what happens then? Carrier wave ((stereo+0.2) * stereo_carrier)*STEREO_VOLUME; // the 0.2 add DC, you know what happens then? Carrier wave
} }
#ifndef ALSA_OUTPUT
if (pa_simple_write(output_device, mpx, sizeof(mpx), &pulse_error) < 0) { if (pa_simple_write(output_device, mpx, sizeof(mpx), &pulse_error) < 0) {
fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error)); fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error));
to_run = 0; to_run = 0;
break; break;
} }
#else
snd_pcm_writei(output_handle, mpx, sizeof(mpx));
#endif
} }
printf("Cleaning up...\n"); printf("Cleaning up...\n");
pa_simple_free(input_device); pa_simple_free(input_device);
#ifndef ALSA_OUTPUT
pa_simple_free(output_device); pa_simple_free(output_device);
#else
snd_pcm_drain(output_handle);
snd_pcm_free(output_handle);
return 0; return 0;
} }

View File

@@ -1,6 +1,4 @@
#include <stdio.h> #include <stdio.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <stdint.h> #include <stdint.h>
@@ -18,9 +16,16 @@
#define INPUT_DEVICE "SCA.monitor" #define INPUT_DEVICE "SCA.monitor"
#define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback" #define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback"
// #define ALSA_OUTPUT // Output, not input or both
#define BUFFER_SIZE 512 #define BUFFER_SIZE 512
#define CLIPPER_THRESHOLD 1 // Adjust this as needed, this also limits deviation, so if you set this to 0.5 then the deviation will be limited to half #define CLIPPER_THRESHOLD 1 // Adjust this as needed, this also limits deviation, so if you set this to 0.5 then the deviation will be limited to half
#include <pulse/simple.h>
#include <pulse/error.h>
#ifdef ALSA_OUTPUT
#include <alsa/asoundlib.h>
#endif
#define VOLUME 0.1f // SCA Volume #define VOLUME 0.1f // SCA Volume
#define VOLUME_AUDIO 1.0f // SCA Audio volume #define VOLUME_AUDIO 1.0f // SCA Audio volume
#define FREQUENCY 67000 // SCA Frequency #define FREQUENCY 67000 // SCA Frequency
@@ -66,11 +71,13 @@ int main() {
.maxlength = buffer_maxlength, .maxlength = buffer_maxlength,
.fragsize = buffer_tlength_fragsize .fragsize = buffer_tlength_fragsize
}; };
#ifndef ALSA_OUTPUT
pa_buffer_attr output_buffer_atr = { pa_buffer_attr output_buffer_atr = {
.maxlength = buffer_maxlength, .maxlength = buffer_maxlength,
.tlength = buffer_tlength_fragsize, .tlength = buffer_tlength_fragsize,
.prebuf = buffer_prebuf .prebuf = buffer_prebuf
}; };
#endif
printf("Connecting to input device... (%s)\n", INPUT_DEVICE); printf("Connecting to input device... (%s)\n", INPUT_DEVICE);
@@ -91,7 +98,7 @@ int main() {
} }
printf("Connecting to output device... (%s)\n", OUTPUT_DEVICE); printf("Connecting to output device... (%s)\n", OUTPUT_DEVICE);
#ifndef ALSA_OUTPUT
pa_simple *output_device = pa_simple_new( pa_simple *output_device = pa_simple_new(
NULL, NULL,
"SCAMod", "SCAMod",
@@ -108,6 +115,33 @@ int main() {
pa_simple_free(input_device); pa_simple_free(input_device);
return 1; return 1;
} }
#else
snd_pcm_hw_params_t *output_params;
snd_pcm_t *output_handle;
int output_error = snd_pcm_open(&output_handle, OUTPUT_DEVICE, SND_PCM_STREAM_PLAYBACK, 0);
if(output_error < 0) {
fprintf(stderr, "Error: cannot open output device: %s\n", snd_strerror(output_error));
pa_simple_free(input_device);
return 1;
}
snd_pcm_hw_params_malloc(&output_params);
snd_pcm_hw_params_any(output_handle, output_params);
snd_pcm_hw_params_set_access(output_handle, output_params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(output_handle, output_params, SND_PCM_FORMAT_FLOAT); // Same as pulse's Float32NE
snd_pcm_hw_params_set_channels(output_handle, output_params, 1);
unsigned int rate = SAMPLE_RATE;
int dir;
snd_pcm_hw_params_set_rate_near(output_handle, output_params, &rate, &dir);
snd_pcm_uframes_t frames = BUFFER_SIZE;
snd_pcm_hw_params_set_period_size_near(output_handle, output_params, &frames, &dir); // i don't have a clue why like this
output_error = snd_pcm_hw_params(output_handle, output_params);
if(output_error < 0) {
fprintf(stderr, "Error: cannot open output device: %s\n", snd_strerror(output_error));
snd_pcm_close(output_handle);
pa_simple_free(input_device);
return 1;
}
#endif
FMModulator mod; FMModulator mod;
init_fm_modulator(&mod, FREQUENCY, DEVIATION, SAMPLE_RATE); init_fm_modulator(&mod, FREQUENCY, DEVIATION, SAMPLE_RATE);
@@ -157,14 +191,23 @@ int main() {
signal[i] = modulate_fm(&mod, current_input)*VOLUME; signal[i] = modulate_fm(&mod, current_input)*VOLUME;
} }
#ifndef ALSA_OUTPUT
if (pa_simple_write(output_device, signal, sizeof(signal), &pulse_error) < 0) { if (pa_simple_write(output_device, signal, sizeof(signal), &pulse_error) < 0) {
fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error)); fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error));
to_run = 0; to_run = 0;
break; break;
} }
#else
snd_pcm_writei(output_handle, signal, sizeof(signal));
#endif
} }
printf("Cleaning up...\n"); printf("Cleaning up...\n");
pa_simple_free(input_device); pa_simple_free(input_device);
#ifndef ALSA_OUTPUT
pa_simple_free(output_device); pa_simple_free(output_device);
#else
snd_pcm_drain(output_handle);
snd_pcm_free(output_handle);
#endif
return 0; return 0;
} }

View File

@@ -23,7 +23,7 @@
#define INPUT_DEVICE "real_real_tx_audio_input.monitor" #define INPUT_DEVICE "real_real_tx_audio_input.monitor"
#define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback" #define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback"
#define ALSA_OUTPUT // Output, not input or both // #define ALSA_OUTPUT // Output, not input or both
#define BUFFER_SIZE 512 #define BUFFER_SIZE 512
#define CLIPPER_THRESHOLD 0.525 // Adjust this as needed #define CLIPPER_THRESHOLD 0.525 // Adjust this as needed
@@ -89,11 +89,13 @@ int main() {
.maxlength = buffer_maxlength, .maxlength = buffer_maxlength,
.fragsize = buffer_tlength_fragsize .fragsize = buffer_tlength_fragsize
}; };
#ifndef ALSA_OUTPUT
pa_buffer_attr output_buffer_atr = { pa_buffer_attr output_buffer_atr = {
.maxlength = buffer_maxlength, .maxlength = buffer_maxlength,
.tlength = buffer_tlength_fragsize, .tlength = buffer_tlength_fragsize,
.prebuf = buffer_prebuf .prebuf = buffer_prebuf
}; };
#endif
int open_pulse_error; int open_pulse_error;

View File

@@ -1,48 +0,0 @@
// This will encode a black and white TV signal using a luminance value, how does it work?
/*
It encodes the luminance into negative values, so totally white pixel should output -1, a black one should be 0
Every new line it sends a 0.5, every frame it is a 1.0
*/
#include "../lib/fm_modulator.h"
unsigned int rgb_to_luminance(unsigned int r, unsigned int g, unsigned int b) {
return (unsigned int)(0.299 * r + 0.587 * g + 0.114 * b);
}
typedef struct {
int line;
int pixel;
int lines;
int pixels;
} TVEncoder;
void init_tv_modulator(TVEncoder* tv, int lines, int pixels) {
tv->pixels = pixels;
tv->lines = lines;
tv->line = 0;
tv->pixel = 0;
}
float tv_encode(TVEncoder* tv, float luminance) {
float normalized_luminance = luminance / 255.0f; // Normalize luminance to [0, 1]
if (tv->line < tv->lines) {
if (tv->pixel < tv->pixels) {
// Process pixel within the current line
tv->pixel++;
return -normalized_luminance;
} else {
// End of line: reset pixel counter and move to the next line
tv->pixel = 0;
tv->line++;
return 0.5f;
}
} else {
// End of frame: reset frame counters
tv->line = 0;
tv->pixel = 0;
return 1.0f;
}
}