├── go.mod ├── go.sum ├── .gitignore ├── .dockerignore ├── .github └── workflows │ └── docker-image.yml ├── LICENSE ├── Dockerfile ├── templates ├── index.html ├── style.css └── app.js ├── README.md └── main.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Nirmata-1/Audiforge 2 | 3 | go 1.24.1 4 | 5 | require github.com/google/uuid v1.6.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= 2 | github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool 12 | *.out 13 | 14 | # Dependency directories 15 | # vendor/ 16 | 17 | # Go workspace file 18 | go.work 19 | go.work.sum 20 | 21 | # env file 22 | .env 23 | 24 | # audiveris directory 25 | audiveris/ -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | # Ignore Git 2 | .git/ 3 | .github/ 4 | 5 | # Ignore the Dockerfile itself 6 | Dockerfile 7 | .dockerignore 8 | .gitignore 9 | 10 | # Ignore Go build stuff 11 | bin/ 12 | *.exe 13 | *.exe~ 14 | *.dll 15 | *.so 16 | *.dylib 17 | 18 | # Ignore any temporary files 19 | .DS_Store 20 | Thumbs.db 21 | 22 | # Ignore any logs or backup files 23 | *.log 24 | *.bak 25 | -------------------------------------------------------------------------------- /.github/workflows/docker-image.yml: -------------------------------------------------------------------------------- 1 | name: Build and Push Docker Image 2 | 3 | on: 4 | push: 5 | branches: 6 | - main # Trigger workflow on pushes to the main branch 7 | workflow_dispatch: # Allow manual triggering 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | # Step 1: Check out the repository 15 | - name: Check out code 16 | uses: actions/checkout@v3 17 | 18 | # Step 2: Log in to Docker Hub 19 | - name: Log in to Docker Hub 20 | uses: docker/login-action@v2 21 | with: 22 | username: ${{ secrets.DOCKER_USERNAME }} 23 | password: ${{ secrets.DOCKER_PASSWORD }} 24 | 25 | # Step 3: Build the Docker image 26 | - name: Build Docker image 27 | run: | 28 | docker build -t ${{ secrets.DOCKER_USERNAME }}/audiforge:latest . 29 | 30 | # Step 4: Push the Docker image to Docker Hub 31 | - name: Push Docker image 32 | run: | 33 | docker push ${{ secrets.DOCKER_USERNAME }}/audiforge:latest 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Jermiah Jeffries 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Stage 1: Build Go application 2 | FROM golang:1.24 AS go-builder 3 | 4 | WORKDIR /app 5 | COPY . . 6 | COPY templates/ ./templates/ 7 | RUN go build -o audiforge . 8 | 9 | # Stage 2: Build Audiveris and final image 10 | FROM debian:bookworm-slim 11 | 12 | # Install system dependencies 13 | RUN apt-get update && \ 14 | apt-get install -y \ 15 | git \ 16 | wget \ 17 | unzip \ 18 | zip \ 19 | ca-certificates \ 20 | fontconfig \ 21 | fonts-dejavu \ 22 | libfreetype6 \ 23 | && apt-get clean \ 24 | && rm -rf /var/lib/apt/lists/* 25 | 26 | # Install Java 21 JDK 27 | RUN mkdir -p /etc/apt/keyrings && \ 28 | wget -O /etc/apt/keyrings/adoptium.asc https://packages.adoptium.net/artifactory/api/gpg/key/public && \ 29 | echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | \ 30 | tee /etc/apt/sources.list.d/adoptium.list && \ 31 | apt-get update && \ 32 | apt-get install -y temurin-21-jdk 33 | 34 | # Install Gradle 8.7 35 | RUN wget https://services.gradle.org/distributions/gradle-8.7-bin.zip -O /tmp/gradle.zip \ 36 | && unzip -d /opt /tmp/gradle.zip \ 37 | && rm /tmp/gradle.zip 38 | ENV PATH="/opt/gradle-8.7/bin:${PATH}" 39 | 40 | # Build Audiveris 41 | WORKDIR /app 42 | RUN git clone https://github.com/Nirmata-1/audiveris.git 43 | WORKDIR /app/audiveris 44 | RUN ./gradlew build 45 | 46 | # Copy Go artifacts from first stage 47 | COPY --from=go-builder /app/audiforge /app/ 48 | COPY --from=go-builder /app/templates /app/templates 49 | 50 | # Setup environment 51 | RUN mkdir -p /tmp/uploads /tmp/downloads && \ 52 | chmod -R 755 /tmp/uploads /tmp/downloads /app/templates 53 | 54 | ENV AUDIVERIS_HOME=/app/audiveris \ 55 | UPLOAD_DIR=/tmp/uploads \ 56 | DOWNLOAD_DIR=/tmp/downloads \ 57 | LOG="" 58 | 59 | EXPOSE 8080 60 | ENTRYPOINT ["/app/audiforge"] 61 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |PDF to MusicXML Conversion Platform
13 |Click or drag PDF to convert
20 |Transforming your sheet music...
26 |${message}
110 | 115 | `; 116 | completeDiv.classList.remove('hidden'); 117 | } 118 | 119 | function resetDropZone() { 120 | dropZone.style.borderColor = 'var(--brand-mid)'; 121 | dropZone.style.background = 'rgba(255, 255, 255, 0.95)'; 122 | } 123 | 124 | function resetUI() { 125 | currentConversionId = null; 126 | completeDiv.classList.add('hidden'); 127 | dropZone.classList.remove('hidden'); 128 | processingDiv.classList.add('hidden'); 129 | } 130 | }); -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "html/template" 7 | "io" 8 | "log" 9 | "net/http" 10 | "os" 11 | "os/exec" 12 | "path/filepath" 13 | "strings" 14 | "sync" 15 | "time" 16 | 17 | "github.com/google/uuid" 18 | ) 19 | 20 | const ( 21 | UploadDir = "/tmp/uploads" 22 | DownloadDir = "/tmp/downloads" 23 | CleanupInterval = 1 * time.Hour 24 | FileTTL = 1 * time.Hour 25 | ) 26 | 27 | type ProcessingStatus struct { 28 | Status string `json:"status"` 29 | Message string `json:"message"` 30 | Timestamp int64 `json:"timestamp"` 31 | MovementCount int `json:"movementCount,omitempty"` 32 | } 33 | 34 | var ( 35 | processing sync.Map 36 | templates *template.Template 37 | ) 38 | 39 | func init() { 40 | os.MkdirAll(UploadDir, 0755) 41 | os.MkdirAll(DownloadDir, 0755) 42 | templates = template.Must(template.ParseGlob("/app/templates/*.html")) 43 | go startCleanupRoutine() 44 | } 45 | 46 | // I can't believe this Janky code works. Good luck to whoever reads this. 47 | func main() { 48 | http.HandleFunc("/", indexHandler) 49 | http.HandleFunc("/upload", uploadHandler) 50 | http.HandleFunc("/status/", statusHandler) 51 | http.HandleFunc("/download/", downloadHandler) 52 | http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/app/templates")))) 53 | 54 | log.Println("Server started on :8080") 55 | log.Fatal(http.ListenAndServe(":8080", nil)) 56 | } 57 | 58 | // Add the cleanup routine 59 | func startCleanupRoutine() { 60 | for { 61 | time.Sleep(CleanupInterval) 62 | cleanupFiles() 63 | } 64 | } 65 | 66 | func cleanupFiles() { 67 | if os.Getenv("LOG") != "debug" { 68 | return 69 | } 70 | 71 | log.Println("Starting cleanup routine...") 72 | cleanupDirectory(UploadDir, cleanUploads) 73 | cleanupDirectory(DownloadDir, cleanDownloads) 74 | } 75 | 76 | func cleanupDirectory(path string, cleanFunc func(string) error) { 77 | err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error { 78 | if err != nil { 79 | return nil // Skip unreadable files 80 | } 81 | 82 | // Check if the file is older than TTL 83 | if time.Since(info.ModTime()) > FileTTL { 84 | // Call the cleanup function for old files 85 | if err := cleanFunc(filePath); err != nil { 86 | log.Printf("Cleanup failed for %s: %v", filePath, err) 87 | } 88 | } 89 | return nil 90 | }) 91 | 92 | if err != nil { 93 | log.Printf("Cleanup error: %v", err) 94 | } 95 | } 96 | 97 | func cleanUploads(path string) error { 98 | if strings.HasSuffix(path, ".pdf") { 99 | if err := os.Remove(path); err == nil { 100 | log.Printf("Cleaned up upload file: %s", path) 101 | } 102 | } 103 | return nil 104 | } 105 | 106 | func cleanDownloads(path string) error { 107 | // Resolve absolute paths to handle symlinks/relative paths 108 | downloadDirAbs, err := filepath.Abs(DownloadDir) 109 | if err != nil { 110 | return fmt.Errorf("failed to resolve DownloadDir: %v", err) 111 | } 112 | 113 | fileDir := filepath.Dir(path) 114 | fileDirAbs, err := filepath.Abs(fileDir) 115 | if err != nil { 116 | return nil // Skip if path resolution fails 117 | } 118 | 119 | // Check if the directory is a subdirectory of DownloadDir 120 | if strings.HasPrefix(fileDirAbs, downloadDirAbs + string(filepath.Separator)) && 121 | fileDirAbs != downloadDirAbs { 122 | 123 | // Delete the entire subdirectory 124 | if err := os.RemoveAll(fileDir); err != nil { 125 | return fmt.Errorf("failed to delete %s: %v", fileDir, err) 126 | } 127 | log.Printf("Cleaned up download directory: %s", fileDir) 128 | return filepath.SkipDir // Skip further processing of this directory 129 | } 130 | 131 | return nil 132 | } 133 | 134 | func indexHandler(w http.ResponseWriter, r *http.Request) { 135 | templates.ExecuteTemplate(w, "index.html", nil) 136 | } 137 | 138 | func uploadHandler(w http.ResponseWriter, r *http.Request) { 139 | if r.Method != "POST" { 140 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) 141 | return 142 | } 143 | 144 | file, header, err := r.FormFile("file") 145 | if err != nil { 146 | http.Error(w, "Invalid file", http.StatusBadRequest) 147 | return 148 | } 149 | defer file.Close() 150 | 151 | if ext := filepath.Ext(header.Filename); ext != ".pdf" { 152 | http.Error(w, "Only PDF files allowed", http.StatusBadRequest) 153 | return 154 | } 155 | 156 | id := uuid.New().String() 157 | uploadPath := filepath.Join(UploadDir, id+".pdf") 158 | 159 | out, err := os.Create(uploadPath) 160 | if err != nil { 161 | http.Error(w, "Failed to save file", http.StatusInternalServerError) 162 | return 163 | } 164 | defer out.Close() 165 | 166 | if _, err := io.Copy(out, file); err != nil { 167 | http.Error(w, "Failed to save file", http.StatusInternalServerError) 168 | return 169 | } 170 | 171 | processing.Store(id, ProcessingStatus{ 172 | Status: "processing", 173 | Message: "File uploaded, starting conversion", 174 | Timestamp: time.Now().Unix(), 175 | }) 176 | 177 | go processFile(id, uploadPath) 178 | 179 | w.Header().Set("Content-Type", "application/json") 180 | json.NewEncoder(w).Encode(map[string]string{"id": id}) 181 | } 182 | 183 | func processFile(id, inputPath string) { 184 | outputDir := filepath.Join(DownloadDir, id) 185 | if err := os.MkdirAll(outputDir, 0755); err != nil { 186 | log.Printf("Error creating output dir: %v", err) 187 | processing.Store(id, ProcessingStatus{ 188 | Status: "error", 189 | Message: fmt.Sprintf("Failed to create output directory: %v", err), 190 | Timestamp: time.Now().Unix(), 191 | }) 192 | return 193 | } 194 | 195 | cmdArgs := []string{ 196 | "-batch", 197 | "-export", 198 | "-output", outputDir, 199 | "--", inputPath, 200 | } 201 | 202 | cmd := exec.Command( 203 | "gradle", 204 | "run", 205 | "-PjvmLineArgs=-Xmx3g", 206 | fmt.Sprintf("-PcmdLineArgs=%s", escapeArgs(cmdArgs)), 207 | ) 208 | cmd.Dir = "/app/audiveris" 209 | 210 | logPath := filepath.Join(outputDir, "conversion.log") 211 | outputFile, err := os.Create(logPath) 212 | if err != nil { 213 | log.Printf("Error creating log file: %v", err) 214 | processing.Store(id, ProcessingStatus{ 215 | Status: "error", 216 | Message: fmt.Sprintf("Failed to create log file: %v", err), 217 | Timestamp: time.Now().Unix(), 218 | }) 219 | return 220 | } 221 | defer outputFile.Close() 222 | 223 | var logWriter io.Writer = outputFile 224 | if os.Getenv("LOG") == "debug" { 225 | logWriter = io.MultiWriter(outputFile, os.Stdout) 226 | } 227 | 228 | cmd.Stdout = logWriter 229 | cmd.Stderr = logWriter 230 | 231 | log.Printf("\n=== START Processing %s ===", id) 232 | defer log.Printf("=== END Processing %s ===\n", id) 233 | 234 | processing.Store(id, ProcessingStatus{ 235 | Status: "processing", 236 | Message: "Converting PDF to MusicXML", 237 | Timestamp: time.Now().Unix(), 238 | }) 239 | 240 | runErr := cmd.Run() 241 | 242 | // Check for generated movements 243 | files, _ := filepath.Glob(filepath.Join(outputDir, "*.mxl")) 244 | movementCount := len(files) 245 | 246 | // Update processFile to check for both errors and files 247 | if movementCount > 0 { 248 | msg := "Conversion completed with potential warnings" 249 | status := "completed" 250 | if runErr != nil { 251 | msg = fmt.Sprintf("Conversion completed with errors (%v)", runErr) 252 | } 253 | processing.Store(id, ProcessingStatus{ 254 | Status: status, 255 | Message: msg, 256 | Timestamp: time.Now().Unix(), 257 | MovementCount: movementCount, 258 | }) 259 | } else { 260 | errorMsg := "Conversion failed - no movements generated" 261 | if runErr != nil { 262 | errorMsg += fmt.Sprintf(" (exec error: %v)", runErr) 263 | } 264 | processing.Store(id, ProcessingStatus{ 265 | Status: "error", 266 | Message: errorMsg, 267 | Timestamp: time.Now().Unix(), 268 | }) 269 | } 270 | } 271 | 272 | func escapeArgs(args []string) string { 273 | var escaped []string 274 | for _, arg := range args { 275 | if strings.ContainsAny(arg, " ,") { 276 | escaped = append(escaped, fmt.Sprintf(`"%s"`, arg)) 277 | } else { 278 | escaped = append(escaped, arg) 279 | } 280 | } 281 | return strings.Join(escaped, ",") 282 | } 283 | 284 | func statusHandler(w http.ResponseWriter, r *http.Request) { 285 | id := strings.TrimPrefix(r.URL.Path, "/status/") 286 | status, ok := processing.Load(id) 287 | if !ok { 288 | http.Error(w, "Invalid ID", http.StatusNotFound) 289 | return 290 | } 291 | 292 | w.Header().Set("Content-Type", "application/json") 293 | json.NewEncoder(w).Encode(status) 294 | } 295 | 296 | func downloadHandler(w http.ResponseWriter, r *http.Request) { 297 | id := strings.TrimPrefix(r.URL.Path, "/download/") 298 | outputDir := filepath.Join(DownloadDir, id) 299 | 300 | // Find all MXL files 301 | files, err := filepath.Glob(filepath.Join(outputDir, "*.mxl")) 302 | if err != nil || len(files) == 0 { 303 | http.Error(w, "No movements found", http.StatusNotFound) 304 | return 305 | } 306 | 307 | // Create ZIP archive 308 | zipPath := filepath.Join(outputDir, "converted.zip") 309 | args := append([]string{"-j", zipPath}, files...) 310 | cmd := exec.Command("zip", args...) 311 | if err := cmd.Run(); err != nil { 312 | http.Error(w, "Failed to create ZIP archive: " + err.Error(), http.StatusInternalServerError) 313 | return 314 | } 315 | 316 | // Serve ZIP file 317 | w.Header().Set("Content-Type", "application/zip") 318 | w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", id)) 319 | http.ServeFile(w, r, zipPath) 320 | } 321 | --------------------------------------------------------------------------------