Sponsored Content
Top Forums Programming My first PERL incarnation... Audio Oscillograph Post 303038632 by wisecracker on Monday 9th of September 2019 08:55:11 AM
Old 09-09-2019
Hi fellas and dames...
(Apologies for any typos, etc...)

I have not been idle on this but I am on holiday/vacation and I only have limited www access AND Linux Mint 19, current updates as of 06-09-2019. My OSX machine is safely at home.
Well needless to say, what works in OSX 10.14.3 and the Perl version it possesses didn't work in Linux Mint and its Perl version. ;o/

As I am still out here on holiday I have released this Linux Mint 19 version so far. I have no idea what needs compromising until I get home and test on OSX 10.14.3.
"DSP", '/dev/dsp/' and "ALSA", 'arecord' now work as planned, 'RESTART' is now a complete 'RESET'.
This line in the 'RESET' section, exit(system("$abs_path")); is way cool, and the 'Title Bar' now reads "Perl Audio Oscillograph.".
I am NOT taking this fun project to the same limits as AudioScope.sh at all, just enough to whet peoples appetites and better it or reconstruct/translate the idea completely to another language.
There are lots of other changes that are in there too.
When I return home I will let you all know about how OSX 10.14.3 and 'QT' reacts to these changes.
Bizarre as it might sound I could NOT get Linux Mint's Perl version to recognise the 'current directory' as the valid default, so PATHs are now absolute.
As I have NOT done the drawing routine yet this is a low level whistle into the internal microphone using the ALSA capture mode.
Code:
#!/usr/bin/perl

# #########################################################
# Audio Oscillograph, (uncalibrated AF Oscilloscope).
# Oscillograph.pl
#
# This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level
# (with 2 registered patches, see perl -V for more detail)
# Developed on a[n] MBP, OSX Mojave, Version 10.14.3!
# NOT tested on OSX greater than version 10.14.3!
#
# This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi
# (with 67 registered patches, see perl -V for more detail)
# Co-developed on Linux Mint 19, full Mint 19 updates as of 06-09-2019!
#
# Thanks to drl and MadeInGermany for further info.

# #########################################################
# Needed!
use warnings;
use strict;
use Cwd 'abs_path';

print "\033]0;Perl Audio Oscillograph.\007";

# #########################################################
# Global variables...
my $abs_path=abs_path('Oscillograph.config');
my $blankline="                                                                                ";
my $horiz=9;
my $vert=9;
my $plot="";
my @hexdump_array=();
our $capture_mode="DEMO";
our $mode=" DEMO  ";

# #########################################################
# Make sure a bare config file exists in the current directory.
unless(-e "$abs_path")
{
    my $config='$capture_mode="DEMO";
$mode=" DEMO  ";
1;
';
    open(my $config_file, '>', "$abs_path");
    print $config_file "$config";
    close($config_file);
}

# #########################################################
# Source config file[s] as required.
require "$abs_path";

# #########################################################
# Clear the screen and reset the terminal.
sub clrscn
{
    print "\033[0m\033c\033[2J\033[H";
}

# #########################################################
# Basic command processing...
sub commands
{
    print "\033[23;1f\033[0m$blankline";
    print "\033[23;1f\033[0mEnter 'QUIT'<CR> to quit or <CR> to [Re-]Run:- ";
    my $command=<STDIN>;
    chomp $command;
    if($command eq 'QUIT')
    {
        my $config='$capture_mode="'."$capture_mode".'";
$mode="'."$mode".'";
1;
';
        open(my $config_file, '>', "$abs_path");
        print $config_file "$config";
        close($config_file);
        &clrscn();
        print "Saved Oscillograph.config and resetting the terminal to the default state.\n";
        exit(0);
    }
    if($command eq 'DEMO')
    {
        $capture_mode='DEMO';
        $mode=' DEMO  ';
    }
    if($command eq 'DSP')
    {
        $capture_mode='DSP';
        $mode='  DSP  ';
    }
    if($command eq 'ALSA')
    {
        $capture_mode='ALSA';
        $mode=' ALSA  ';
    }
    if($command eq 'QT')
    {
        $capture_mode='QT';
        $mode=' QEMAC ';
        &QT_script();
        system("/bin/sh", "/tmp/QT.scpt");
    }
    if($command eq 'RESET')
    {
        &clrscn();
        # Thanks MaedInGermany for the line below.
        unlink("$abs_path") or die "Cannot delete Oscillograph.config!";
        $abs_path=abs_path('Oscillograph.pl');
        exit(system("$abs_path"));
    }
    if($command eq 'TEST')
    {
        $capture_mode='TEST';
        $mode=' TEST  ';
        # Use ASCII characters ONLY for DC offset test waveform, exactly 1KHz, 1mS/DIV timebase speed.
        #
        # Sinewave...
        # my $waveform="Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -";
        #
        # Squarewave...
        my $waveform="    ~~~~    ~~~~    ~~~~    ~~~~    ~~~~    ~~~~    ~~~~    ~~~~";
        open(my $waveform_file, '>', '/tmp/waveform.raw');
        print $waveform_file "$waveform";
        close($waveform_file);
    }
    print "\033[23;1f\033[0m$blankline";
    print "\033[23;1f\033[0mEnter 'QUIT'<CR> to quit or <CR> to [Re-]Run:- ";    
}

