Last active
August 8, 2017 03:43
-
-
Save Claudiohbsantos/bd752cd7591fac9b97efcb6b95dab8da to your computer and use it in GitHub Desktop.
How to get parameter from reaper RPP file. Reascript API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- this function reads the RPP and stores it as chunks of 8 Kb in a table. This is more efficient than reading the file line by line (or at least I believe it is, please someone prove me wrong if you disagree =]) | |
function readProjFileToTable(projFile) | |
local f = assert(io.open(projFile,"r")) | |
local projectChunks = {} | |
local BUFSIZE = 2^13 -- 8K | |
while true do | |
local projectChunk, rest = f:read(BUFSIZE, "*line") | |
if not projectChunk then break end | |
if rest then projectChunk = projectChunk .. rest .. '\n' end | |
table.insert(projectChunks,projectChunk) | |
end | |
f:close() | |
return projectChunks | |
end | |
-- given a table with the projectfile and the parameter you are looking for, returns the ful value of the parameter, which is the line of the parameter - parameter name (there are exceptions to this in the RPP file that would have to be addressed individually with this code) | |
function getParameterFromProjectChunks(param,projectChunks) | |
local projectChunks = readProjFileToTable(projFile) | |
for i,chunk in ipairs(projectChunks) do | |
for line in string.gmatch(chunk,"([^\r\n]*)[\r\n]") do | |
local value = string.match(line,param.." (.*)") | |
if value then return value end | |
end | |
end | |
end | |
------------- Main Execution: | |
local _,projFile = reaper.EnumProjects(0,"") -- gets path to .RPP | |
--[[ In the RPP, the SAMPLERATE line has 3 values: | |
(example from rpp:) | |
SAMPLERATE 48000 1 0 | |
48000 is the samplerate | |
1 represents the checkbox "Project sample rate:" in the Project Settings. | |
0 represents the checkbox "Force project tempo/time signature...." in the Project Settings | |
The following call will get all three values as a string: ]] | |
local samplerateFromRPP = getParameterFromProjectChunks("SAMPLERATE",readProjFileToTable(projFile)) | |
-- Extract first number from the SAMPLERATE value from the RPP, which is the samplerate. | |
local samplerate = string.match(samplerateFromRPP,"^(%d+)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment