#!/bin/sh
if ! which sox > /dev/null; then
 echo "Sorry, we want SoX installed"
 exit
fi
if ! which gcc > /dev/null; then
 echo "Sorry, we want compiling tools"
 exit
fi
if expr $# "<" "1" >/dev/null; then
 echo "Usage dnb <audio file>"
 exit
fi
if ! test -f /tmp/dnb.binary; then
 if ! which sdl-config > /dev/null; then
  echo "Sorry, we want SDL-devel and sdl-config"
  exit
 fi
 grep SRC: $0 | grep -v "grep SRC:" | sed -e s/SRC://g > /tmp/dnb.c
 gcc -o /tmp/dnb.binary /tmp/dnb.c `sdl-config --libs --cflags` -w
fi
sox $1 -t raw -r 22050 -c 1 -u -1 /tmp/loop.bin 1> /dev/null 2> /dev/null
sox $1 -t raw -r 22050 -c 1 -u -b /tmp/loop.bin 1> /dev/null 2> /dev/null
/tmp/dnb.binary /tmp/loop.bin
exit
SRC:#include <stdio.h>
SRC:#include <stdlib.h>
SRC:#include <SDL.h>
SRC:#define CONSTANT	1.009f
SRC:SDL_Surface *d_screen;
SRC:int white, black;
SRC:unsigned char *sample;
SRC:int pos, lpos;
SRC:int pos2;
SRC:float beat_pos, speed;
SRC:int bend_up, bend_down;
SRC:int mods[17];
SRC:char bit_codes[]= {
SRC: "qwertyuiasdfghjk"
SRC:};
SRC:void q_input(void) {
SRC: int i;
SRC: SDL_Event event;
SRC: while(SDL_PollEvent(&event) ) {
SRC:  switch(event.type) {
SRC:   case SDL_KEYDOWN:
SRC:    if(event.key.keysym.sym =='z') 
SRC:    { bend_down=1; bend_up = 0; speed=1.0f; }
SRC:    if(event.key.keysym.sym=='x')
SRC:    { bend_up = 1; bend_down =0;speed=1.0f; }
SRC:    for(i=0;i<16;i++) 
SRC:     if(event.key.keysym.sym == bit_codes[i])
SRC:      mods[i+1] = 1;
SRC:    if(event.key.keysym.sym == ' ' )
SRC:     mods[17] = 1;
SRC:    break;
SRC:   case SDL_KEYUP:
SRC:    if(event.key.keysym.sym == 'z') 
SRC:    { bend_down = 0; speed= 1.0f; }
SRC:    if(event.key.keysym.sym == 'x') 
SRC:    { bend_up = 0; speed = 1.0f; }
SRC:    for(i=0;i<16;i++) 
SRC:     if(event.key.keysym.sym == bit_codes[i])
SRC:      mods[i+1] = 0;
SRC:    if(event.key.keysym.sym == ' ') 
SRC:     mods[17] = 0;
SRC:    break;
SRC:  }
SRC: }
SRC:}
SRC:#define SET_BIT(x,y) (x|(1<<y))
SRC:#define TEST_BIT(x,y) ((x>>y)&1)
SRC:int mod_pos(int in) {
SRC: int out, pool,i;
SRC: pool = 0;
SRC: out = in;
SRC: for(i=0;i<16;i++)
SRC:  if(mods[i+1] == 1)
SRC:   if(TEST_BIT(out,i) == 1) 
SRC:    pool = 1;
SRC: if(mods[17] == 1 && pool == 1)
SRC:  return 0; 
SRC: for(i=0;i<16;i++)
SRC:  if(mods[i+1] == 1 && pool == 1)
SRC:   out = SET_BIT(out,i); 
SRC: return out%0xffff;
SRC:}
SRC:void draw_vline(int x, signed char h, int color) {
SRC: int i,j;
SRC: unsigned char *pix, *src;
SRC: pix = (unsigned char *)((int)d_screen->pixels + ((h+128) * d_screen->pitch) + 
SRC: (d_screen->format->BytesPerPixel * x));
SRC: src = (unsigned char *)&color;
SRC: for(i=0;i<abs(h);i++) {
SRC:  for(j=0;j<d_screen->format->BytesPerPixel;j++)
SRC:   pix[j] = src[j];
SRC:  if(h<0) 
SRC:   pix+=d_screen->pitch;
SRC:  else
SRC:   pix-=d_screen->pitch;
SRC: } 
SRC:}
SRC:void audio(void *userdata, Uint8 *stream, int len) {
SRC: int i,j;
SRC: int qq;
SRC: SDL_FillRect(d_screen, NULL, white);
SRC: if(bend_up == 1) 
SRC:  speed *=CONSTANT;
SRC: if(bend_down == 1)
SRC:  speed /=CONSTANT;
SRC: SDL_LockSurface(d_screen);
SRC: for(i=0;i<len;i++) {
SRC:  lpos = pos2;
SRC:  pos2 = mod_pos(pos);
SRC:  if(((pos2 -1)%0xffff) != lpos) {
SRC:   beat_pos = (float)pos2;
SRC:  }
SRC:  qq = (int)beat_pos;
SRC:  qq&=0xffff;
SRC:  if( abs(qq - pos2) > 0x7ff) 
SRC:   stream[i] = 0x7f;
SRC:  else
SRC:   stream[i] = sample[qq];
SRC:  beat_pos+=speed;
SRC:  pos++;
SRC:  pos%=0xffff;
SRC:  draw_vline( i, (signed char)stream[i], black);
SRC: }
SRC: SDL_UnlockSurface(d_screen);
SRC: SDL_UpdateRect(d_screen,0,0,0,0);
SRC:}
SRC:SDL_AudioSpec audio_spec;
SRC:int main(int argc, char **argv){
SRC: int i;
SRC: FILE *fp;
SRC: signed char j;
SRC: SDL_AudioSpec spec;
SRC: if(argc<2) exit(-1);
SRC: if(!(fp = fopen(argv[1], "rb"))) {
SRC:  perror(argv[1]);
SRC:  exit(-1);
SRC: }
SRC: sample = (unsigned char *)malloc(0xffff);
SRC: fread(sample, 1, 0xffff, fp);
SRC: fclose(fp);
SRC: SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER);
SRC: spec.freq = 22050;
SRC: spec.format= AUDIO_S8;
SRC: spec.channels = 1;
SRC: spec.samples = 512;
SRC: spec.callback = audio;
SRC: if(SDL_OpenAudio(&spec, &audio_spec) < 0) {
SRC:  printf(SDL_GetError());
SRC:  exit(-1);
SRC: }
SRC: if((d_screen = SDL_SetVideoMode(512, 256, 16, 0)) < 0){
SRC:  printf(SDL_GetError());
SRC:  exit(-1);
SRC: }
SRC: white = SDL_MapRGB(d_screen->format, 255,255,255);
SRC: black = SDL_MapRGB(d_screen->format, 0,0,0); 
SRC: pos = 0;
SRC: lpos = 43;
SRC: speed = 1.0f;
SRC: bend_up = 0;
SRC: bend_down = 0;
SRC: bzero(mods, 256);
SRC: SDL_PauseAudio(0);
SRC: for(;;) 
SRC:  q_input();
SRC:}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (OpenBSD)

iD8DBQBJCtiiMNO4A6bnBrMRAp8bAKCEUqdZc1hAUB6enj2Bg4mqxJYVxQCdF8Aj
iBQPfbpKchRZX/ZS57O7xDI=
=75nS
-----END PGP SIGNATURE-----
