Page 2 of 2

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 7:55 am
by PeterO
1dot0 wrote: that is also interesting to me, but where is a function to play just a .wav file by it's filename out of a proprietary source code,
I doubt you'll find anything like that in a library because in general it just isn't useful, and is trivial to implement anyway.
The tinyalsa library looks like it provides a simpler wrapper round the full alsa API, and provides the important functions to write blocks of samples to a sound device.
Normally writing samples is incorporated into an event loop of some form so that, for example, a "STOP PLAY" button on a GUI can be implemented easily.
PeterO

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 8:10 am
by buja
Playing a .wav file with a single function call with the name of the .wav file sounds like the most basic operation I can imagine, so 1dot0 asks the right question.

I can totally see code like this:

Code: Select all

if (error)
    playwav("error.wav");
else
    playwav("okay.wav");
It may be trivial, but useful. For many people it will be the only thing they will ever need.

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 8:18 am
by 1dot0
buja wrote:Playing a .wav file with a single function call with the name of the .wav file sounds like the most basic operation I can imagine, so 1dot0 asks the right question.

I can totally see code like this:

Code: Select all

if (error)
    playwav("error.wav");
else
    playwav("okay.wav");
It may be trivial, but useful. For many people it will be the only thing they will ever need.
does it mean when you say "I can totally see code like this:
... playwav("error.wav");
that you actually see it here somewhere already or that you also would appreciate to have that?

BTW, also a function like
recordwav("mytestrecord.wav")
would be useful to have, too, recorded by a microphone plugged to a USB sound"card" e.g., started and stopped by a digital pin read, e.g.

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 8:21 am
by buja
I mean I would also appreciate if there was such a simple function like that ;)

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 8:22 am
by 1dot0
aah, thanks! 8-)

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 8:32 am
by PeterO
It's all there in https://github.com/tinyalsa/tinyalsa/bl ... tinyplay.c It takes a wav file name as a parameter (in argv) and plays it. If you can't work it out from there, well maybe you need to learn more about C programming first.
PeterO

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 8:35 am
by 1dot0
PeterO wrote:It's all there in https://github.com/tinyalsa/tinyalsa/bl ... tinyplay.c It takes a wav file name as a parameter (in argv) and plays it. If you can't work it out from there, well maybe you need to learn more about C programming first.
PeterO
just like always PeterO's posts are famous for what they are.

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 9:30 am
by miaomiao
LdB wrote:You should have got all that when you installed the package .. see the instructions
(https://github.com/tinyalsa/tinyalsa)

sudo apt-add-repository ppa:taylorcholberton/tinyalsa
sudo apt-get update
sudo apt-get install tinyalsa
sudo apt-get install libtinyalsa-dev

The files just use the linux API there really isn't much to them.

Hi, I am using following code in Geany with a laser scanner. #include <stdlib.h>
#include <stdio.h>
#include <string.h>


int playSound( char *filename ) {
char command[256];
int status;

/* create command to execute */
sprintf( command, "omxplayer -o local giveway.mp3" );

/* play sound */
status = system( command );

return status;
}

int main( int argc, char *argv[] ) {


//----- TX BYTES -----
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;

p_tx_buffer = &tx_buffer[0];
*p_tx_buffer++ = 'S'; //receive "S" means stop, then play the music
if( distance<600)
{
/* play the music */
playSound( argv[1] );

if (uart0_filestream != -1)
{
int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
if (count < 0)
{
printf("UART TX error\n");
}
}



return 0;
}
}
else .....

However, raspberry pi could not work with the playSound function and show the below message.

pi@raspberrypi:~/urg_library/samples/c $ sudo ./get_distance

2794 [mm],2637 [mm],0 [mm], (102384 [msec])
2789 [mm],2640 [mm],0 [mm], (103485 [msec])
2794 [mm],2639 [mm],0 [mm], (103585 [msec])
2799 [mm],563 [mm],542 [mm], (103686 [msec])
Audio codec mp3 channels 1 samplerate 22050 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)
619 [mm],513 [mm],519 [mm], (103786 [msec])
Audio codec mp3 channels 1 samplerate 22050 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)
2790 [mm],2640 [mm],509 [mm], (103886 [msec])
Audio codec mp3 channels 1 samplerate 22050 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)
2801 [mm],2642 [mm],0 [mm], (103986 [msec])
urg_get_distance: no error.

What should I do to solve this?

Re: .wav file: play from a C/C++ program (Raspbian)

Posted: Wed Jul 19, 2017 10:05 am
by LdB
I am a programmer I don't even use linux but looking at that I would say it's playing as there is no error reported. As I said I can fix the code because it was trivial and a programming thing and this is a C++ forum.

So it's probably up on a sound output you aren't connected to. The Pi has two I know of from my baremetal work being the HDMI channel which goes to screens with HDMI sound and the physical jack port. It probably supports USB sound devices as well if you have them plugged in.

I am guessing on linux desktop like on windows there is some sort of control you can select which device is default. My guess is your default is the wrong one to what you are plugged into :-)