AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

iTunes - Voice Control

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
trueski



Joined: 08 Apr 2008
Posts: 32

PostPosted: Fri Aug 08, 2008 6:52 am    Post subject: iTunes - Voice Control Reply with quote

Code's a little messy, help cleaning it up / fixing performance issues / recommendations would be appreciated.

Currently using with Vista

I put an audio file named, AcceptCommand.wav, in the script directory that plays a sound to confirm command was recognized

be sure to download COM.ahk
http://www.autohotkey.com/forum/topic22923.html

To Import your music library from iTunes,

Open iTunes -> File -> Export

Export a .txt file.

Run the script, right click on the taskbar icon and choose import iTunes Library, find the file that was exported.


Code:




#Persistent
OnExit, CleanUp
#Include COM.ahk
#SingleInstance Force
DetectHiddenWindows,On


;----------------- { Load / Save Settings } -------------------;
ReadSettings(Key)
  {
    IniRead, OutputVar, settings.ini, BasicSettings, %Key%
    Return, OutputVar
  }



SaveSettings(Value, Key)
  {
    IniWrite, %Value%, settings.ini, BasicSettings, %Key%
    Return
  }

;--------------------------------------------------------------;

GetFolder(var)
  {
    StringGetPos, varPos, var, \, R
    StringLeft, var, var, %varPos%
    Return Var
  }



;--------------------------------------------------------------;

MyName := ReadSettings("MyName")

SuccessVerification := ReadSettings("SuccessVerification")

YourName := ReadSettings("YourName")

LastFoundML := ReadSettings("LastFoundML")

LastFoundMLDate := ReadSettings("LastFoundMLDate")

AutoUpdateML := ReadSettings("AutoUpdateML")

;--------------------------------------------------------------;




;---------------------- { Remove Symbols } ---------------------;
varize(var)

   chars = .,<>:;'"/|\(){}=-+!`%^*~``
   loop, parse, chars,
      stringreplace,var,var,%A_loopfield%,,a

      stringreplace,var,var,&,and,a
   return var
}
;---------------------------------------------------------------;

;---------------------- { Tray Menu } ---------------------;
menu, tray, add ; separator
Menu, tray, add, Import iTunes Library, ImportiTunesLibrary
;----------------------------------------------------------;

IfEqual, AutoUpdateML, 1
  {
    FileGetTime, ItunesExportMod, %LastFoundML%
    IfLess, LastFoundMLDate, %ItunesExportMod%
      {
         SilentUpdate := 1
         GoSub, ImportiTunesLibrary
         SilentUpdate := 0
      }
   }














;----------------------------------------------------------;
COM_Init()
pspeaker := COM_ActiveXObject("SAPI.SpVoice")
plistener:= COM_CreateObject("SAPI.SpSharedRecognizer")
pcontext := COM_Invoke(plistener, "CreateRecoContext")
pgrammar := COM_Invoke(pcontext , "CreateGrammar")
COM_Invoke(pgrammar, "DictationSetState", 0)
prules := COM_Invoke(pgrammar, "Rules")
prulec := COM_Invoke(prules, "Add", "wordsRule", 0x1|0x20)
COM_Invoke(prulec, "Clear")
pstate := COM_Invoke(prulec, "InitialState")

; Add here the words to be recognized!
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "One")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Two")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Three")

COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Play Next Song")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Play Previous Song")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Music Volume Up")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Music Volume Down")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Pause Song")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Play Song")







;------------------ { Play Song } ------------------;
Loop, Read, MusicLibrary.dat
  {
    StringSplit, MusicLibLine, A_LoopReadLine, %A_Tab%
    PlaySongPhrase = Play song %MusicLibLine1% by %MusicLibLine2%
    COM_Invoke(pstate, "AddWordTransition", "+" . 0, PlaySongPhrase)
  }
;---------------------------------------------------;






;;
COM_Invoke(prules, "Commit")
COM_Invoke(pgrammar, "CmdSetRuleState", "wordsRule", 1)
COM_Invoke(prules, "Commit")
pevent := COM_ConnectObject(pcontext, "On")
Return

CleanUp:
COM_Release(pevent)
COM_Release(pstate)
COM_Release(prulec)
COM_Release(prules)
COM_Release(pgrammar)
COM_Release(pcontext)
COM_Release(pspeaker)
COM_Release(plistener)
COM_Term()
ExitApp

