Been doing some more playing around with the Pi, trying to do some sound generation. Would like to add in some of the beeps I have in other versions of the codeā¦ Been searching and hacking and have a little test program that is starting to workā¦ Needs work to make safe, also figure out things like when is it doneā¦ but at least I can make noise other than trying to talkā¦
/*
* This extra small demo sends a random samples to your speakers.
*/
#include <alsa/asoundlib.h>
#include <math.h>
static char *device = "default"; /* playback device */
snd_output_t *output = NULL;
unsigned char buffer[24*1024]; /* some random data */
#define sampleRate 48000L
extern int FillSoundBuffer(unsigned char *psz, int cb, unsigned int freq, unsigned long duration);
int main(void)
{
int err;
unsigned int i;
int cb;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
int freq = 500;
for (i = 0; i < sizeof(buffer); i++)
buffer* = random() & 0xff;
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_U8,
SND_PCM_ACCESS_RW_INTERLEAVED,
1,
48000,
1,
500000)) < 0) { /* 0.5sec */
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
for (i = 0; i < 16; i++) {
cb = FillSoundBuffer(buffer, sizeof(buffer), freq, 500);
frames = snd_pcm_writei(handle, buffer, cb);
printf ("freq %i frames: %li\n", freq, frames);
if (frames < 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0) {
printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
break;
}
if (frames > 0 && frames < (long)sizeof(buffer))
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
freq += 250;
}
snd_pcm_close(handle);
return 0;
}
int FillSoundBuffer(unsigned char *psz, int cb, unsigned int freq, unsigned long duration) {
double sample;
unsigned char val;
int numSamples = ((long)((long)duration * sampleRate))/1000L; //
printf("D: %lu RT: %lu, Samples %u\n", duration, sampleRate, numSamples);
for (int i = 0; i < numSamples; ++i) {
sample = sin(2 * M_PI * i / (sampleRate/freq));
val = (unsigned char)(sample * 255);
*psz++ = val;
}
return numSamples;
}
The way more complicated than it needs to be makefile looks like:
[code]#~~~~~~~~~~~~~~~~~~~~ Output File Name ~~~~~~~~~~~~~~~~~~~~
MAIN_OUT = testPCM
#~~~~~~~~~~~~~~~~~~~~ Source Files ~~~~~~~~~~~~~~~~~~~~
SOURCES =
testPCM.cpp
MAIN_OBJS:= $(subst .cpp,.o,$(SOURCES))
MAIN_DEPS:= $(subst .cpp,.d,$(SOURCES))
#~~~~~~~~~~~~~~~~~~~~ Include Directories ~~~~~~~~~~~~~~~~~~~~
INCLUDE_DIRS = -I. -I/usr/include/espeak
#~~~~~~~~~~~~~~~~~~~~ Library Directories ~~~~~~~~~~~~~~~~~~~~
LIBRARY_DIRS = -L/usr/lib/arm-linux-gnueabihf
#~~~~~~~~~~~~~~~~~~~~ Compiler Options ~~~~~~~~~~~~~~~~~~~~
COMPILE_OPTS = -Wall -pedantic -g -O2 -fno-rtti
#COMPILE_OPTS = -Wall -pedantic -g -O2
#~~~~~~~~~~~~~~~~~~~~ Linker Options ~~~~~~~~~~~~~~~~~~~~
LDFLAGS = $(LIBRARY_DIRS) -lpthread -lespeak
#~~~~~~~~~~~~~~~~~~~~ Toolchain Prefix ~~~~~~~~~~~~~~~~~~~~
TCHAIN_PREFIX=arm-linux-gnueabihf-
#TCHAIN_PREFIX=x86_64-linux-gnu-
CXX = $(TCHAIN_PREFIX)g++
CXXFLAGS = $(COMPILE_OPTS) $(INCLUDE_DIRS)
#~~~~~~~~~~~~~~~~~~~~ all ~~~~~~~~~~~~~~~~~~~~
all: begin gccversion build end
#~~~~~~~~~~~~~~~~~~~~ build ~~~~~~~~~~~~~~~~~~~~
build: $(MAIN_OUT)
$(MAIN_OUT): $(MAIN_OBJS)
$(CXX) $(CXXFLAGS) $(MAIN_OBJS) -o $(MAIN_OUT) $(LDFLAGS)
MSG_BEGIN = -------- begin --------
MSG_END = -------- end --------
#~~~~~~~~~~~~~~~~~~~~ Eye candy ~~~~~~~~~~~~~~~~~~~~
begin:
@echo
@echo $(MSG_BEGIN)
end:
@echo $(MSG_END)
@echo
gccversion:
@$(CC) --version
#~~~~~~~~~~~~~~~~~~~~ clean ~~~~~~~~~~~~~~~~~~~~
clean: begin clean_list end
clean_list:
-rm $(MAIN_OBJS)
-rm $(MAIN_OUT)
-rm $(MAIN_DEPS)
#~~~~~~~~~~~~~~~~~~~~ backup ~~~~~~~~~~~~~~~~~~~~
backup: clean
tar cJvf ā¦/$(MAIN_OUT)_date +"%Y-%m-%d_%H%M"
.tar.xz *
#~~~~~~~~~~~~~~~~~~~~ Dependency Generation
include $(subst .cpp,.d,$(SOURCES))
%.d: %.cpp
$(CC) -M $(CPPFLAGS) $< > $@.$$$$;
sed ās,($).o :],\1.o $@ : ,gā < $@.$$$$ > $@;
rm -f $@.$$$$
[/code]
In order to get this to build I needed the appropriate header files and libs, which I believe I got by doing the command:
sudo apt-get install libasound2-dev
Maybe tomorrow I will try to integrate this s a function into the Phoenix code. Also need to see if it should be done on separate threadā¦*