Skip to content

Commit

Permalink
minor fix and update for version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesneimog committed Sep 28, 2023
1 parent 485b793 commit b3fee35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
15 changes: 7 additions & 8 deletions pd2wasm/externals/pmpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def pmpd_extra(librarySelf: PureDataExternals):
if not os.path.exists(os.path.join(librarySelf.PROJECT_ROOT, "webpatch", "includes")):
os.makedirs(os.path.join(librarySelf.PROJECT_ROOT, "webpatch", "includes"))


folder = librarySelf.folder

# search for pmpd.h and pmpd_version.h and copy to externals folder
for root, _, files in os.walk(folder):
for file in files:
Expand All @@ -38,19 +36,20 @@ def pmpd_extra(librarySelf: PureDataExternals):
if not os.path.exists(os.path.join(folder, "build")):
os.makedirs(os.path.join(folder, "build"))

os.system(f"{emCmake} -B {os.path.join(folder, 'build')} {folder} -Wno-dev")
# check if pmpd_export.h already exists, if not, run cmake
if not os.path.exists(os.path.join(folder, "build", "pmpd_export.h")):
os.system(f"{emCmake} -B {os.path.join(folder, 'build')} {folder} -Wno-dev")

# from the build folder, copy pmpd_export.h to includes folder
shutil.copy(os.path.join(folder, "build", "pmpd_export.h"),
os.path.join(librarySelf.PROJECT_ROOT, "webpatch", "includes", "pmpd_export.h"))

# from the build folder, copy pmpd_export.h to includes folder
shutil.copy(os.path.join(folder, "build", "pmpd_version.c"),
os.path.join(librarySelf.PROJECT_ROOT, "webpatch", "externals", "pmpd_version.c"))




if "pmpd" in librarySelf.usedObjs:
for file in ["pmpd_core.c", "pmpd_set.c", "pmpd_get.c", "pmpd_list.c", "pmpd_tab.c", "pmpd_test.c", "pmpd_stat.c", "pmpd_various.c", "pmpd_deprecated.c"]:
shutil.copy(os.path.join(folder, file),
os.path.join(librarySelf.PROJECT_ROOT, "webpatch", "includes", file))



47 changes: 28 additions & 19 deletions pd2wasm/pd2wasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, sourcefile="src/template.c", pdpatch=None,

# get this folder directory
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter, description="Check the complete docs in https://www.charlesneimog.com/PdWebCompiler")
formatter_class=argparse.RawTextHelpFormatter, description="Check the complete docs in https://www.charlesneimog.github.io/PdWebCompiler")
parser.add_argument('--patch', required=True,
help='Patch file (.pd) to compile')
parser.add_argument(
Expand All @@ -63,12 +63,18 @@ def __init__(self, sourcefile="src/template.c", pdpatch=None,

parser.add_argument('--initial-memory', required=False,
default=32, help='Set the initial memory of the WebAssembly in MB')

parser.add_argument('--replace-helper', required=False,
default=None, help="Replace helpers.js file by your own file")

parser.add_argument('--version', action='version',
version='%(prog)s 1.0.9')
version='%(prog)s 1.1.0')

self.args = parser.parse_args()

if pdpatch is not None:
self.args.patch = pdpatch

if not os.path.isabs(self.args.patch) and not insideaddAbstractions:
absolutePath = os.path.dirname(os.path.abspath(
os.path.join(os.getcwd(), self.args.patch)))
Expand Down Expand Up @@ -106,6 +112,7 @@ def __init__(self, sourcefile="src/template.c", pdpatch=None,
self.PROJECT_ROOT = self.PROJECT_ROOT + "/"

self.FoundExternals = False
self.jsHelper = self.args.replace_helper
self.html = False
self.pageFolder = self.args.page_folder
self.parent = parent
Expand Down Expand Up @@ -226,8 +233,22 @@ def main(self, pdpatch=None, insideaddAbstractions=False):
self.addAbstractions()
shutil.copy(self.PdWebCompilerPath +
"/src/index.html", self.PROJECT_ROOT + "webpatch/index.html")
shutil.copy(self.PdWebCompilerPath +

if self.jsHelper is not None:
# check if the file is relative or absolute
if not os.path.isabs(self.jsHelper):
self.jsHelper = os.getcwd() + "/" + self.jsHelper

if not os.path.exists(self.jsHelper):
myprint("The file " + self.jsHelper + " does not exist!", color="red")
sys.exit(1)

shutil.copy(self.jsHelper, self.PROJECT_ROOT + "webpatch/helpers.js")

else:
shutil.copy(self.PdWebCompilerPath +
"/src/helpers.js", self.PROJECT_ROOT + "webpatch/helpers.js")

shutil.copy(self.PdWebCompilerPath +
"/src/enable-threads.js", self.PROJECT_ROOT + "webpatch/enable-threads.js")
if self.pageFolder is not None:
Expand Down Expand Up @@ -392,27 +413,22 @@ def replaceVISArray(self):
arrayFirstIndex = i + 1
arrayName = LineTokens_Next[2]
arrayLength = LineTokens_Next[3]

j = 2
while True:
if self.PatchLines[i + j].split(" ")[0] == "#A":
j += 1
else:
arrayLastIndex = i + j - 1
break

if self.PatchLines[arrayLastIndex + 1].split(" ")[1] == "coords":
coordsIndex = arrayLastIndex + 1
if self.PatchLines[arrayLastIndex + 2].split(" ")[1] == "restore":
restoreIndex = arrayLastIndex + 2
x_y_coords['x'] = self.PatchLines[restoreIndex].split(" ")[2]
x_y_coords['y'] = self.PatchLines[restoreIndex].split(" ")[3]

if canvasIndex and coordsIndex and restoreIndex:
break

if canvasIndex and coordsIndex and restoreIndex:
# from self.PatchLines, remove canvasIndex value
self.PatchLines.pop(canvasIndex)
arrayDefine = f"#X obj {x_y_coords['x']} {x_y_coords['y']} array define " \
f"{arrayName} {arrayLength};\n"
Expand All @@ -423,8 +439,6 @@ def replaceVISArray(self):
myprint("" + self.args.patch +
" has VIS array, it is not supported and was replaced by [array define]", color='yellow')
self.replaceVISArray()

# save self.PatchLines to file
with open(self.args.patch, "w") as file:
for line in self.PatchLines:
file.write(line)
Expand All @@ -446,7 +460,7 @@ def copyAllDataFiles(self):
'''
if not os.path.exists(self.PROJECT_ROOT + "webpatch/data"):
os.mkdir(self.PROJECT_ROOT + "webpatch/data")
for folderName in ["extra", "Extras", "Audios", "libs", "Abstractions"]:
for folderName in ["extra", "Extras", "Audios", "Libs", "libs", "Abstractions"]:
if not os.path.exists(folderName):
continue
if folderName != "Extras":
Expand All @@ -461,8 +475,6 @@ def checkIfIsSupportedObject(self, patchLine):
myprint("Visual Arrays are not supported, "
+ "use [array define] object", color='red')
sys.exit(1)

# here you add all other objects


def findExternals(self):
Expand All @@ -480,7 +492,6 @@ def findExternals(self):
objName = patchLine.Tokens[4].replace(
"\n", "").replace(";", "").replace(",", "")
self.checkIfIsSupportedObject(patchLine.Tokens)

if (patchLine.Tokens[0] == "#X" and patchLine.Tokens[1] == "obj"
and "/" in patchLine.Tokens[4]) and objName != "/":
patchLine.isExternal = True
Expand All @@ -492,7 +503,6 @@ def findExternals(self):
patchLine.isAbstraction = True
patchLine.objFound = True
patchLine.isExternal = False

elif self.ObjIsLibrary(patchLine.Tokens):
patchLine.isExternal = True
patchLine.library = objName
Expand All @@ -501,7 +511,6 @@ def findExternals(self):
myprint("It is an abstraction", color='red')
patchLine.objGenSym = 'gensym("' + patchLine.library + '")'
patchLine.singleObject = True

elif ("s" == patchLine.Tokens[4] or "send" == patchLine.Tokens[4]):
receiverSymbol = patchLine.Tokens[5].replace(
"\n", "").replace(";", "").replace(",", "")
Expand All @@ -510,7 +519,6 @@ def findExternals(self):
patchLine.uiSymbol = receiverSymbol
self.uiReceiversSymbol.append(receiverSymbol)
patchLine.name = objName

else:
patchLine.name = objName
self.searchForSpecialObject(patchLine)
Expand Down Expand Up @@ -964,6 +972,7 @@ def emccCompile(self):
self.libpd_dir = self.PdWebCompilerPath + '/libpd'
self.src_files = self.PROJECT_ROOT + 'webpatch/main.c'
emcc = self.PdWebCompilerPath + '/emsdk/upstream/emscripten/emcc'
os.chdir(self.PROJECT_ROOT)
command = [emcc,
'-I', self.PROJECT_ROOT + 'webpatch/includes/',
'-I', self.libpd_dir + '/pure-data/src/',
Expand All @@ -977,7 +986,7 @@ def emccCompile(self):
'-s', 'WASM_WORKERS=1',
'-s', 'WASM=1',
'-s', 'USE_PTHREADS=1',
'--preload-file', self.PROJECT_ROOT + 'webpatch/data/',
'--preload-file', 'webpatch/data/',
]

indexFlag = 0
Expand Down Expand Up @@ -1049,4 +1058,4 @@ def emccCompile(self):
emrun = self.PdWebCompilerPath + \
f'/emsdk/upstream/emscripten/emrun --port {self.args.server_port} .'
os.system(emrun)
sys.exit(0)
sys.exit(0)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pd2wasm"
version = "1.0.10"
version = "1.1.0"
description = "pd2wasm convert a PureData patch to Wasm file."
authors = ["Charles K. Neimog <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit b3fee35

Please sign in to comment.