OnRecognition(prms, this)
{
   Global   pspeaker

   presult := COM_DispGetParam(prms, 3, 9)
   pphrase := COM_Invoke(presult, "PhraseInfo")
   sText   := COM_Invoke(pphrase, "GetText")
   COM_Release(pphrase)
   SoundPlay, AcceptCommand.wav
;------------------ { Play Song } ------------------;

   IfInString, sText, Play song
    {
      StringTrimLeft, SongName, sText, 10
      StringGetPos, SubtractRight, SongName, by
      SubtractRight -= 1
      StringLeft, SongName, SongName, %SubtractRight%



      StringLen, SongNameLen, SongName
      TrimLeft := SongNameLen + 14
      StringTrimLeft, ArtistName, sText, %TrimLeft%


     
     
      Loop, Read, MusicLibrary.dat
        {
          StringSplit, MusicLibLine, A_LoopReadLine, %A_Tab%
            ifEqual, SongName, %MusicLibLine1%
              ifEqual, ArtistName, %MusicLibLine2%
                  {
                    Run, "C:\Program Files\iTunes\iTunes.exe" "%MusicLibLine3%"
                    break
                   }
        }
    }
;---------------------------------------------------;


IfEqual, sText, Play Next Song
  ControlSend, ahk_parent, ^{right}, iTunes ahk_class iTunes

IfEqual, sText, Play Previous Song
  ControlSend, ahk_parent, ^{left}, iTunes ahk_class iTunes

IfEqual, sText, Play Song
  ControlSend, ahk_parent, {space}, iTunes ahk_class iTunes

IfEqual, sText, Pause Song
  ControlSend, ahk_parent, {space}, iTunes ahk_class iTunes

IfEqual, sText, Music Volume Up
  ControlSend, ahk_parent, ^{up}, iTunes ahk_class iTunes

IfEqual, sText, Music Volume Down
  ControlSend, ahk_parent, ^{down}, iTunes ahk_class iTunes



;COM_Invoke(pspeaker, "Speak", "This will be spoken")



}
Return

ImportiTunesLibrary:



IfNotEqual, SilentUpdate, 1
  {
    LastFoundML := ReadSettings("LastFoundML")
    IfExist, %LastFoundML%
      StartFolderNav := GetFolder(LastFoundML)

    Else
      StartFolderNav = %A_Desktop%

    FileSelectFile, ItunesExport,, %StartFolderNav%, Select text file exported from iTunes, *.txt
      IfEqual, ItunesExport,
        Return
  }

IfEqual, SilentUpdate, 1
  ItunesExport = %LastFoundML%

FileDelete, MusicLibrary.dat

Loop, Read, %ItunesExport%
  NumberofLines = %A_Index%

FileGetTime, ItunesExportMod, %ItunesExport%

NumberofLines -= 2
 

Gui, Add, Progress, x12 y12 w260 h20 vImportProgress Range0-%NumberofLines% -Smooth, 0
Gui, Add, Text, x12 y37, Importing iTunes Library...

Gui, Show, h60 w288, Importing iTunes Library
Gui, +alwaysonTop -sysmenu



Loop, Read, %ItunesExport%
  {
   
    StringSplit, CurrentLine, A_LoopReadLine, %A_Tab%
    CurrentLine1 := varize(CurrentLine1)
    CurrentLine2 := varize(CurrentLine2)

    IfNotEqual, A_Index, 1
      {
        IfEqual, A_Index, 2
          FileAppend, %CurrentLine1%%A_Tab%%CurrentLine2%%A_Tab%%CurrentLine27%, MusicLibrary.dat
       
        Else
          {
            IfNotEqual, A_LoopReadLine,
              {
                IfEqual, CurrentLine27,
                  FileAppend, `n%CurrentLine1%%A_Tab%%CurrentLine2%%A_Tab%%CurrentLine28%, MusicLibrary.dat

                Else
                  FileAppend, `n%CurrentLine1%%A_Tab%%CurrentLine2%%A_Tab%%CurrentLine27%, MusicLibrary.dat
              }

            GuiControl,, ImportProgress, +1
          }
      }
  }

Gui, Destroy

SaveSettings(ItunesExport, "LastFoundML")
SaveSettings(ItunesExportMod, "LastFoundMLDate")

IfExist, MusicLibrary.dat
  MsgBox, 4160, Import Complete!, iTunes Library imported successfully!, 5

Else
  MsgBox, 4144, Error, An error occured.`n`niTunes Library was not imported successfully!, 5
Return






Here's the settings.ini file..
Code:

[BasicSettings]

MyName=Computer

SuccessVerification=1

YourName=Trueski

LastFoundML=C:\Users\Trueski\Desktop\Music.txt

LastFoundMLDate=20080807224210

AutoUpdateML=1

_________________
-Trueski-
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1397

PostPosted: Fri Aug 08, 2008 9:56 am    Post subject: Reply with quote

Well, you can control iTunes via COM too. For example, this line:
Code:
Run, "C:\Program Files\iTunes\iTunes.exe" "%MusicLibLine3%"

can be translated like
Code:
If Not iTunes
       iTunes := COM_CreateObject("iTunes.Application")
COM_Invoke(iTunes, "PlayFile", MusicLibLine3)

Other commands may be
Code:
COM_Invoke(iTunes, "PreviousTrack")
COM_Invoke(iTunes, "NextTrack")
COM_Invoke(iTunes, "Play")
COM_Invoke(iTunes, "Pause")
COM_Invoke(iTunes, "Resume")
COM_Invoke(iTunes, "Stop")
COM_Invoke(iTunes, "SoundVolume", COM_Invoke(iTunes, "SoundVolume")+10)
COM_Invoke(iTunes, "SoundVolume", COM_Invoke(iTunes, "SoundVolume")-10)
Back to top
View user's profile Send private message
trueski



Joined: 08 Apr 2008
Posts: 32

PostPosted: Fri Aug 08, 2008 6:38 pm    Post subject: Reply with quote

all right, thanks, will make changes later tonight.. I also need to change the way the music library is accessed. Right now it's slow and CPU intensive. I'll work on it
_________________
-Trueski-
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group