Skip to content

Instantly share code, notes, and snippets.

@butchi
Created December 3, 2016 14:42
Show Gist options
  • Save butchi/1f069caaebf885a6046561ee862097e6 to your computer and use it in GitHub Desktop.
Save butchi/1f069caaebf885a6046561ee862097e6 to your computer and use it in GitHub Desktop.
SoundNoteをPlayで再実装してきよしこの夜を鳴らす ref: http://qiita.com/butchi_y/items/d8b85aa3e13a835a33fa
sound[{SoundNote[7], SoundNote[7], SoundNote[7], SoundNote[3, 4]}, 1.5]
sound[SoundNote[pitch_?NumberQ, time_?NumberQ, tspec_: Null,
style_: Null]] :=
Sound[Play[Sin[440*2^((pitch - 9)/12) 2 Pi t], {t, 0, time},
SampleRate -> 44100]]
noteExpand[str_String] := {
First[StringCases[str, CharacterRange["A", "G"]]],
First[StringCases[str, {"#", "b"}], ""],
First[StringCases[str, DigitCharacter ..], "4"]
}
basePitch[s_String] :=
Piecewise[{{0, s == "C"}, {2, s == "D"}, {4, s == "E"}, {5,
s == "F"}, {7, s == "G"}, {9, s == "A"}, {11, s == "B"}}, 0]
accidentalToShift[s_String] :=
Piecewise[{{1, s == "#"}, {-1, s == "b"}}, 0]
noteObjToNum[{sign_String, accidental_String, octave_String}] :=
12 (ToExpression[octave] - 4) + basePitch[sign] +
ToExpression[accidentalToShift[accidental]]
noteToNum[str_String] := noteObjToNum[noteExpand[str]]
pitch[val_] := If[NumberQ[val], val, noteToNum[val]]
mid = Import["data/kiyosi.mid"]
Export["kiyoshi-org.mp3", mid]
mid = Import["data/kiyosi--www-ne-jp--asahi--music--myuu.mid"];
snd = sound[First[mid]]
Export["kiyoshi-sine.wav", snd]
noteExpand[str_String] := {
First[StringCases[str, CharacterRange["A", "G"]]],
First[StringCases[str, {"#", "b"}], ""],
First[StringCases[str, DigitCharacter ..], "4"]
}
basePitch[s_String] :=
Piecewise[{{0, s == "C"}, {2, s == "D"}, {4, s == "E"}, {5,
s == "F"}, {7, s == "G"}, {9, s == "A"}, {11, s == "B"}}, 0]
accidentalToShift[s_String] :=
Piecewise[{{1, s == "#"}, {-1, s == "b"}}, 0]
noteObjToNum[{sign_String, accidental_String, octave_String}] :=
12 (ToExpression[octave] - 4) + basePitch[sign] +
ToExpression[accidentalToShift[accidental]]
noteToNum[str_String] := noteObjToNum[noteExpand[str]]
pitch[val_] := If[NumberQ[val], val, noteToNum[val]]
sound[SoundNote[p_String, time_?NumberQ, tspec_: Null,
style_: Null]] := sound[SoundNote[pitch[p], time, tspec, style]]
sound[SoundNote[p_String, timeLi_List, tspec_: Null, style_: Null]] :=
sound[SoundNote[pitch[p], timeLi]]
sound[SoundNote[p_], timeLi_List] :=
sound[SoundNote[pitch[p], timeLi]]
sound[SoundNote[p_String]] := sound[SoundNote[p, 1]]
sound[SoundNote[pitch_?NumberQ, time_?NumberQ, tspec_: Null,
style_: Null]] :=
Sound[Play[Sin[440*2^((pitch - 9)/12) 2 Pi t], {t, 0, time},
SampleRate -> 44100]]
sound[SoundNote[pitch_?NumberQ]] := sound[SoundNote[pitch, 1]]
sound[SoundNote[]] := sound[SoundNote[0]]
sound[SoundNote[pitch_?NumberQ, timeLi_List, tspec_: Null,
style_: Null]] :=
Sound[sound[SoundNote[pitch, (#[[2]] - #[[1]]) &@timeLi]], timeLi]
sound[SoundNote[pitch_?NumberQ], timeLi_List] :=
Sound[sound[SoundNote[pitch, (#[[2]] - #[[1]]) &@timeLi]], timeLi]
sound[SoundNote[None, time_: 1]] := Sound[SoundNote[None, time]]
sound[{sn__SoundNote}, time_?NumberQ] := Sound[sound /@ {sn}, time]
sound[{sn__SoundNote}] := Sound[sound /@ {sn}]
sound[SoundNote[{p__}, time_?NumberQ]] :=
Sound[sound[SoundNote[#], {0, time}] & /@ {p}, time]
sound[SoundNote[{p__}]] :=
Sound[sound[SoundNote[#], {0, 1}] & /@ {p}, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment