'===========================================================================
' Subject: CD RIPPER FOR LIBERTY BASIC        Date: 08-07-99 (14:59)       
'  Author: Michael T. Rankin                  Code: LB                     
'  Origin: walkerby@frontiernet.net         Packet: LIBERTY.ABC
'===========================================================================
' *************************************************************
' *                      MrRipper v0.5                        *
' *          Pasted together by Michael T. Rankin             *
' *                 walkerby@frontiernet.net                  *
' *                      copyleft 1999                        *
' *                                                           *
' *   Portions of this code come from Brosco, Carl Gundel's   *
' *   tutorials and Alyce Watson. I may have cut and pasted   *
' *   a line or ten from someone else, IF I did THEN give     *
' *   them credit also. Please contact the respective persons *
' *   if you wish to use any portion of their code. If you    *
' *   modify this program you must release the source,        *
' *   including modifications, if you wish to distribute it.  *
' *                                                           *
' *************************************************************

    nomainwin
    action=0
    notchk=0
    DIM tracks$(20)
    DIM quality$(16)
    DIM wavsize(16)

' Setting up the arrays.
    quality$(0)=" 8000 Hz,    8 Bit, Mono"
    quality$(1)=" 8000 Hz,    8 Bit, Stereo"
    quality$(2)=" 8000 Hz,  16 Bit, Mono"
    quality$(3)=" 8000 Hz,  16 Bit, Stereo"
    quality$(4)="11025 Hz,   8 Bit, Mono"
    quality$(5)="11025 Hz,   8 Bit, Stereo"
    quality$(6)="11025 Hz, 16 Bit, Mono"
    quality$(7)="11025 Hz, 16 Bit, Stereo"
    quality$(8)="22050 Hz,   8 Bit, Mono"
    quality$(9)="22050 Hz,   8 Bit, Stereo"
    quality$(10)="22050 Hz, 16 Bit, Mono"
    quality$(11)="22050 Hz, 16 Bit, Stereo"
    quality$(12)="44100 Hz,   8 Bit, Mono"
    quality$(13)="44100 Hz,   8 Bit, Stereo"
    quality$(14)="44100 Hz, 16 Bit, Mono"
    quality$(15)="44100 Hz, 16 Bit, Stereo"

    wavsize(1)=8
    wavsize(2)=16
    wavsize(3)=16
    wavsize(4)=31
    wavsize(5)=11
    wavsize(6)=22
    wavsize(7)=22
    wavsize(8)=43
    wavsize(9)=22
    wavsize(10)=43
    wavsize(11)=43
    wavsize(12)=86
    wavsize(13)=43
    wavsize(14)=86
    wavsize(15)=86
    wavsize(16)=172

    UpperLeftX=int((DisplayWidth-480)/2)
    UpperLeftY=int((DisplayHeight-270)/2)
    WindowWidth = 480
    WindowHeight = 270

    combobox #cd2wav.traxs, tracks$(, [calc.stuff], 22, 36, 48, 100
    combobox #cd2wav.quality, quality$(, [calc.stuff], 100, 36, 230, 100
    textbox #cd2wav.saveas, 350, 36, 100, 25
    button #cd2wav.eject, "Eject CD", [eject.cd], UL, 22, 196, 80, 25
    button #cd2wav.record, "Record", [record], UL, 142, 196, 80, 25
    button #cd2wav.refresh, "Refresh", [refresh], UL, 262, 196, 80, 25
    button #cd2wav.exit, "Exit", [xit], UL, 382, 196, 80, 25
    OPEN "LB CD Audio to Wav file" for graphics_nsb_nf as #cd2wav
    PRINT #cd2wav.quality, "selectindex 1"
    PRINT #cd2wav, "fill lightgray; backcolor lightgray"
    PRINT #cd2wav, "font MS_Sans_SerIF 7 16";
    PRINT #cd2wav.traxs, "font MS_Sans_SerIF 7 16";
    PRINT #cd2wav.quality, "font MS_Sans_SerIF 7 16";
    PRINT #cd2wav.saveas, "!font MS_Sans_SerIF 7 16";
    PRINT #cd2wav.eject, "!font Times_New_Roman_Bold 0 18"
    PRINT #cd2wav.record, "!font Times_New_Roman_Bold 0 18"
    PRINT #cd2wav.refresh, "!font Times_New_Roman_Bold 0 18"
    PRINT #cd2wav.exit, "!font Times_New_Roman_Bold 0 18"
    PRINT #cd2wav, "down ; color black ; backcolor lightgray"
    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; place 22 30"
    PRINT #cd2wav, "\Tracks:"
    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; place 100 30"
    PRINT #cd2wav, "\Quality of Recording:"
    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; place 350 30"
    PRINT #cd2wav, "\Save as:"
    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; place 22 80"
    PRINT #cd2wav, "\Recording Time of Song:"
    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; place 22 100"
    PRINT #cd2wav, "\Approximate size of wav file:"
    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; place 22 120"
    PRINT #cd2wav, "\Time Playing:"
    PRINT #cd2wav.saveas, "untitled"
    PRINT #cd2wav, "flush"
    PRINT #cd2wav, "trapclose [xit]"

' These lines just make sure the CD drive is ready
    OPEN "mmsystem.dll" for dll as #mm
    calldll #mm,"mciSENDString",_
    "stop cdaudio" as ptr,_
    0 as dword,0 as short,0 as short, r as long
    calldll #mm,"mciSENDString",_
    "close cdaudio" as ptr,_
    0 as dword,0 as short,0 as short, r as long
    calldll #mm,"mciSENDString",_
    "OPEN cdaudio" as ptr,_
    0 as dword,0 as short,0 as short, r as long

    goto [media.present]


[wait.loop]
    INPUT pause$
        goto [wait.loop]


[calc.stuff]
' Get the selections
    PRINT #cd2wav.traxs, "selection?"
    INPUT #cd2wav.traxs, selected$
    PRINT #cd2wav.quality, "selectionindex?"
    INPUT #cd2wav.quality, qualindex


' Set the time to milliseconds
    calldll #mm,"mciSENDString",_
    "set cdaudio time format ms" as ptr,_
    0 as dword,0 as short,0 as short, r as long

' Find the playing time of the selected track.
    reslt$ = space$(19)+chr$(0)
    mslen$=""
    size = 20
    a$="status cdaudio length track " + selected$

    calldll #mm,"mciSENDString",_
    a$ as ptr,_
    reslt$ as ptr,_
    size as short,_
    0 as short, r as long
    mslen$=left$(reslt$, instr(reslt$, chr$(0)) - 1)

' This gets the length of the track in standard time.
    calldll #mm,"mciSENDString",_
    "set cdaudio time format TMSF" as ptr,_
    0 as dword,0 as short,0 as short, r as long

    result$ = space$(19)+chr$(0)
    len1$=""
    size = 20
    a$="status cdaudio length track " + selected$

    calldll #mm,"mciSENDString",_
    a$ as ptr,_
    result$ as ptr,_
    size as short,_
    0 as short, r as long
    len1$=left$(result$, instr(result$, chr$(0)) - 1)

' This calculates how big the wav file will be.
    sizeowav = int(val(mslen$)*wavsize(qualindex))
    sizeowav$ = str$(sizeowav) + " bytes"


    PRINT #cd2wav, "font Times_New_Roman 0 18;"
    PRINT #cd2wav, "place 190 80"
    PRINT #cd2wav, Space$(100)
    PRINT #cd2wav, "|";len1$
    PRINT #cd2wav, "font Times_New_Roman 0 18;"
    PRINT #cd2wav, "place 215 100"
    PRINT #cd2wav, Space$(100)
    PRINT #cd2wav, "|";sizeowav$
    PRINT #cd2wav, "flush"
        goto [wait.loop]



[record]
' Make sure the name it is to be saved as is proper.
    PRINT #cd2wav.saveas, "!contents?"
    INPUT #cd2wav.saveas, saveas$
    IF LEN(saveas$) > 8 OR LEN(saveas$) < 1 THEN
       Notice "File Name Wrong Length" + chr$(13) +_
         "The file name must be a least 1 character, but no more than 8." + chr$(13) +_
            "Do not enter an extension."
        PRINT #cd2wav.saveas, ""
        PRINT #cd2wav.saveas, "!setfocus"
       goto [wait.loop]
    END IF
    SavStrng$ = "save capture "; saveas$; ".wav"

' This sets up the string to later use to play the correct track.
    IF VAL(selected$) = notrslt THEN
        TrxStrng$ = "play cdaudio from "; selected$
    ELSE
        TrxStrng$ = "play cdaudio from "; selected$; " to "; (VAL(selected$) + 1)
    END IF

' This sets up the string to later use to set the correct quality for recording.
    PRINT #cd2wav.quality, "selection?" : INPUT #cd2wav.quality, Qselection$
    AudioKHz$ = TRIM$(LEFT$(Qselection$, instr(Qselection$, ",")))
    AudioKHz = VAL(LEFT$(AudioKHz$, instr(AudioKHz$, " ")-1))
    AudioBits = VAL(TRIM$(MID$(Qselection$, instr(Qselection$, ",")+1,_
    instr(Qselection$, "Bit")-instr(Qselection$, ",")-1)))
    IF instr(Qselection$, "Mono") >1 THEN AudioChannels = 1 Else AudioChannels = 2

    CapStrng$ = "set capture BitsPerSample "; AudioBits ;_
    " Channels "; AudioChannels ;_
    " samplespersec "; AudioKHz ;_
    " alignment "; ((AudioBits / 8) * AudioChannels) ;_
    " bytespersec "; (AudioKHz * AudioChannels * (AudioBits /8)) ;_
    " time format ms"

' This plays the correct track.
    calldll #mm,"mciSENDString",_
    TrxStrng$ as ptr,_
    0 as dword,_
    0 as short,_
    0 as short, r as long

' This tells the program we will be recording a wave file.
    calldll #mm,"mciSENDString",_
    "OPEN new type waveaudio alias capture" as ptr,_
    0 as dword,_
    0 as short,_
    0 as short, r as long

' This sets the qaulity of the recording.
    calldll #mm,"mciSENDString",_
    CapStrng$ as ptr,_
    0 as dword,_
    0 as short,_
    0 as short, r as long

' This tells it to start recording.
    calldll #mm,"mciSENDString",_
    "record capture" as ptr,_
    0 as dword,_
    0 as short,_
    0 as short, r as long

' This calls a subroutine to wait until the track is done playing.
    rectime = 0
    while val(mslen$) >= rectime
    gosub [timer]
    rectime = newTime - startTime
    wEND
    mins$ = "" : secs$ = "" : msecs = 0

[stop]
' This stops the CD player.
    calldll #mm,"mciSENDString",_
    "stop cdaudio" as ptr,_
    0 as dword,_
    0 as short,_
    0 as short, r as long

' This releases the CD player.
    calldll #mm,"mciSENDString",_
    "close cdaudio" as ptr,_
    0 as dword,_
    0 as short,_
    0 as short, r as long

' This is just a window to let you know it is saving the wav file to disk.
    UpperLeftX=int((DisplayWidth-185)/2)
    UpperLeftY=int((DisplayHeight-50)/2)
    WindowWidth = 185
    WindowHeight = 50
    OPEN "****Saving File****" for graphics_nsb_nf as #Save
    PRINT #Save, "fill lightgray; backcolor lightgray"
    PRINT #Save, "down ; color black ; backcolor lightgray"
    PRINT #Save, "font Times_New_Roman_Bold 0 18; place 30 20"
    PRINT #Save, "\Saving file to disk"
    PRINT #Save, "flush"

' This saves the recorded file to disk.
    calldll #mm,"mciSENDString",_
    SavStrng$ as ptr,_
    0 as dword,_
    0 as short,_
    0 as short, r as long

' This releases the file so other programs can access it.
    calldll #mm,"mciSENDString",_
    "close capture" as ptr,_
    0 as dword,_
    0 as short,_
    0 as short, r as long

    CLOSE #Save

        goto [wait.loop]


' You know what this does!
[eject.cd]
    calldll #mm,"mciSENDString",_
    "set cdaudio door OPEN" as ptr,_
    0 as dword,0 as short,0 as short, r as long

    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; color red ; place 22 140"
    PRINT #cd2wav, "\Press Refresh after changing the CD!"
    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; color black"
    PRINT #cd2wav, "flush"
        goto [wait.loop]


[refresh]
' Resets everything after thee CD has been changed.
    PRINT #cd2wav, "font Times_New_Roman_Bold 0 18; place 22 140"
    PRINT #cd2wav, "|";SPACE$(100)
    GOTO [media.present]


' You know this one too!
[xit]
    CLOSE #mm
    CLOSE #cd2wav
    END

'***** Sub Routines Start Here *****
' This gets the number of tracks on the CD.
[numoftracks]
    REDIM tracks$(20)
    notchk = 1
    result$ = space$(19)+chr$(0)
    size = 20

    calldll #mm,"mciSENDString",_
    "status cdaudio number of tracks" as ptr,_
    result$ as ptr,size as short,0 as short, r as long

    notrslt=VAL(left$(result$, instr(result$, chr$(0)) - 1))

' If it's an audio CD, I'm going to assume it has more than 1 track.
    IF notrslt < 2 THEN
      Notice "Not an Audio CD" + chr$(13) + "Please insert an audio CD."
        goto [media.present]
    END IF

' This seeds the array
    FOR TRCK = 1 TO notrslt
    tracks$(TRCK) = STR$(TRCK)
    NEXT TRCK

    PRINT #cd2wav.traxs, "reload"
    PRINT #cd2wav.traxs, "selectindex 1"
        goto [calc.stuff]



[media.present]
' This just checks to see if a CD is in the drive.
    result$ = space$(19)+chr$(0)
    size = 20
    a$="status cdaudio media present"
    calldll #mm,"mciSENDString",_
    a$ as ptr,_
    result$ as ptr,_
    size as short,_
    0 as short, r as long

    presence$=left$(result$, instr(result$, chr$(0)) - 1)
    IF presence$="false" THEN
      Notice "No CD Present" + chr$(13) + "Please insert an audio CD."
        goto [media.present]
    END IF

    GOTO [numoftracks]


[error_message]
' This is how you tell what the errors mean from the DLL call. (I don't use it in this program)
    result$ = space$(199)+chr$(0)
    size = 200
    calldll #mm,"mciGetErrorString",r as long, result$ as ptr,_
    size as short, result as bool

    profile$ = left$(result$, instr(result$, chr$(0)) - 1)
    notice profile$
    goto [wait.loop]


[timer]
' This tells us how much time has past since recording began.
    IF begin = 0 THEN
        OPEN "user" for dll as #user
            calldll #user, "GetTickCount",_
                    result as long
                startTime = result
            close #user
        begin = 1
    END IF

    OPEN "user" for dll as #user
    calldll #user, "GetTickCount",_
    result as long
    newTime = result
    close #user

    msecs = VAL(Right$(STR$(int((newTime-startTime)/10)),2))
    mins$ = STR$(int((newTime-startTime)/60000))
    secs$ = STR$(int((newTime-startTime)/1000)-(60*val(mins$)))
    IF LEN(secs$) < 2 THEN secs$ = "0" + secs$
    IF LEN(mins$) < 2 THEN mins$ = "0" + mins$
    PRINT #cd2wav, "font Times_New_Roman 0 18; color white ; backcolor black"
    PRINT #cd2wav, "place 115 120"
    PRINT #cd2wav, Space$(100)
    PRINT #cd2wav, "|";mins$;":";secs$;":";msecs

    return
