START ESCWYP MUSIC VIDEOS

This script will fold an audio file onto itself. The idea is to split the file at some position and mix the second part with the first.
Let's see:

6                   Audio file                                                     6                   first part                                                                     
5         .                                                                        5         .                                                                                        
4        . ..                     .                ..            .                 4        . ..                     .                ..    
3            . .                 . .. .          .   .     ...  . .                3            . .                 . .. .          .   .   
2 ..    .     . .           ..  .    . .        .     .   .   ..   .               2 ..    .     . .           ..  .    . .        .     .  
1.  .  .         .         .  ..        .  .   .        ..          .              1.  .  .         .         .  ..        .  .   .        .
---------------------------------------------------------------------   --->       ---------------------------------------------------------
1    ..           .       .              .. . .         ^                          1    ..           .       .              .. . .         
2                  ...  .                    .          |                          2                  ...  .                    .          
3                     .                                 |                          3                     .                                 
4                      .                                |                          4                      .                                
5                                                       |                          5                               +                       
6                                                       Cut here                   6                               +                       
                                                                                                               +++++++++                                                            
                                                                                                                   +                                                                
                                                                                                                   +                                                                


                                                                                   6                  second part                                                                  
                                                                                   5                                          
                                                                                   4        .                                 
                                                                                   3  ...  . .                                
                                                                                   2 .   ..   .                               
                                                                                   1.          .                          
                                                                                   --------------                                                                                                          
                                                                                   1
                                                                                   2
                                                                                   3
                                                                                   4
                                                                                   5
                                                                                   6

Or in symbolic form, where every letter represents a sample:

abcdefghijklmnopqrst   ---->   abcdefghijklm
            ^                     + 
            |                  nopqrst
            Cut                

If the second part after the split is longer than the first part, it will be further split at the length of the first split and so on:

abcdefghijklmnopqrst   ---->   abcdef
     ^                           + 
     |                         ghijkl
     Cut                         + 
                               mnopqr
                                 +
                               st

The script uses SoX for splitting and merging. So you have to have that installed.

------------------------------------------------- folda.sh -----------------------

#!/bin/bash

sox --version > /dev/null 2>&1
if [ $? -ne 0 ]; then
   echo "Please install sox!";
   exit 1;
fi
 
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
   echo "$0: Fold an audio file on itself using sox."
   echo "Usage: There are 2 modes of operation"
   echo "-------------------------------------"
   echo "[1] INPUTFILE   TARGET_DURATION"
   echo "TARGET_DURATION may be <seconds.milliseconds> or <samples>s"
   echo "e.g. $0 abc.wav 4.58"
   echo "to fold at 4 seconds 58 milliseconds"
   echo ""
   echo "e.g. $0 abc.wav 4895832s"
   echo "to fold at sample 4895832. Note the s after the sample number."
   echo ""
   echo "[2] INPUTFILE   BEATS_PER_MINUTE   NUMBER_OF_BEATS"
   echo "e.g. $0 abc.wav 130 8"
   echo "to fold at 8 beats, given that the input file has 130 beats/minute"
   echo ""
   exit 1;
fi
name=$1

if [ $# -eq 3 ]; then
   srate=$(soxi -r $1);
   if [ -n "$srate" ]; then
      dur=$(echo "" | awk '{a = sprintf("%.2f\n", srate*beats*60/bpm); if(a ~ /\.[56789]/){a = int(a); a++}else{a = int(a)} print a}' srate=$srate bpm=$2 beats=$3)s;
   else
      echo "Could not get samplerate of $name";
      exit 1;
   fi
else
   dur=$2
fi

echo "DURATION: $dur";

name_start=$(echo $name|awk -F'.' '{print $1}')
name_end=$(echo $name|awk -F'.' '{print $2}')

if echo $dur | grep -q '^[0-9]\+s$'; then
   insize=$(soxi -s $name 2> /dev/null);
   dur=$(echo $dur | sed -e 's/s$//');
   suffix='s'
elif echo $dur | grep -q '^[0-9]\+\.\?[0-9]\+\?$'; then
   insize=$(soxi -D $name 2> /dev/null);
fi

if [ -z "$insize" ]; then
   echo "No valid audio input file. Could not determine length. Nothing to do.";
   exit 1;
fi
echo $dur $insize | awk '{if($1 >= $2 || $1 <= 0){ exit 1}}';
if [ $? -ne 0 ]; then
   echo "$dur >= size of input file: $insize. Or $dur <= 0. Nothing to do."; 
   exit 2;
fi
timez=$(echo $dur $insize | awk '{print int($2/$1)}');
c=0;
erg="";
while [ $c -le $timez ]; do
   a=$(echo $dur $c | awk '{print $1*$2}');
   b=$dur;
   if [ $c -eq $timez ]; then
      b='-0'
   fi
   sox $name /tmp/${name_start}_$c.$name_end trim $a$suffix $b$suffix;
   erg=$erg" -v 1.0 /tmp/${name_start}_$c.$name_end"; 
   let c++;
done
if [ -n "$erg" ]; then
   sox -m $erg /tmp/${name_start}_out.$name_end;
   rm $(echo $erg | sed -e 's/-v 1.0//g');

   echo "Output is /tmp/${name_start}_out.$name_end"
   printf "Duration: "; soxi -D /tmp/${name_start}_out.$name_end;
   printf "#Samples: "; soxi -s /tmp/${name_start}_out.$name_end;
fi

START ESCWYP MUSIC VIDEOS



{
   if($#ARGV >= 1){
      my $v = shift;
      my $sr = shift;
      if($v =~ /^\d+$/ && $v > 1 && $sr =~ /^\d+$/ && $sr > 1){
         my $preci_seconds = 0.0001;
         my $preci_samples = sprintf("%.0f", $sr*$preci_seconds);
         print "_____ $v _____\n";
         my %a;
         for(my $i=2; !$a{$i}; $i++){
            my $r;
            if($v % $i == 0){
               $r = $v/$i;
            }else{
               $r = sprintf("%.0f", $v/$i);
            }
            if($a{$r}){
               last;
            }
            $a{$i} = $r;
            if(($v - $r*$i) <= $preci_samples && (-$v + $r*$i) <= $preci_samples){
               print "$i $a{$i} ", ($v - $r*$i), " $preci_samples\n";
            }
         }
      }else{
         print "Parameter $v AND/OR $sr are not a Numbers > 1.\n";
         exit 1;
      }
   }else{
      print "Please give Number of samples AND Samplerate.\n";
      exit 1;
   }
}

/usr/lib/pkgconfig/ldns.pc
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: ldns
Description: Library for DNS programming
URL: http://www.nlnetlabs.nl/projects/ldns
Version: 1.7.1
Requires: 
Libs: -L${libdir} -lldns
Libs.private: -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now
Cflags: -I${includedir}