1#!/usr/bin/perl
2
3# This version holds the tones for longer than the normal version --
4# 25 minutes on and 5 minutes off, compared to 5:5 for the normal
5# version.
6
7# Enter wake-up hour as hh or hh:mm as first argument, if required
8
9$tar= 7;	# 7am
10if (@ARGV && ($ARGV[0] =~ /^(\d+):(\d+)$/)) {
11    shift;
12    $tar= $1 + $2 / 60;
13} elsif (@ARGV && ($ARGV[0] =~ /^(\d+)$/)) {
14    shift;
15    $tar= $1;
16}
17
18printf "Generating tones from 21:00 -> %02d:%02d\n", int($tar), int(60 * ($tar - int($tar)));
19
20my @ts= ();	# Tone-sets
21my @tl= ();	# Time-lines
22
23# Binaural range
24my $r0= 0.2;
25my $r1= 1.0;
26my $lr0= log $r0;
27my $lr1= log $r1;
28
29# Carrier range
30my $cr0= 60;
31my $cr1= 120;
32my $lcr0= log $cr0;
33my $lcr1= log $cr1;
34
35my $cnt= 0;
36while (1) {
37    $hh= -3 + int($cnt / 6);
38    $mm= 10 * ($cnt % 6);
39    last if ($hh + $mm/60 >= $tar);
40    $hh += 24 if ($hh < 0);
41
42    $bb= exp($lr0 + ($lr1 - $lr0) * rand);
43    $cc= exp($lcr0 + ($lcr1 - $lcr0) * rand);
44    $dmy= rand;
45    push @ts, sprintf "t%02d: %.2f+%5.3f/40 %.2f+4.2/10\n", $cnt, $cc, $bb, $cc * 2;
46    push @tl, sprintf "%02d:%02d t%02d\n", $hh, $mm, $cnt;
47    push @tl, sprintf "%02d:%02d off\n", $hh, $mm+25;
48    $cnt+=3;
49
50    # Alternative version for 15 minutes on, 5 minutes off:
51    #    push @tl, sprintf "%02d:%02d off\n", $hh, $mm+15;
52    #    $cnt+=2;
53}
54
55die unless open OUT, ">tmp-prog";
56print OUT <<END;
57#
58#       The idea of this sequence is to play random tones in the sub
59#       1.0Hz band all night, accompanied by a 4.2Hz tone to somehow
60#       provide a kind of link up to consciousness.
61#
62#	This file was AUTOMATICALLY GENERATED
63#
64
65END
66
67print OUT @ts;
68print OUT <<END;
69off: -
70wakeup: 100+0.92/20 200+4.2/20 400+7/40
71
72END
73print OUT @tl;
74printf OUT "%02d:%02d wakeup\n", int($tar), int(60 * ($tar - int($tar)));
75printf OUT "%02d:%02d off\n", 1 + int($tar), int(60 * ($tar - int($tar)));
76die unless close OUT;
77
78exec "sbagen tmp-prog";
79die;
80