Skip to content

Commit 23392f7

Browse files
committed
Use executable-path to reliably find the contrib/ directory
1 parent c5f4ade commit 23392f7

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

src/PlaySound.hs

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import Control.Exception
99
import System.IO.Error
1010
import System.IO
1111
import System.Directory
12+
import System.FilePath
1213
import qualified Data.ByteString.Lazy as B
14+
import System.Environment.Executable
1315

1416

15-
players :: String -> [(FilePath, [String])]
16-
players fn =
17-
[ ("sox", ["-q", fn, "-d"])
18-
, ("./contrib/playmus", [fn])
17+
players :: FilePath -> FilePath -> [(FilePath, [String])]
18+
players myDir fn =
19+
[ ("sox", ["-q", fn, "-d"])
20+
, (myDir </> "contrib" </> "playmus", [fn])
1921
]
2022

2123
playSound :: B.ByteString -> IO ()
@@ -25,7 +27,9 @@ playSound content = do
2527
B.hPutStr h content
2628
hClose h
2729

28-
tryPrograms (players tmp) $ do
30+
(myDir,_) <- splitExecutablePath
31+
32+
tryPrograms (players myDir tmp) $ do
2933
putStrLn "Could not play audio file."
3034
putStrLn "Do you have \"sox\" installed?"
3135

src/TextToSpeech.hs

+13-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Control.Exception
1414
import System.IO.Error
1515
import System.Environment
1616
import System.Info (os)
17+
import System.Environment.Executable
1718

1819
import Language
1920

@@ -40,22 +41,22 @@ espeak :: Language -> FilePath -> String -> (String, [String])
4041
espeak lang tmp txt =
4142
("espeak", ["-v", l, "-w", tmp, "-s", "120", txt])
4243
where
43-
l = case lang of
44+
l = case lang of
4445
Language s -> s
4546

46-
espeak_contrib :: Language -> FilePath -> String -> (String, [String])
47-
espeak_contrib lang tmp txt =
48-
("./contrib/espeak", ["-v", l, "-w", tmp, "-s", "120", txt])
47+
espeak_contrib :: FilePath -> Language -> FilePath -> String -> (String, [String])
48+
espeak_contrib myDir lang tmp txt =
49+
(myDir </> "contrib" </> "espeak", ["-v", l, "-w", tmp, "-s", "120", txt])
4950
where
50-
l = case lang of
51+
l = case lang of
5152
Language s -> s
5253

5354

54-
engines :: Language -> FilePath -> String -> [(String, [String])]
55-
engines l ft txt =
55+
engines :: FilePath -> Language -> FilePath -> String -> [(String, [String])]
56+
engines myDir l ft txt =
5657
[ pico l ft txt
5758
, espeak l ft txt
58-
, espeak_contrib l ft txt
59+
, espeak_contrib myDir l ft txt
5960
]
6061

6162
oggenc :: FilePath -> FilePath -> (String, [String])
@@ -68,7 +69,7 @@ encoders :: FilePath -> FilePath -> [(String, [String])]
6869
encoders from to =
6970
[ oggenc from to
7071
, oggenc_contrib from to
71-
]
72+
]
7273

7374
tryPrograms [] e = e
7475
tryPrograms ((c,args):es) e = do
@@ -100,7 +101,9 @@ textToSpeech lang txt = do
100101
(tmp,h) <- openTempFile (takeDirectory fn) (takeBaseName fn <.> "wav")
101102
hClose h
102103

103-
tryPrograms (engines lang tmp txt) $ do
104+
(myDir,_) <- splitExecutablePath
105+
106+
tryPrograms (engines myDir lang tmp txt) $ do
104107
putStrLn "No suitable text-to-speech-engine found."
105108
putStrLn "Do you have libttspico-utils or espeak installed?"
106109

tttool.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ executable tttool
4949
KnownCodes,
5050
Types,
5151
Utils
52-
52+
5353
build-depends:
5454
base == 4.5.* || == 4.6.* || == 4.7.* || == 4.8.*,
5555
binary == 0.5.* || == 0.7.*,
5656
containers == 0.4.* || == 0.5.*,
5757
directory == 1.2.*,
58+
executable-path == 0.0.*,
5859
filepath == 1.3.* || == 1.4.*,
5960
template-haskell >= 2.7 && < 2.11,
6061

0 commit comments

Comments
 (0)