For plugins, how to get the size of a file?

So I’m looking for something like…

(setf fp (open fname :direction :input))
(setf fsize (file-length fp))

I’d also be happy with…

(file-position fp endpos)
(setf fpos (file-position fp))

or even…

(system “dir myfile.name > myfile.info”)

Edit: Forgot to mention, I know I could do something like below, but that’s only good for files under a megabyte.

(loop
(byte (read-byte fp))
(when (null byte) (return fsize))
(incf fsize)
)

Anyone know of a way to get the size of a specified file?

I don’t think it can be done with Nyquist in Audacity. It would be possible in the standalone version of Nyquist, but Nyquist in Audacity restricts system access for safety and security reasons.

Well what I’m really after is the file size of the current project, but that doesn’t seem to be available anywhere?

Btw, I did find another method…

(listdir “C:/Temp”)

It returns a list of files for any dir, but not apparently the size of each file. So close.

The (byte (read-byte fp)) method works, but it takes way too long to process for large files. Using (byte (read-float fp)) is faster, but not enough faster.

I wish the file-length call was supported. Or anything with the file size, really.

Exactly. It is very slow because it has to read every byte in the file to count them one by one.

You would really need XLisp’s “system” function to call the appropriate system command from the operating system, but that is one of the few XLISP functions excluded from Nyquist.

The (file-length fp) call could do this instantly, if only it were present. I wouldn’t really want to do this as a system call – that’s kind of a last resort situation.

Sigh, it’s always the coding for the boundaries of what’s possible on a platform that takes the most effort.

I know you’re using (open <filename> :direction :input) currently, but is XLISP’s (s-read) also available in Audacity?

If so, you could read the file in with (s-read fname), then (immediately) query the loaded duration from the global result variable with (snd-read-dur *rslt*).

Might be too heavy for examining full-length audio recordings, since it seems to be intended for loading brief sound samples directly into memory. But it definitely works for determining the duration of a short .wav file, in the NyquistIDE.

Hey, thanks so much, that did it!

If you use s-read with just the right arguments then it can instantly return the size of any type of file in bytes. Perfect! :grinning_face:

(s-read fname :format snd-head-raw :bits 8 :srate 1)
(snd-read-dur *rslt*)

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.