#!/usr/bin/perl

#
#       This script generates a sequence for the night and starts
#       playing it.  All through the night tone-sets are played, five
#       minutes on, then five minutes off.  The gaps are designed to
#       allow the brain some freedom to wander before being pulled
#       back by another tone-set.  This feels more comfortable to me
#       than 100% saturation tone-sets.  Also, others have noted that
#       the brain responds better to changes.
#
#       The tone-sets consist of the following frequencies:
#
#       - A random sub-delta frequency in the range 0.2 to 1.0 Hz,
#       designed to stimulate interesting things way down there in the
#       region beyond true delta (in my opinion something signficant
#       changes around 0.9Hz)
#
#       - A mid-range frequency in the range 3.5 to 5Hz, which cycles up
#       and down slowly throughout the night, ending at 5Hz, hopefully
#       modelling the natural sleep cycle.  I'm using a cycle of about
#       90 minutes.
#
#       - An optional random high beta frequency in the range 32 to 40
#       Hz (switched on/off using command-line options).  I can't
#       remember exactly what this was for - something someone
#       mentioned in an E-mail.  I always use it now.
#
#       In addition, at a few random points during the night, other
#       predefined tone-sets are played for 15 minutes.  These are
#       designed to break the pattern and cause disturbance and
#       hopefully unexpected changes in consciousness.
#
#       All-in-all, this is the sequence I use now when I fancy a
#       `binaural' night.  I've found it helps me bring waking
#       consciousness and sleeping consciousness closer - no longer
#       sleeping "the sleep of oblivion" as Merilyn Tunneshende puts
#       it.  It also helps me sleep more efficiently - I seem to get
#       better value from my night.  It also gives a different quality
#       to the night, which is why I don't use it all the time.  
#
#       I particularly like the deep slow waves, which is why I was
#       drawn to sub-delta to start with, and this is based around
#       these, now with a lot of extra trimmings based on ideas from
#       all over.
#
#       For Windows users without Perl, I've put an example sequence
#       generate by this script in the file `prog-NSDWS-example'.
#

sub usage {
    print STDERR <<END;
Usage: xxxx -b|-n [wake_up_time]
 -b    Add beta frequencies
 -n    No beta frequencies
END
    exit 1;
}

shift if ($opt_b= (@ARGV && $ARGV[0] eq '-b'));
shift if ($opt_n= (@ARGV && $ARGV[0] eq '-n'));
usage() unless $opt_b || $opt_n;

$tar= 7;	# 7am
if (@ARGV && ($ARGV[0] =~ /^(\d+):(\d+)$/)) {
    shift;
    $tar= $1 + $2 / 60;
} elsif (@ARGV && ($ARGV[0] =~ /^(\d+)$/)) {
    shift;
    $tar= $1;
}

usage() if (@ARGV);

printf "Generating tones from 21:00 -> %02d:%02d\n", int($tar), int(60 * ($tar - int($tar)));

my $beta= $opt_b ? " %.2f+%.2f/3.5" : '';

my @ts= ();	# Tone-sets
my @tl= ();	# Time-lines

# Base binaural range
my $r0= 0.2;
my $r1= 1.0;
my $lr0= log $r0;
my $lr1= log $r1;

# Binaural range for beta
my $br0= 32;
my $br1= 40;
my $lbr0= log $br0;
my $lbr1= log $br1;

# Carrier range
my $cr0= 60;
my $cr1= 120;
my $lcr0= log $cr0;
my $lcr1= log $cr1;

# Upper tone range
my $ur0= 3.5;
my $ur1= 5;	# It will end on this frequency just before wake-up time
my $lur0= log $ur0;
my $lur1= log $ur1;
my $ulen= 1.531;	# Cycle length for upper tone