# #########################################################
# This generates a shell script that runs an applescript to access QuickTime Player.
sub QT_script
{
my $AppleScript='/usr/bin/osascript << AppleSampler
    tell application "QuickTime Player"
        activate
        set savePath to "Macintosh HD:tmp:Untitled.m4a"
        set recording to new audio recording
        set visible of front window to false
        delay 2
        start recording
        delay 1
        stop recording
        export document "Untitled" in file savePath using settings preset "Audio Only"
        close (every document whose name contains "Untitled") saving no
        tell application "System Events" to click menu item "Hide Export Progress" of menu "Window" of menu bar 1 of process "QuickTime Player"
        delay 1
        quit
    end tell
AppleSampler
';
open(my $QuickTime, '>', '/tmp/QT.scpt');
print $QuickTime "$AppleScript";
close($QuickTime);
}

# #########################################################
# Screen DISPLAY setup function. For a terminal size of 80 x 24.
sub display
{
    # Set foreground and background graticule colours and foreground and background other window colours.
    print "\033[H\033[0;36;44m       +-------+-------+-------+---[\033[1;37;44mDISPLAY\033[0;36;44m]---+-------+-------+--------+       
       |       |       |       |       +       |       |       |        | \033[0;31;44mMAX\033[0;36;44m   
       |       |       |       |       +       |       |       |        |       
       |       |       |       |       +       |       |       |        |       
   \033[0;31;44m+ve\033[0;36;44m +-------+-------+-------+-------+-------+-------+-------+--------+       
       |       |       |       |       +       |       |       |        |       
       |       |       |       |       +       |       |       |        |       
       |       |       |       |       +       |       |       |        |       
     \033[1;32;44m0\033[0;36;44m +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--+ \033[1;32;44mREF\033[0;36;44m   
       |       |       |       |       +       |       |       |        |       
       |       |       |       |       +       |       |       |        |       
       |       |       |       |       +       |       |       |        |       
   \033[0;30;44m-ve\033[0;36;44m +-------+-------+-------+-------+-------+-------+-------+--------+       
       |       |       |       |       +       |       |       |        |       
       |       |       |       |       +       |       |       |        |       
       |       |       |       |       +       |       |       |        |       
       |       |       |       |       +       |       |       |        | \033[0;30;44mMIN\033[0;36;44m   
       +-------+-------+-----[\033[1;37;44mAUDIO  OSCILLOGRAPH\033[0;36;44m]-----+-------+--------+       \033[0m

Commands available: QUIT, DEMO, DSP, ALSA, QT, RESET and TEST.
$blankline
$blankline
$blankline\033[23;1f\033[0mEnter 'QUIT'<CR> to quit or <CR> to [Re-]Run:- 
   \033[0;36m\$VER: Audio_Oscillograph._Author_B.Walker,_G0LCU_courtesy_of_www.unix.com.\033[0m   ";
}

# #########################################################
# Capture methods, '/dev/urandom', '/dev/dsp', '/usr/bin/arecord' and 'QuickTime Player'.
sub capture
{
    if("$capture_mode" eq 'DEMO')
    {
        # DEMO mode that requires no HW access, working as of 27-06-2019.
        system("/bin/dd if=/dev/urandom of=/tmp/waveform.raw bs=1 count=64 > /dev/null 2>&1");
        sleep(1);
    }
    if($capture_mode eq 'DSP')
    {
        # DSP mode for the likes of old Linux boxes and CygWin, (OSS or PulseAudio), working as of 06-09-2019.
        system("/bin/dd if=/dev/dsp of=/tmp/waveform.bin bs=1 count=8000 > /dev/null 2>&1");
        system("/bin/dd if=/tmp/waveform.bin of=/tmp/waveform.raw skip=5120 bs=1 count=64 > /dev/null 2>&1");
    }
    if($capture_mode eq 'ALSA')
    {
        # ALSA mode for Linux boxes that have 'arecord', working as of 06-09-2019.
        system("/usr/bin/arecord -d 1 -c 1 -f U8 -r 8000 -t wav /tmp/waveform.wav > /dev/null 2>&1");
        system("/bin/dd if=/tmp/waveform.wav of=/tmp/waveform.raw skip=5120 bs=1 count=64 > /dev/null 2>&1");
    }
    if($capture_mode eq 'QT')
    {
        # A crucifyingly SSLLOOWW mode for Apple, OSX Mojave.
        # Working as of 28-08-2019.
        system("/bin/sh", "/tmp/QT.scpt");
        system("/usr/bin/afconvert -f WAVE -c 1 -d UI8\@8000 /tmp/Untitled.m4a /tmp/waveform.wav");
        system("dd if=/tmp/waveform.wav of=/tmp/waveform.raw skip=5120 bs=1 count=64 > /dev/null 2>&1");
    }
    if($capture_mode eq 'TEST')
    {
        # Use ASCII characters ONLY for offset waveform, exactly 1KHz.
        # Sinewave...
        # my $waveform="Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -Oq~qO- -";
        # Squarewave...
        my $waveform="    ~~~~    ~~~~    ~~~~    ~~~~    ~~~~    ~~~~    ~~~~    ~~~~";
        open(my $waveform_file, '>', '/tmp/waveform.raw');
        print $waveform_file "$waveform";
        close($waveform_file);
    }
    @hexdump_array=`/usr/bin/hexdump -v -e '1/1 "%u\n"' /tmp/waveform.raw`;
}

# #########################################################
# Plot the points in the terminal window.
sub plotter
{
    for($horiz=9; $horiz<=72; $horiz=$horiz+1)
    {
        # Simulate an 8 bit grab and divide by 16 to give 4 bit depth.
        # Add offset of 2 to allow for missing the top graticule line.
        chomp(${hexdump_array[($horiz-9)]});
        $vert="${hexdump_array[($horiz-9)]}";
        if($capture_mode eq 'TEST')
        {
            $vert=$vert+50;
        }
        if($vert == 127)
        {
            # This allows for centreline 0, zero, AC voltage bit error, (noise).
            $vert=128;
        }
        $vert=int(($vert/16)+2);
        # invert the signal. I'll let you work out why...
        $vert=(19-$vert);
        $plot="\033["."$vert".";"."$horiz"."f\033[1;37;44m*";
        print "$plot";
    }
}

# #########################################################
# Initialisation and main loop.
&clrscn();
&display();
if($capture_mode eq 'QT')
{
    # Initialise Quicktime Player when 'QT' is selected.
    &QT_script();
    system("/bin/sh", "/tmp/QT.scpt");
}

while(1)
{
    &capture();
    &display();
    &plotter();
    &commands();
    print "\033[1;37f\033[1;32;44m$mode\033[23;48f\033[0;36;44m";
 }

Have fun ripping it apart...
My first PERL incarnation...  Audio Oscillograph-alsa-whstle_cpaturepng
This User Gave Thanks to wisecracker For This Post:
 

6 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Audio HELP Please!

I know about next to nothing, and I am trying to play an audio file on HP-UX 10.2 (743i) every time I try to use the ‘/opt/audio/bin/AudioCP &' command I get an error message that says “Unable to access audio hardware.” I checked the man page on audio, and both Aserver and rpcd are running. Am I... (4 Replies)
Discussion started by: Matt
4 Replies

2. UNIX for Dummies Questions & Answers

playing audio

Hi all, I play audio through the web browser and it plays ok on windows. When i try the same thing on unix i get the error message "sh: /usr/local/bin/sox: not found. I've tried 'locate sox' and can't seem to find it. Is there some way I can change the browser settings so they play the audio... (3 Replies)
Discussion started by: molli_81
3 Replies

3. UNIX for Advanced & Expert Users

Audio-Text in Unix

Hello all, Could anyone help to me :as to how can i record an audio file in unix and convert it into a text . Or anything with audio recording to get started would help . thanks, (3 Replies)
Discussion started by: phantom308
3 Replies

4. UNIX for Dummies Questions & Answers

Audio Problem

Hi I recently installed BT 5 Gnome on my Asus laptop running on Intel HD graphics. I've been having problems with audio, at first I experienced the basic problem that was solved by adding pulse audio to the starting programs. This allowed me to open Sound preferences but I still have no sound.... (0 Replies)
Discussion started by: edreeso
0 Replies

5. Slackware

Problems with audio recording in Audacity 2.0.5. Slackware64 14.1; Intel HD Audio.

I'm trying to record audio using Audacity 2.0.5 installed from SlackBuilds. My system is 64-bit Slackware 14.1 and a sound card is Intel HD Audio. I didn't change my sound system to OSS. (Default sound system in Slackware 14.1 is ALSA, isn't it?) First, I set Internal Microphone slider in KMix... (2 Replies)
Discussion started by: qzxcvbnm
2 Replies

6. OS X (Apple)

An Audio Function Generator...

Ok guys, gals and geeks... As from today I am starting to learn awk in earnest doing something totally different. I am going to create a pseudo-Audio_Function Generator centred around OSX 10.11.x minimum. The code below is a tester to see what the possibilities are. All waveforms will be... (11 Replies)
Discussion started by: wisecracker
11 Replies
All times are GMT -4. The time now is 05:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy