Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Recursively Scan a Directory
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




PostPosted: Fri Apr 08, 2011 10:45 pm   Post subject: Recursively Scan a Directory

I've been trying to get this to work for years and finally got it. I deleted tons of extra code just to get to the bare bones, but when I have time I'll develop it further to be a full blown, lightweight media player.

Turing:


View.Set ("text")                                   % Start in text mode
var defMusDir : string                              % Root music directory
var searchExt := ".mp3"                             % Look for MP3's
const lengthExt := (length (searchExt) - 1)         % Length of extension
var mp3Counter : int := 0                           % Count number of MP3's
var songFullPath : flexible array 1 .. 0 of string  % Obtain full path of MP3

procedure ScanDir (dir : string, switch : string)   % Scan directory for files
    var streamNumber : int                          % File stream
    var fileName : string                           % Hold file name
    streamNumber := Dir.Open (dir)                  % Open directory for output
    assert streamNumber > 0                         % Make sure stream is ready

    loop                                            % Main loop
        fileName := Dir.Get (streamNumber)          % Obtain filename from directory stream
        exit when fileName = "" or hasch            % Quit program when user presses a key or end of stream

        if (fileName not= ".") and (fileName not= "..") and length (fileName) > 4 then              % Makes sure we don't leave root and file is big enough for parsing
            if fileName (length (fileName) - 3 .. length (fileName)) = searchExt then               % Check if file is an MP3
                mp3Counter += 1                                                                     % Found an mp3!
                new songFullPath, mp3Counter                                                        % Resize array dynamically
                songFullPath (mp3Counter) := dir + fileName                                         % Save path + filename to array
            end if

            if Dir.Exists (dir + fileName) then                                                     % If a directory is found
                ScanDir (dir + fileName + "/", "calculate")                                         % Recursively look for more MP3s & directories within that folder
            end if

        end if
    end loop
    Dir.Close (streamNumber)                        % Close the stream
end ScanDir

put "Please enter a music directory: " ..           % Display message to user
get defMusDir : *                                   % User inputs directory to scan
if defMusDir (length (defMusDir)) not= "/" then     % Checks if user ended with a forward slash
    defMusDir += "/"                                % Appends forward slash to prevent error
end if
ScanDir (defMusDir, "calculate")                    % Calculate number of MP3's
const numOfMP3 := mp3Counter                        % Store number of MP3's
ScanDir (defMusDir, "store")                        % Store all MP3's
   
var randomSong := songFullPath (Rand.Int (1, numOfMP3)) % Selects a random song
put "Playing ", randomSong                              % Display name of song selected
Music.PlayFileReturn (randomSong)                       % Plays that song
Sponsor
Sponsor
Sponsor
sponsor
Prince Pwn




PostPosted: Sat Apr 09, 2011 7:40 am   Post subject: RE:Recursively Scan a Directory

I don't know why but sometimes the program wont play a song, and sometimes I get "Assert condition is false". Darn =|

And sometimes it won't play a song. I've determines it's because it's over a certain filesize. Turing won't play music files bigger than like roughly 5MB but will play songs under.
Tony




PostPosted: Sat Apr 09, 2011 11:52 am   Post subject: RE:Recursively Scan a Directory

I don't see the switch : string argument doing anything, so wouldn't
code:

ScanDir (defMusDir, "calculate")
const numOfMP3 := mp3Counter
ScanDir (defMusDir, "store")

just scan everything twice? You wouldn't really notice it, as numOfMP3 will make random choices just from the first half, but if you check the size of songFullPath, it should be double of that.


I didn't know about Turing's music file size limitations, but I wouldn't be entirely surprised if that's the case.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Prince Pwn




PostPosted: Sat Apr 09, 2011 4:56 pm   Post subject: RE:Recursively Scan a Directory

Ah good catch on the switch. I left that in there from when I was trying another method of collecting the MP3's, but then I managed to do it using a dynamically sized array.

I pretty much tested playing diff files with Music.PlayFileReturn, and I never get an error and Error.LastMsg is always empty, the only thing I've noticed is files larger than like 5MB do not play. I'll test this further.
Prince Pwn




PostPosted: Sat Apr 09, 2011 5:23 pm   Post subject: RE:Recursively Scan a Directory

Ah I found the error by switching from Music.PlayFileReturn to Music.PlayFile. Error.LastMsg shows

A problem occured in initializing MCI. Now does anyone know what that means? I'm Googling meanwhile but coming up with nothing good. Anyone know how Turing plays music, what Windows API it hooks into or anything?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: