Add music source edna project

soundtrack
Ensar Sarajčić 2021-11-12 12:11:49 +01:00
parent 2b36af882e
commit 655dc82ef8
11 changed files with 172 additions and 0 deletions

5
assets/source/music/.gitignore vendored 100644
View File

@ -0,0 +1,5 @@
target/
resources/
!resources/public/index.html
.*
!.gitignore

View File

@ -0,0 +1,30 @@
# Bugged racing music source
Music for this game is builts using [edna](https://github.com/oakes/edna) clojure library, which uses [alda](https://github.com/alda-lang/alda) underneath.
To build this project, you'll need the Clojure CLI tool:
https://clojure.org/guides/deps_and_cli
To develop in a browser with live code reloading:
`clj -M dev.clj`
To build a release version for the web:
`clj -M prod.clj`
To play the song once:
`clj -M dev.clj play [song]`
To build wav:
`clj -M prod.clj wav [song]`
To build wav for all songs:
`clj -M prod.clj soundtrack`

View File

@ -0,0 +1,4 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojurescript {:mvn/version "1.10.764"}
com.bhauman/figwheel-main {:mvn/version "0.2.9"}
edna/edna {:mvn/version "1.6.0"}}}

View File

@ -0,0 +1,20 @@
(defmulti task first)
(defmethod task :default
[[task-name]]
(println "Unknown task:" task-name)
(System/exit 1))
(require '[figwheel.main :as figwheel])
(defmethod task nil
[_]
(figwheel/-main "--build" "dev"))
(require '[music.core])
(defmethod task "play"
[_]
(music.core/-main))
(task *command-line-args*)

View File

@ -0,0 +1,5 @@
{:main music.core
:optimizations :none
:output-to "resources/public/main.js"
:output-dir "resources/public/main.out"
:asset-path "/main.out"}

View File

@ -0,0 +1,2 @@
{:open-url "http://localhost:9500"
:clean-outputs true}

View File

@ -0,0 +1,55 @@
(defmulti task first)
(defmethod task :default
[[task-name]]
(println "Unknown task:" task-name)
(System/exit 1))
(require
'[edna.core :as edna]
'[music.core :as c]
'[cljs.build.api :as api]
'[clojure.java.io :as io])
(defn delete-children-recursively! [f]
(when (.isDirectory f)
(doseq [f2 (.listFiles f)]
(delete-children-recursively! f2)))
(when (.exists f) (io/delete-file f)))
(defmethod task nil
[_]
(let [out-file "resources/public/main.js"
out-dir "resources/public/main.out"]
(println "Building main.js")
(delete-children-recursively! (io/file out-dir))
(api/build "src" {:main 'music.core
:optimizations :advanced
:output-to out-file
:output-dir out-dir
:infer-externs true})
(delete-children-recursively! (io/file out-dir))
(println "Build complete:" out-file)
(System/exit 0)))
(defn export-song-wav [song]
(let [wav-name (str song ".wav")
build-dir "target"
output-file (io/file build-dir wav-name)]
(.mkdir (io/file build-dir))
(println "Building" wav-name)
(edna/export! (c/read-music song) {:type :wav, :out output-file})
(println "Build complete:" wav-name)
(println "Song saved into:" (.getPath output-file))))
(defmethod task "wav"
[[_ song]]
(export-song-wav song)
(System/exit 0))
(defmethod task "soundtrack"
[_]
(run! #(task ["wav" %]) (c/get-all-songs))
(System/exit 0))
(task *command-line-args*)

View File

@ -0,0 +1,21 @@
(ns music.core
(:require [edna.core :as edna]
[clojure.java.io :as io]
[clojure.string :as string]))
(def songs-dir "src/music/songs/")
(defn read-music [song]
(load-file (str songs-dir song ".clj")))
(defn get-all-songs []
(map #(string/replace (.getName %) ".clj" "") (.listFiles (io/file songs-dir))))
(defonce state (atom nil))
(defn -main [song]
(swap! state edna/stop!)
(reset! state (edna/play! (read-music song))))
(defmacro build-for-cljs []
(edna/edna->data-uri (read-music "main-theme")))

View File

@ -0,0 +1,10 @@
(ns music.core
(:require-macros [music.music]
[music.core :refer [build-for-cljs]]))
(defonce audio (js/document.createElement "audio"))
(set! (.-src audio) (build-for-cljs))
(set! (.-controls audio) true)
(js/document.body.appendChild audio)
(.play audio)

View File

@ -0,0 +1,10 @@
(ns music.music)
[:piano {:octave 4
:tempo 74}
1/8 #{:-d :-a :e :f#} :a 1/2 #{:f# :+d}
1/8 #{:-e :e :+c} :a 1/2 #{:c :e :f}
1/8 #{:-d :-a :e :f#} :a :+d :+c# :+e :+d :b :+c#
1/2 #{:-e :c :a} 1/2 #{:c :e}]

View File

@ -0,0 +1,10 @@
(ns music.songs.main-theme)
[:piano {:octave 4
:tempo 60}
1/8 #{:-d :-a :e :f#} :a 1/2 #{:f# :+d}
1/8 #{:-e :e :+c} :a 1/2 #{:c :e :f}
1/8 #{:-d :-a :e :f#} :a :+d :+c# :+e :+d :b :+c#
1/2 #{:-e :c :a} 1/2 #{:c :e}]