Products > Programming

Ubuntu C-language sound peripheral programming - simple C example to read/write?

<< < (4/5) > >>

ve7xen:

--- Quote from: wb0gaz on January 24, 2022, 06:44:49 pm ---My question is this - should I remove pulse audio (sudo apt remove "something") then install jackd (sudo apt install jackd?)? To narrow down the OS environment - Ubuntu 20.04.3 LTS running on 64-bit X86 type machine; the OS install has nothing beyond whatever constitutes a "minimum install" plus build-essential.
--- End quote ---

If you remove Pulse from an Ubuntu system, you will basically break audio entirely, all the system audio depends on it. I don't recommend doing that unless you want to go down that particular unprodcutive rabbit hole, or if this will be a machine dedicated to live audio processing and you don't care about normal system audio.

If you want to use JACK, I suggest you just stop Pulse while you are using it. I don't recall off the top of my head how Ubuntu starts PA, but `systemctl --user stop pulseaudio.service pulseaudio.socket` is probably sufficient to control it manually. You could also look into using `pasuspender` to automatically suspend PA while running jackd and restart it when you are done, I'm not sure if this tool is included in Ubuntu or not.

I'd suggest starting with the simplest case, which would be either PulseAudio or ALSA (or your original pipeline idea), and if you find you need/want better realtime performance, you can add JACK later. The complication will just make development more cumbersome, IMO.

PKTKS:
yeah.. the POTTERIX  thing is made to put everything under that..
It is a whole crippled thing from ground to top

You can not remove systemd..
You can not just use the plain ALSA like it should..
You can not just put JACK to run with all those things running...

Change your system.. and put proper things in place..

My nephew loves POTTERIX.. I like to see him happy..
But no chance in hell I will use that ...

The JACK code contains the best examples available..


--- Code: ---/** @file simple_client.c
 *
 * @brief This simple client demonstrates the basic features of JACK
 * as they would be used by many applications.
 */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <jack/jack.h>

jack_port_t *output_port1, *output_port2;
jack_client_t *client;

#ifndef M_PI
#define M_PI  (3.14159265)
#endif

#define TABLE_SIZE   (200)
typedef struct
{
float sine[TABLE_SIZE];
int left_phase;
int right_phase;
}
paTestData;

static void signal_handler(int sig)
{
jack_client_close(client);
fprintf(stderr, "signal received, exiting ...\n");
exit(0);
}

/**
 * The process callback for this JACK application is called in a
 * special realtime thread once for each audio cycle.
 *
 * This client follows a simple rule: when the JACK transport is
 * running, copy the input port to the output.  When it stops, exit.
 */

int
process (jack_nframes_t nframes, void *arg)
{
jack_default_audio_sample_t *out1, *out2;
paTestData *data = (paTestData*)arg;
int i;

out1 = (jack_default_audio_sample_t*)jack_port_get_buffer (output_port1, nframes);
out2 = (jack_default_audio_sample_t*)jack_port_get_buffer (output_port2, nframes);

for( i=0; i<nframes; i++ )
{
out1[i] = data->sine[data->left_phase];  /* left */
out2[i] = data->sine[data->right_phase];  /* right */
data->left_phase += 1;
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
}
   
return 0;     
}

/**
 * JACK calls this shutdown_callback if the server ever shuts down or
 * decides to disconnect the client.
 */
void
jack_shutdown (void *arg)
{
exit (1);
}

