soundplay filename length limit?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

soundplay filename length limit?

10 Apr 2019, 00:56

I'm making a quiz and using the questions as the names of the wav files containing the answers. The first question won't play, I just discovered the second will:

Code: Select all

soundplay, Audio\What's the name of the wall that the Romans built to keep the Scots out of England.wav, wait
soundplay, Audio\What's the name of the wall.wav, wait
Is there a known limit on the length of a file name that will play? What's a better approach? The questions are stored in a text file, pulled into a variable using a read command and then the order is randomised. I suppose I could use some kind of unique code for each question rather than the question itself but I want to keep track of the actual questions.
Rohwedder
Posts: 2338
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: soundplay filename length limit?

10 Apr 2019, 04:08

Hallo,
here: Ahk 1.1.30.03 64bit, WIN_7 64bit

Code: Select all

soundplay, Audio\What's the name of the wall that the Romans built to keep the Scots out of England a.wav, wait
works, but

Code: Select all

soundplay, Audio\What's the name of the wall that the Romans built to keep the Scots out of England ab.wav, wait
not!
The full path of my working wav:
D:\Programme\Spezial-Autohotkeys\Audio\What's the name of the wall that the Romans built to keep the Scots out of England a.wav
contains 127 characters, seems to be the limit.
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: soundplay filename length limit?

10 Apr 2019, 17:58

Thanks for working that out. I'm using the same system. I've just noticed that if I use the mci functions I can use a total length of 248 characters not including the period and file extensions, so that will have to do.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: soundplay filename length limit?

10 Apr 2019, 19:01

You could try using the short-form path:

Code: Select all

q:: ;file get short-form path
vPath := A_ScriptFullPath
Loop, Files, % vPath, F
	vPathShort := A_LoopFileShortPath
MsgBox, % vPathShort
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: soundplay filename length limit?

11 Apr 2019, 22:25

Thanks for the suggestion but it didn't work, long file names still wouldn't play
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: soundplay filename length limit?

12 Apr 2019, 01:59

This worked for me. (The path was long, SoundPlay didn't work on the literal filename, but did work on the short-form name.)

Code: Select all

q:: ;play sound (handle longer filenames)
vPath := A_Desktop "\MyMp3.mp3"
Loop, Files, % vPath, F
	vPathShort := A_LoopFileShortPath
MsgBox, % vPathShort
SoundPlay, % vPathShort
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Rohwedder
Posts: 2338
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: soundplay filename length limit?

12 Apr 2019, 03:50

Hallo,
fantastic! This works:

Code: Select all

q:: ;play sound (handle longer filenames)
vPath =
(Join
Audio\What's the name of the wall that the Romans built to keep the Scots out of England.
 What's the name of the wall that the Romans built to keep the Scots out of England.wav
)
Loop, Files, % vPath, F
	vPathShort := A_LoopFileShortPath
SoundPlay, % vPathShort
return
The full path contains 209 characters.
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: soundplay filename length limit?

13 Apr 2019, 03:08

Not sure why it's not working for me. It would work with shorter file names but not longer ones.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: soundplay filename length limit?

13 Apr 2019, 03:31

It's possible that a short-form path will still be over 127 characters in length.

Here's the AHK v1.1.30.03 source code for SoundPlay from script2.cpp. In case it explains the path-length limit. It uses mciSendString.

Code: Select all

ResultType Line::SoundPlay(LPTSTR aFilespec, bool aSleepUntilDone)
{
	LPTSTR cp = omit_leading_whitespace(aFilespec);
	if (*cp == '*')
		return SetErrorLevelOrThrowBool(!MessageBeep(ATOU(cp + 1)));
		// ATOU() returns 0xFFFFFFFF for -1, which is relied upon to support the -1 sound.
	// See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_play.asp
	// for some documentation mciSendString() and related.
	TCHAR buf[MAX_PATH * 2]; // Allow room for filename and commands.
	mciSendString(_T("status ") SOUNDPLAY_ALIAS _T(" mode"), buf, _countof(buf), NULL);
	if (*buf) // "playing" or "stopped" (so close it before trying to re-open with a new aFilespec).
		mciSendString(_T("close ") SOUNDPLAY_ALIAS, NULL, 0, NULL);
	sntprintf(buf, _countof(buf), _T("open \"%s\" alias ") SOUNDPLAY_ALIAS, aFilespec);
	if (mciSendString(buf, NULL, 0, NULL)) // Failure.
		return SetErrorLevelOrThrow();
	g_SoundWasPlayed = true;  // For use by Script's destructor.
	if (mciSendString(_T("play ") SOUNDPLAY_ALIAS, NULL, 0, NULL)) // Failure.
		return SetErrorLevelOrThrow();
	// Otherwise, the sound is now playing.
	g_ErrorLevel->Assign(ERRORLEVEL_NONE);
	if (!aSleepUntilDone)
		return OK;
	// Otherwise, caller wants us to wait until the file is done playing.  To allow our app to remain
	// responsive during this time, use a loop that checks our message queue:
	// Older method: "mciSendString("play " SOUNDPLAY_ALIAS " wait", NULL, 0, NULL)"
	for (;;)
	{
		mciSendString(_T("status ") SOUNDPLAY_ALIAS _T(" mode"), buf, _countof(buf), NULL);
		if (!*buf) // Probably can't happen given the state we're in.
			break;
		if (!_tcscmp(buf, _T("stopped"))) // The sound is done playing.
		{
			mciSendString(_T("close ") SOUNDPLAY_ALIAS, NULL, 0, NULL);
			break;
		}
		// Sleep a little longer than normal because I'm not sure how much overhead
		// and CPU utilization the above incurs:
		MsgSleep(20);
	}
	return OK;
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Rohwedder
Posts: 2338
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: soundplay filename length limit?

13 Apr 2019, 03:53

Hallo,
my Win_7 and Windows Explorer is unable to name and open files whose full path length exceeds 259 characters.
Up to this limit SoundPlay, % vPathShort works here.
I've read that Windows_10 1803 has removed this limit, but Windows Explorer has been forgotten and still can't name and open these files.
It would be good if Autohotkey also removes this limit, with a little more care than Microsoft.
Last edited by Rohwedder on 13 Apr 2019, 04:02, edited 1 time in total.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: soundplay filename length limit?

13 Apr 2019, 04:00

Yes, there is the 259-char limit, but for this particular problem there appears to be a 127-char limit also.

Some notes/workarounds re. the 259-char limit:
long path support - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=47705
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 6551
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: soundplay filename length limit?

13 Apr 2019, 05:26

jeeswg wrote:
13 Apr 2019, 03:31
It's possible that a short-form path will still be over 127 characters in length.

Here's the AHK v1.1.30.03 source code for SoundPlay from script2.cpp. In case it explains the path-length limit. It uses mciSendString.

...

Code: Select all

	TCHAR buf[MAX_PATH * 2]; // Allow room for filename and commands.
Allocates a buffer of 520 TCHARs (1040 bytes).

Code: Select all

	sntprintf(buf, _countof(buf), _T("open \"%s\" alias ") SOUNDPLAY_ALIAS, aFilespec);
Builds a string of 6 (open \") + 8 (s\" alias) + 10 (#define SOUNDPLAY_ALIAS _T("AHK_PlayMe")) characters plus the length of the file path. I.e., a file path of MAX_PATH will always fit in the buffer. If there is a ~127 chars limit, it must be within the mciSendString() function.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: soundplay filename length limit?

13 Apr 2019, 05:33

Thanks just me, I noticed the buffer was big enough (twice MAX_PATH), suggesting a Winapi function being the problem.
This thread mentions the 127-char limit and possible solutions, one of which is setting the working directory, and using the name only, omitting the folder.
c++ - mciSendString won't play an audio file if path is too long - Stack Overflow
https://stackoverflow.com/questions/45221390/mcisendstring-wont-play-an-audio-file-if-path-is-too-long

A note re. the 127-char limit should be added to the documentation. I've mentioned it here:
Suggestions on documentation improvements - Page 28 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=13&t=1434&p=272453#p272453
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Ragnar
Posts: 264
Joined: 30 Sep 2013, 15:25

Re: soundplay filename length limit?

19 Apr 2019, 06:43

After some testing, it turned out that apparently only the path names of WAV files are affected by this 127-character-limit. For example, MP3 files with a path name longer than 127 characters will work, but only up to a maximum of 255 characters.
Last edited by Ragnar on 19 Apr 2019, 12:03, edited 1 time in total.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: soundplay filename length limit?

19 Apr 2019, 09:10

I tested an mp3 file, located on the desktop, with a path of 259 chars, it didn't play unless I used the short-form path. Tested on Windows 7.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Ragnar
Posts: 264
Joined: 30 Sep 2013, 15:25

Re: soundplay filename length limit?

19 Apr 2019, 12:02

Oops, my bad. The limit is 255. But please test any value between 128 and 255, not exactly the upper limit.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: soundplay filename length limit?

19 Apr 2019, 12:11

Mp3, path of length 255 did work, of length 259 didn't work.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 6551
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: soundplay filename length limit?

20 Apr 2019, 03:26

jeeswg wrote: c++ - mciSendString won't play an audio file if path is too long - Stack Overflow
https://stackoverflow.com/questions/45221390/mcisendstring-wont-play-an-audio-file-if-path-is-too-long
Following the next to last post to mmioOpen there is a interesting remark related to file names:
Parameters
  • szFilename
    ...
    The file name should not be longer than 128 characters, including the terminating NULL character.
Seemingly this function is still called for WAV files.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Odlanir, pn4265 and 246 guests