sending raw data to soundcard with c code in ubuntu
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I"m trying to write raw data i get externally from serial port to the sound card device using c code in ubuntu in order to play it.
I"ve looked everywhere for a solution and found that it can be done via ALSA API, but couldn't fund any clear example on how should i do it.
I also tried using the ioctl() function - but it doesn't play.
#include <stdio.h>
#include <fcntl.h> // File Control Definitions
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
int flg = 0, rdlen = 0;
int com_ptr, sound_ptr;
char input_data[16];
char output_data[16];
sem_t data_mutex;
void* read_data(void *arg)
{
int i, j;
for (j = 0 ; j < 5 ; j++)
{
while(flg == 1);//waits for the data to be read
sem_wait(&data_mutex);
printf("ndata_read_mutex MUTEX TAKENn");
printf("readn");
rdlen = read(com_ptr, input_data, sizeof(input_data));
//fwrite(buf, 1, rdlen, log_file);
printf("input length : %dn",rdlen);
for(i = 0 ; i < rdlen - 1; i++)
{
//input_data[i] = data[i];
printf("Reading.... : %cn", input_data[i]);
}//for
if(sem_post(&data_mutex))
{
printf("ERROR POSTING data_mutexn");
}//if
else
{
flg = 1;
printf("data_mutex WAS POSTEDn");
}
}//for
}//read_data
void* play_data(void *arg)
{
int i, j;
for (j = 0 ; j < 5 ; j++)
{
while (flg == 0);// waits for the data to be redy
sem_wait(&data_mutex);
printf("ndata_ready_mutex MUTEX TAKENn");
printf("playn");
for(i = 0 ; i < rdlen - 1; i++)
{
output_data[i] = input_data[i];
printf("Playing.... : %cn", output_data[i]);
//rdlen = write(sound_ptr, input_data, sizeof(input_data));
//ioctl(sound_ptr, KIOCSOUND, 119);
//usleep(500000);
//sound(output_data[i] - 48);
}//for
if(sem_post(&data_mutex))
{
printf("ERROR POSTING data_mutexn");
}//if
else
{
flg = 0;
printf("data_mutex WAS POSTEDn");
}
}//for
}//play_data
int main(int argc, char **argv)
{
int i;
sem_init(&data_mutex, 0, 1);
void * read_return, *play_return;
pthread_t play_t, read_t;
if (argc == 3)
{
printf( "input address = %sn", argv[1]);
printf( "output address = %sn", argv[2]);
}
com_ptr = open("/dev/stdin", O_RDWR | O_NOCTTY);
sound_ptr = open("/proc/asound/card0", 'w');
if(com_ptr == -1)
printf("nError! in Opening ttyUSB0 ");
else
printf("nttyUSB0 Opened Successfully ");
if (pthread_create(&read_t, NULL, &read_data, NULL))
{
perror("read pthread_create() error");
exit(1);
}//if
sleep(1); // ensuring the read gets the first round
if (pthread_create(&play_t, NULL, &play_data, NULL))
{
perror("play pthread_create() error");
exit(1);
}//if
if (pthread_join(read_t, &read_return))
{
perror("read pthread_join() error");
exit(1);
}//if
if (pthread_join(play_t, &play_return))
{
perror("play pthread_join() error");
exit(1);
}//if
sem_destroy(&data_mutex);
return 0;
}//main
I'm getting the data and can read it - but I need to write it to the sound card - which i can't do.
c linux
add a comment |
I"m trying to write raw data i get externally from serial port to the sound card device using c code in ubuntu in order to play it.
I"ve looked everywhere for a solution and found that it can be done via ALSA API, but couldn't fund any clear example on how should i do it.
I also tried using the ioctl() function - but it doesn't play.
#include <stdio.h>
#include <fcntl.h> // File Control Definitions
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
int flg = 0, rdlen = 0;
int com_ptr, sound_ptr;
char input_data[16];
char output_data[16];
sem_t data_mutex;
void* read_data(void *arg)
{
int i, j;
for (j = 0 ; j < 5 ; j++)
{
while(flg == 1);//waits for the data to be read
sem_wait(&data_mutex);
printf("ndata_read_mutex MUTEX TAKENn");
printf("readn");
rdlen = read(com_ptr, input_data, sizeof(input_data));
//fwrite(buf, 1, rdlen, log_file);
printf("input length : %dn",rdlen);
for(i = 0 ; i < rdlen - 1; i++)
{
//input_data[i] = data[i];
printf("Reading.... : %cn", input_data[i]);
}//for
if(sem_post(&data_mutex))
{
printf("ERROR POSTING data_mutexn");
}//if
else
{
flg = 1;
printf("data_mutex WAS POSTEDn");
}
}//for
}//read_data
void* play_data(void *arg)
{
int i, j;
for (j = 0 ; j < 5 ; j++)
{
while (flg == 0);// waits for the data to be redy
sem_wait(&data_mutex);
printf("ndata_ready_mutex MUTEX TAKENn");
printf("playn");
for(i = 0 ; i < rdlen - 1; i++)
{
output_data[i] = input_data[i];
printf("Playing.... : %cn", output_data[i]);
//rdlen = write(sound_ptr, input_data, sizeof(input_data));
//ioctl(sound_ptr, KIOCSOUND, 119);
//usleep(500000);
//sound(output_data[i] - 48);
}//for
if(sem_post(&data_mutex))
{
printf("ERROR POSTING data_mutexn");
}//if
else
{
flg = 0;
printf("data_mutex WAS POSTEDn");
}
}//for
}//play_data
int main(int argc, char **argv)
{
int i;
sem_init(&data_mutex, 0, 1);
void * read_return, *play_return;
pthread_t play_t, read_t;
if (argc == 3)
{
printf( "input address = %sn", argv[1]);
printf( "output address = %sn", argv[2]);
}
com_ptr = open("/dev/stdin", O_RDWR | O_NOCTTY);
sound_ptr = open("/proc/asound/card0", 'w');
if(com_ptr == -1)
printf("nError! in Opening ttyUSB0 ");
else
printf("nttyUSB0 Opened Successfully ");
if (pthread_create(&read_t, NULL, &read_data, NULL))
{
perror("read pthread_create() error");
exit(1);
}//if
sleep(1); // ensuring the read gets the first round
if (pthread_create(&play_t, NULL, &play_data, NULL))
{
perror("play pthread_create() error");
exit(1);
}//if
if (pthread_join(read_t, &read_return))
{
perror("read pthread_join() error");
exit(1);
}//if
if (pthread_join(play_t, &play_return))
{
perror("play pthread_join() error");
exit(1);
}//if
sem_destroy(&data_mutex);
return 0;
}//main
I'm getting the data and can read it - but I need to write it to the sound card - which i can't do.
c linux
Then have you searched the ALSA API for input function? Did they work? How are threads related to sending raw data to soundcard? - it would be best to show the minimal code needed to reproduce the behaviour.
– Kamil Cuk
Jan 4 at 18:37
add a comment |
I"m trying to write raw data i get externally from serial port to the sound card device using c code in ubuntu in order to play it.
I"ve looked everywhere for a solution and found that it can be done via ALSA API, but couldn't fund any clear example on how should i do it.
I also tried using the ioctl() function - but it doesn't play.
#include <stdio.h>
#include <fcntl.h> // File Control Definitions
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
int flg = 0, rdlen = 0;
int com_ptr, sound_ptr;
char input_data[16];
char output_data[16];
sem_t data_mutex;
void* read_data(void *arg)
{
int i, j;
for (j = 0 ; j < 5 ; j++)
{
while(flg == 1);//waits for the data to be read
sem_wait(&data_mutex);
printf("ndata_read_mutex MUTEX TAKENn");
printf("readn");
rdlen = read(com_ptr, input_data, sizeof(input_data));
//fwrite(buf, 1, rdlen, log_file);
printf("input length : %dn",rdlen);
for(i = 0 ; i < rdlen - 1; i++)
{
//input_data[i] = data[i];
printf("Reading.... : %cn", input_data[i]);
}//for
if(sem_post(&data_mutex))
{
printf("ERROR POSTING data_mutexn");
}//if
else
{
flg = 1;
printf("data_mutex WAS POSTEDn");
}
}//for
}//read_data
void* play_data(void *arg)
{
int i, j;
for (j = 0 ; j < 5 ; j++)
{
while (flg == 0);// waits for the data to be redy
sem_wait(&data_mutex);
printf("ndata_ready_mutex MUTEX TAKENn");
printf("playn");
for(i = 0 ; i < rdlen - 1; i++)
{
output_data[i] = input_data[i];
printf("Playing.... : %cn", output_data[i]);
//rdlen = write(sound_ptr, input_data, sizeof(input_data));
//ioctl(sound_ptr, KIOCSOUND, 119);
//usleep(500000);
//sound(output_data[i] - 48);
}//for
if(sem_post(&data_mutex))
{
printf("ERROR POSTING data_mutexn");
}//if
else
{
flg = 0;
printf("data_mutex WAS POSTEDn");
}
}//for
}//play_data
int main(int argc, char **argv)
{
int i;
sem_init(&data_mutex, 0, 1);
void * read_return, *play_return;
pthread_t play_t, read_t;
if (argc == 3)
{
printf( "input address = %sn", argv[1]);
printf( "output address = %sn", argv[2]);
}
com_ptr = open("/dev/stdin", O_RDWR | O_NOCTTY);
sound_ptr = open("/proc/asound/card0", 'w');
if(com_ptr == -1)
printf("nError! in Opening ttyUSB0 ");
else
printf("nttyUSB0 Opened Successfully ");
if (pthread_create(&read_t, NULL, &read_data, NULL))
{
perror("read pthread_create() error");
exit(1);
}//if
sleep(1); // ensuring the read gets the first round
if (pthread_create(&play_t, NULL, &play_data, NULL))
{
perror("play pthread_create() error");
exit(1);
}//if
if (pthread_join(read_t, &read_return))
{
perror("read pthread_join() error");
exit(1);
}//if
if (pthread_join(play_t, &play_return))
{
perror("play pthread_join() error");
exit(1);
}//if
sem_destroy(&data_mutex);
return 0;
}//main
I'm getting the data and can read it - but I need to write it to the sound card - which i can't do.
c linux
I"m trying to write raw data i get externally from serial port to the sound card device using c code in ubuntu in order to play it.
I"ve looked everywhere for a solution and found that it can be done via ALSA API, but couldn't fund any clear example on how should i do it.
I also tried using the ioctl() function - but it doesn't play.
#include <stdio.h>
#include <fcntl.h> // File Control Definitions
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
int flg = 0, rdlen = 0;
int com_ptr, sound_ptr;
char input_data[16];
char output_data[16];
sem_t data_mutex;
void* read_data(void *arg)
{
int i, j;
for (j = 0 ; j < 5 ; j++)
{
while(flg == 1);//waits for the data to be read
sem_wait(&data_mutex);
printf("ndata_read_mutex MUTEX TAKENn");
printf("readn");
rdlen = read(com_ptr, input_data, sizeof(input_data));
//fwrite(buf, 1, rdlen, log_file);
printf("input length : %dn",rdlen);
for(i = 0 ; i < rdlen - 1; i++)
{
//input_data[i] = data[i];
printf("Reading.... : %cn", input_data[i]);
}//for
if(sem_post(&data_mutex))
{
printf("ERROR POSTING data_mutexn");
}//if
else
{
flg = 1;
printf("data_mutex WAS POSTEDn");
}
}//for
}//read_data
void* play_data(void *arg)
{
int i, j;
for (j = 0 ; j < 5 ; j++)
{
while (flg == 0);// waits for the data to be redy
sem_wait(&data_mutex);
printf("ndata_ready_mutex MUTEX TAKENn");
printf("playn");
for(i = 0 ; i < rdlen - 1; i++)
{
output_data[i] = input_data[i];
printf("Playing.... : %cn", output_data[i]);
//rdlen = write(sound_ptr, input_data, sizeof(input_data));
//ioctl(sound_ptr, KIOCSOUND, 119);
//usleep(500000);
//sound(output_data[i] - 48);
}//for
if(sem_post(&data_mutex))
{
printf("ERROR POSTING data_mutexn");
}//if
else
{
flg = 0;
printf("data_mutex WAS POSTEDn");
}
}//for
}//play_data
int main(int argc, char **argv)
{
int i;
sem_init(&data_mutex, 0, 1);
void * read_return, *play_return;
pthread_t play_t, read_t;
if (argc == 3)
{
printf( "input address = %sn", argv[1]);
printf( "output address = %sn", argv[2]);
}
com_ptr = open("/dev/stdin", O_RDWR | O_NOCTTY);
sound_ptr = open("/proc/asound/card0", 'w');
if(com_ptr == -1)
printf("nError! in Opening ttyUSB0 ");
else
printf("nttyUSB0 Opened Successfully ");
if (pthread_create(&read_t, NULL, &read_data, NULL))
{
perror("read pthread_create() error");
exit(1);
}//if
sleep(1); // ensuring the read gets the first round
if (pthread_create(&play_t, NULL, &play_data, NULL))
{
perror("play pthread_create() error");
exit(1);
}//if
if (pthread_join(read_t, &read_return))
{
perror("read pthread_join() error");
exit(1);
}//if
if (pthread_join(play_t, &play_return))
{
perror("play pthread_join() error");
exit(1);
}//if
sem_destroy(&data_mutex);
return 0;
}//main
I'm getting the data and can read it - but I need to write it to the sound card - which i can't do.
c linux
c linux
asked Jan 4 at 18:23
MichaelMichael
12
12
Then have you searched the ALSA API for input function? Did they work? How are threads related to sending raw data to soundcard? - it would be best to show the minimal code needed to reproduce the behaviour.
– Kamil Cuk
Jan 4 at 18:37
add a comment |
Then have you searched the ALSA API for input function? Did they work? How are threads related to sending raw data to soundcard? - it would be best to show the minimal code needed to reproduce the behaviour.
– Kamil Cuk
Jan 4 at 18:37
Then have you searched the ALSA API for input function? Did they work? How are threads related to sending raw data to soundcard? - it would be best to show the minimal code needed to reproduce the behaviour.
– Kamil Cuk
Jan 4 at 18:37
Then have you searched the ALSA API for input function? Did they work? How are threads related to sending raw data to soundcard? - it would be best to show the minimal code needed to reproduce the behaviour.
– Kamil Cuk
Jan 4 at 18:37
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044213%2fsending-raw-data-to-soundcard-with-c-code-in-ubuntu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044213%2fsending-raw-data-to-soundcard-with-c-code-in-ubuntu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Then have you searched the ALSA API for input function? Did they work? How are threads related to sending raw data to soundcard? - it would be best to show the minimal code needed to reproduce the behaviour.
– Kamil Cuk
Jan 4 at 18:37