Page 1 of 1

soundplay filename length limit?

Posted: 10 Apr 2019, 00:56
by PuzzledGreatly
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.

Re: soundplay filename length limit?

Posted: 10 Apr 2019, 04:08
by Rohwedder
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.

Re: soundplay filename length limit?

Posted: 10 Apr 2019, 17:58
by PuzzledGreatly
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.

Re: soundplay filename length limit?

Posted: 10 Apr 2019, 19:01
by jeeswg
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

Re: soundplay filename length limit?

Posted: 11 Apr 2019, 22:25
by PuzzledGreatly
Thanks for the suggestion but it didn't work, long file names still wouldn't play

Re: soundplay filename length limit?

Posted: 12 Apr 2019, 01:59
by jeeswg
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

Re: soundplay filename length limit?

Posted: 12 Apr 2019, 03:50
by Rohwedder
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.

Re: soundplay filename length limit?

Posted: 13 Apr 2019, 03:08
by PuzzledGreatly
Not sure why it's not working for me. It would work with shorter file names but not longer ones.

Re: soundplay filename length limit?

Posted: 13 Apr 2019, 03:31
by jeeswg
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;
}

Re: soundplay filename length limit?

Posted: 13 Apr 2019, 03:53
by Rohwedder
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.

Re: soundplay filename length limit?

Posted: 13 Apr 2019, 04:00
by jeeswg
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

Re: soundplay filename length limit?

Posted: 13 Apr 2019, 05:26
by just me
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.

Re: soundplay filename length limit?

Posted: 13 Apr 2019, 05:33
by jeeswg
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

Re: soundplay filename length limit?

Posted: 19 Apr 2019, 06:43
by Ragnar
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.

Re: soundplay filename length limit?

Posted: 19 Apr 2019, 09:10
by jeeswg
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.

Re: soundplay filename length limit?

Posted: 19 Apr 2019, 12:02
by Ragnar
Oops, my bad. The limit is 255. But please test any value between 128 and 255, not exactly the upper limit.

Re: soundplay filename length limit?

Posted: 19 Apr 2019, 12:11
by jeeswg
Mp3, path of length 255 did work, of length 259 didn't work.

Re: soundplay filename length limit?

Posted: 20 Apr 2019, 03:26
by just me
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.