View Single Post
Old 07-13-2012, 12:56 AM   #1
geekmaster
Carpe diem, c'est la vie.
geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.
 
geekmaster's Avatar
 
Posts: 6,433
Karma: 10773670
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
geekmaster's sound demo thread

Here, I will post a series of sound demos for the eink kindles (except the soundless K4). To begin, I present here a simple sound demo with relatively little code compared to other sound programs, and it even works on all kindles I tested it on (and probably the Raspberry Pi too).


geekmaster's noisy rhythms audio demo

This simple kindle sound demo plays a noisy rhythm on your eink kindle speakers (or headphones). To play this sound demo, just download and extract "noisy" (below), then copy it to your kindle USB drive, then run it (SSH, or with a launcher). It will stop automatically when finished.

Yeah, it is really nothing fancy, but now you can use your kindle to annoy others in the room with you.


The source code:
Spoiler:
PHP Code:
//====================================================
// noisy 1.0 - geekmaster's noisy rhythms audio demo
// Copyright (C) 2012 by geekmaster, with MIT license:
// http://www.opensource.org/licenses/mit-license.php
// with thanks to twobob for figuring out kindle sound
//----------------------------------------------------
// Tested on DX,K3,K5main.
//----------------------------------------------------
#include <stdio.h>
#include <asoundlib.h>

int main(void)
{
    
unsigned int i,j;
    
unsigned char buf[1024]; // play buffer
    
snd_pcm_sframes_t fc;   // frame count
    
snd_pcm_t *hp;         // play handle

    
printf("geekmaster's noisy rhythms audio demo\n");
    
snd_pcm_open(&hp,"default",SND_PCM_STREAM_PLAYBACK,0);
    
snd_pcm_set_params(hp,SND_PCM_FORMAT_U8,
        
SND_PCM_ACCESS_RW_INTERLEAVED,1,8000,1,500000); // 0.5sec
    
for (i=0;i<256;i++) {
        for (
j=0;j<sizeof(buf);j++) buf[j]=(random()&3)*i*350;
        
fc=snd_pcm_writei(hp,buf,sizeof(buf));  // play buffer
        
if (fc<0fc=snd_pcm_recover(hp,fc,0); // error recovery
    
}
    
snd_pcm_close(hp);
    return 
0;


After cross-compiling the latest 1.0.25 ALSA sound library on my linux host PC (from source, with static-linking support), I used it to compile "noisy.c" with this command:

arm-linux-gcc -o noisy noisy.c -lasound

Geekmaster "noisy rhythms" Demo,
Sound Synthesis Algorithm,
Theory of Operation:

Spoiler:
The C "random()" function returns a random positive integer value (including 0). Normally you limit it to a range by truncating it. In this case, the "&3" keeps only the bottom two bits (giving random values in the range 0-3).

The outer "i" loop is repeated 256 times, and each iteration plays a 1024-byte sound buffer after it is filled with "amplitude modulated" random numbers by an inner "j" loop.

In the inner "j" loop, the buffer gets filled with 1024 2-bit random numbers that have been multiplied by (i*350). This multiplication changes (modulates) the maximum value of the result (i.e. the peak amplitude of the waveform in the buffer). In this case, i*350 causes the max value to be greater than 255 in many cases, so when the value is truncated to 8-bits and stored into a 1-byte sound buffer position, it gets "distorted" in a way that causes some very large numbers to become small (quiet).

Because the amplitude modulator control variable "i" sweeps from 0-255 while this demo plays, each 1024-byte buffer (short time period of random "hissing" noise) gets its volume set to a different "constrained random" value. I chose different multiplier (distortion/gain) values until I found a value (350) that sounded pleasing with a nice syncopated periodicity, giving it a not-so-annoying semi-percussive rift.

The sample rate is 8000 samples per second, which means that if you call one play of the 1024-byte sound buffer a "beat", then this program plays 256 beats of almost 8 beats per second. Each beat plays at a different sound volume, creating the "semi-percussive" sound. Changing the "350" multiplier changes both the depth of distortion and the perceived repetition rate of the slowly varying distortion pattern. This perceived repetition is an audio interference pattern similar in nature to the visual interference (moire) patterns in some of my eink animation demos.

Now that we have simple audio support for native mode programs (on all eink kindles except the soundless K4), our next step is to integrate it into gmplay (geekmaster's kindle video player). Then we get to encode movies with sound. Then YOU get to play (and listen to) those movies.

EDIT: K4 owners, never fear, there are plans to add USB sound support to your K4 too, using the same methods already being used for the Nook Simple Touch (a short $0.97USD USB host mode cable, a $1.35USD USB sound stick, and some free software). We just need to borrow a little code from the Nook Simple Touch forums. I ordered my USB host cables and USB sound sticks at the links above (with free international shipping).


The download files:
Attached Files
File Type: gz noisy.tar.gz (3.5 KB, 191 views)

Last edited by geekmaster; 07-13-2012 at 01:52 PM.
geekmaster is offline   Reply With Quote