int
main (int argc, char *argv[])
{
const char **ports;
const char *client_name;
const char *server_name = NULL;
jack_options_t options = JackNullOption;
jack_status_t status;
paTestData data;
int i;

if (argc >= 2) { /* client name specified? */
client_name = argv[1];
if (argc >= 3) { /* server name specified? */
server_name = argv[2];
int my_option = JackNullOption | JackServerName;
options = (jack_options_t)my_option;
}
} else { /* use basename of argv[0] */
client_name = strrchr(argv[0], '/');
if (client_name == 0) {
client_name = argv[0];
} else {
client_name++;
}
}

for( i=0; i<TABLE_SIZE; i++ )
{
data.sine[i] = 0.2 * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
}
data.left_phase = data.right_phase = 0;
 

/* open a client connection to the JACK server */

client = jack_client_open (client_name, options, &status, server_name);
if (client == NULL) {
fprintf (stderr, "jack_client_open() failed, "
"status = 0x%2.0x\n", status);
if (status & JackServerFailed) {
fprintf (stderr, "Unable to connect to JACK server\n");
}
exit (1);
}
if (status & JackServerStarted) {
fprintf (stderr, "JACK server started\n");
}
if (status & JackNameNotUnique) {
client_name = jack_get_client_name(client);
fprintf (stderr, "unique name `%s' assigned\n", client_name);
}

/* tell the JACK server to call `process()' whenever
   there is work to be done.
*/

jack_set_process_callback (client, process, &data);

/* tell the JACK server to call `jack_shutdown()' if
   it ever shuts down, either entirely, or if it
   just decides to stop calling us.
*/

jack_on_shutdown (client, jack_shutdown, 0);

/* create two ports */

output_port1 = jack_port_register (client, "output1",
  JACK_DEFAULT_AUDIO_TYPE,
  JackPortIsOutput, 0);

output_port2 = jack_port_register (client, "output2",
  JACK_DEFAULT_AUDIO_TYPE,
  JackPortIsOutput, 0);

if ((output_port1 == NULL) || (output_port2 == NULL)) {
fprintf(stderr, "no more JACK ports available\n");
exit (1);
}

/* Tell the JACK server that we are ready to roll.  Our
* process() callback will start running now. */

if (jack_activate (client)) {
fprintf (stderr, "cannot activate client");
exit (1);
}

/* Connect the ports.  You can't do this before the client is
* activated, because we can't make connections to clients
* that aren't running.  Note the confusing (but necessary)
* orientation of the driver backend ports: playback ports are
* "input" to the backend, and capture ports are "output" from
* it.
*/
 
ports = jack_get_ports (client, NULL, NULL,
JackPortIsPhysical|JackPortIsInput);
if (ports == NULL) {
fprintf(stderr, "no physical playback ports\n");
exit (1);
}

if (jack_connect (client, jack_port_name (output_port1), ports[0])) {
fprintf (stderr, "cannot connect output ports\n");
}

if (jack_connect (client, jack_port_name (output_port2), ports[1])) {
fprintf (stderr, "cannot connect output ports\n");
}

jack_free (ports);
   
    /* install a signal handler to properly quits jack client */
#ifdef WIN32
signal(SIGINT, signal_handler);
signal(SIGABRT, signal_handler);
signal(SIGTERM, signal_handler);
#else
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
signal(SIGINT, signal_handler);
#endif

/* keep running until the Ctrl+C */

while (1) {
#ifdef WIN32
Sleep(1000);
#else
sleep (1);
#endif
}

jack_client_close (client);
exit (0);
}


--- End code ---


API  at  http://jackaudio.org

It is just another level of things..
BUNTUs POTTERIX and today RedHat thingys are NICHES..

If you want serious things... just move on..

Paul

ve7xen:

--- Quote from: PKTKS on January 24, 2022, 09:04:49 pm ---yeah.. the POTTERIX  thing is made to put everything under that..
--- End quote ---

You mean just like how running jackd will take exclusive control of the audio device? Any audio server will do this, because that's how the kernel's hardware API works. |O

Can you please take your misplaced ideological opposition to Pulse and Systemd elsewhere? It is not helpful to OP to have you suggesting the most convoluted path for their task, just because you don't like the way that modern Linux does things.

PulseAudio is the default audio framework for Linux machines today. Deal with it.


--- Quote ---You can not just use the plain ALSA like it should..
--- End quote ---

You can, the ALSA API will work just fine on a (properly set up) PulseAudio system. So use that if you want, but since it's a wrapper, it doesn't really offer any advantages.


--- Quote ---You can not just put JACK to run with all those things running...

--- End quote ---

You can use JACK as a backend for PulseAudio if you want. But this is just adding complication and for 99% of users has no benefit and several drawbacks. And of course, requires configuration and setup that is not helping OP achieve their rather simple goal of audio I/O on their existing system.


--- Quote ---BUNTUs POTTERIX and today RedHat thingys are NICHES..

--- End quote ---
|O No. Any alternative is a niche. Mainstream Linux is systemd and PulseAudio, period.

PKTKS:

--- Quote from: ve7xen on January 24, 2022, 09:25:29 pm ---
No. Any alternative is a niche. Mainstream Linux is systemd and PulseAudio, period.

--- End quote ---

I have seen this slightly different..
Like

No more good alternatives.
Mainstream POTTERIX is the thing...

And MS is no longer that mainstream thingo

Paul

Bassman59:

--- Quote from: PKTKS on January 25, 2022, 08:52:11 am ---No more good alternatives.
Mainstream POTTERIX is the thing...

--- End quote ---

You keep using that word.

Nobody knows what the fuck you're on about.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

There was an error while thanking
Thanking...
Go to full version