my $cnt= 0;
while (1) {
    $hh= -3 + int($cnt / 6);
    $mm= 10 * ($cnt % 6);
    last if ($hh + $mm/60 >= $tar);
    $hh += 24 if ($hh < 0);
    
    if (rand() < 0.05) {
	$nn= int(10 * rand);
	push @tl, sprintf "%02d:%02d sp%di ->\n", $hh, $mm, $nn;
	push @tl, sprintf "+0:0:20 sp%d ->\n", $nn;
	push @tl, sprintf "+0:14:40 sp%d ->\n", $nn;
	push @tl, sprintf "+0:15 off\n";
	$cnt += 2;		# We're taking two 10min slots
    } else {
	$bb= exp($lr0 + ($lr1 - $lr0) * rand);
	$cc= exp($lcr0 + ($lcr1 - $lcr0) * rand);
	$dd= exp($lbr0 + ($lbr1 - $lbr0) * rand);
	$uu=  exp($lur0 + 0.5 * ($lur1 - $lur0) * 
		  (1 + cos(($tar - ($hh + $mm/60)) / $ulen * 2 * 3.14159)));

	push @ts, sprintf ("t%02d: %.2f+%5.3f/40 %.2f+%.2f/10$beta\n", 
			   $cnt, $cc, $bb, $cc * 2, $uu, $cc * 5, $dd);
	push @tl, sprintf "%02d:%02d t%02d\n", $hh, $mm, $cnt;
	push @tl, sprintf "%02d:%02d off\n", $hh, $mm+5;
	$cnt++;
    }
}

die unless open OUT, ">tmp-prog";
print OUT <<END;
#
#       The idea of this sequence is to play random tones in the sub
#       1.0Hz band all night, accompanied by a 4.2Hz tone to somehow
#       provide a kind of link up to consciousness.
#
#	I'm also now adding in some `surprise' tone-sets through the night
#
#	This file was AUTOMATICALLY GENERATED
#

sp0: 100+1.5/10 200+4.0/10 250+4.0/10 300+4.0/10
sp1: 50+0.25/10 100+1.5/10 200+4.0/7 250+4.0/7 300+4.0/7 400+10.0/5 500+10.1/5 600+4.8/5
sp2: 100+1.5/10 200+4.0/10 250+4.0/7 300+4.0/7 500+7.05/5 630+7.1/5 750+7.0/4
sp3: 200+4.0/10 250+5.4/8 300+5.4/8 600+16.2/7 750+16.2/6 900+16.2/5
sp4: 200+4.0/10 250+4.0/10 300+4.0/8 600+16.2/7 750+15.9/6 900+16.2/5
sp5: 400+3.9/10 503+4.0/9 600+4.0/8 750+3.9/7 900+4.0/6
sp6: 50+0.75/10 200+1.5/10 400+3.9/8 503+4.0/8 600+4.0/7 750+4.0/6 900+4.0/5
sp7: 503+4.0/10 600+4.0/8 750+4.0/7 900+4.0/6
sp8: 400+3.9/10 503+4.2/8 600+4.0/7 750+4.0/6 900+4.0/5
sp9: 50+0.80/10 400+4.0/10 503+4.2/8 600+4.0/7 750+4.0/6 900+4.0/5

sp0i: 100+1.5/0 200+4.0/0 250+4.03/0 300+4.07/0
sp1i: 50+0.25/0 100+1.5/0 200+4.0/0 250+4.03/0 300+4.07/0 400+10.0/0 500+10.1/0 600+4.8/0
sp2i: 100+1.5/0 200+4.0/0 250+4.03/0 300+4.07/0 500+7.05/0 630+7.1/0 750+7.0/0
sp3i: 200+4.0/0 250+5.4/0 300+5.45/0 600+16.2/0 750+16.23/0 900+16.27/0
sp4i: 200+4.0/0 250+4.03/0 300+4.07/0 600+16.2/0 750+15.9/0 900+16.25/0
sp5i: 400+3.9/0 503+4.0/0 600+4.03/0 750+3.95/0 900+4.07/0
sp6i: 50+0.75/0 200+1.5/0 400+3.9/0 503+4.0/0 600+4.03/0 750+4.05/0 900+4.08/0
sp7i: 503+4.0/0 600+4.03/0 750+4.05/0 900+4.08/0
sp8i: 400+3.9/0 503+4.2/0 600+4.0/0 750+4.03/0 900+4.07/0
sp9i: 50+0.80/0 400+4.0/0 503+4.2/0 600+4.03/0 750+4.05/0 900+4.08/0

END

print OUT @ts;
print OUT <<END;
off: -
wakeup: 100+0.92/20 200+4.2/20 400+13/40

END
print OUT @tl;
printf OUT "%02d:%02d wakeup\n", $hh, $mm;
printf OUT "%02d:%02d off\n", 1 + int($tar), int(60 * ($tar - int($tar)));
die unless close OUT;

exec "sbagen tmp-prog";
die;


