7
$\begingroup$

I have a list of imported midi files, as Sound objects. I simply want to string them together. I've tried a lot of things and nothing has worked, apart from a hack-y version of manipulating the midi's as strings, which is needlessly over complicated.

Is there an easy way to combine sound objects like this?

$\endgroup$
7
+50
$\begingroup$

Explanation

Sound can combine the two sound sequences:

Sound[{s1, s2}]

Mathematica graphics

the problem you may be facing is that if you do

Sound[{Import["file1.mid"], Import["file2.mid"]}]

Is that they may play sequentially, one after the other, and not at the same time. That is, unless you specify the initial and final time.

Mathematica graphics

Therefore this would work

Sound[
 {
  Sound[Import["file1.mid"], {0, duration1}],
  Sound[Import["file2.mid"], {0, duration2}]
  }
 ]

To get the duration time you may do

duration1= Import["file1.mid", {"MIDI", "Duration"}]

Solution

mixMIDI[fl_] := Sound[
  Table[
   Module[{
     mf = Import[file, "MIDI"],
     duration = Import[file, {"MIDI", "Duration"}]
     },
    Sound[mf, {0, duration}]
    ]
   , {file, fl}]
  ]

mixMIDI@FileNames["*.mid"]

Mathematica graphics

Example ("Thus Spake Zarathustra")

Export["Bassoon.mid", 
 Sound[{SoundNote["C1", {0.10, 13.13}, "Bassoon", SoundVolume -> 1]}]]

Export["CrashCymbal.mid", 
 Sound[{SoundNote["CrashCymbal", {12.17, 13.48},  SoundVolume -> 1]}]]

Export[
 "FrenchHorn.mid",
 Sound@Map[
   SoundNote[Sequence @@ #, "FrenchHorn", 
     SoundVolume -> 1] &, {{"G3", {11.873005866666666`, 
      12.0371849`}}, {"G3", {12.23867735`, 
      15.850616083333332`}}, {"C4", {4.059321424999999`, 
      5.949151649999996`}}, {"G4", {6.169490599999996`, 
      7.999998799999996`}}, {"C5", {8.211863174999994`, 
      11.537185116666672`}}, {"C4", {11.828229766666672`, 
      11.925244650000005`}}, {"E4", {11.828229766666672`, 
      11.932707333333338`}}, {"G4", {11.820767083333338`, 
      11.955095383333338`}}, {"C4", {12.193901250000005`, 
      15.895392183333337`}}, {"G4", {12.193901250000005`, 
      15.895392183333337`}}, {"D#4", {12.201363933333338`, 
      15.910317550000004`}}}
   ]]

 Export[
 "BrassSection.mid",
 Sound@Map[
   SoundNote[Sequence @@ #, "BrassSection", 
     SoundVolume -> 1] &, {{"C4", {4.059321424999999`, 
      5.949151649999997`}}, {"G4", {6.169490599999997`, 
      7.999998799999997`}}, {"C5", {8.211863174999998`, 
      11.537185116666675`}}, {"C5", {11.828229766666675`, 
      11.925244650000009`}}, {"E5", {11.828229766666675`, 
      11.932707333333342`}}, {"G4", {11.820767083333342`, 
      11.955095383333342`}}, {"C5", {12.193901250000009`, 
      15.89539218333334`}}, {"G4", {12.193901250000009`, 
      15.89539218333334`}}, {"D#5", {12.201363933333342`, 
      15.910317550000007`}}}
   ]]

 Export[
 "Timpani.mid",
 Sound@Map[
   SoundNote[Sequence @@ #, "Timpani", 
     SoundVolume -> 1] &, {{"C3", {13.932706466666666`, 
      13.992407933333332`}}, {"D3", {14.007333299999999`, 
      14.059572083333332`}}, {"C3", {14.111810866666666`, 
      14.141661599999999`}}, {"D3", {14.164049649999999`, 
      14.208825749999999`}}, {"C3", {14.246139166666666`, 
      14.283452583333332`}}, {"D3", {14.298377949999999`, 
      14.350616733333332`}}, {"C3", {14.373004783333332`, 
      14.417780883333332`}}, {"D3", {14.425243566666666`, 
      14.484945033333332`}}, {"C3", {14.507333083333332`, 
      14.559571866666666`}}, {"D3", {14.567034549999999`, 
      14.619273333333332`}}, {"C3", {14.634198699999999`, 
      14.678974799999999`}}, {"D3", {14.693900166666666`, 
      14.746138949999999`}}, {"C3", {14.768526999999999`, 
      14.820765783333332`}}, {"D3", {14.820765783333332`, 
      14.873004566666665`}}, {"C3", {14.887929933333332`, 
      14.940168716666665`}}, {"D3", {14.955094083333332`, 
      15.007332866666665`}}, {"C3", {15.022258233333332`, 
      15.074497016666665`}}, {"D3", {15.074497016666665`, 
      15.126735799999999`}}, {"C3", {15.149123849999999`, 
      15.193899949999999`}}, {"D3", {15.201362633333332`, 
      15.253601416666665`}}, {"C3", {15.268526783333332`, 
      15.313302883333332`}}, {"D3", {15.320765566666665`, 
      15.365541666666665`}}, {"C3", {15.387929716666665`, 
      15.440168499999999`}}, {"D3", {15.440168499999999`, 
      15.484944599999999`}}, {"C3", {15.514795333333332`, 
      15.567034116666665`}}, {"D3", {15.559571433333332`, 
      15.611810216666665`}}, {"C3", {15.634198266666665`, 
      15.656586316666665`}}, {"C3", {15.738675833333332`, 
      15.828228033333332`}}, {"G2", {16.014795116666665`, 
      16.09688463333333`}}, {"C3", {16.2909144`, 
      16.3804666`}}, {"G2", {16.574496366666665`, 
      16.67151125`}}, {"C3", {16.850615649999998`, 
      16.932705166666665`}}, {"G2", {17.141660299999998`, 
      17.23867518333333`}}, {"C3", {17.425242266666665`, 
      17.50733178333333`}}, {"G2", {17.723749599999998`, 
      17.813301799999998`}}, {"C3", {18.007331566666664`, 
      18.08942108333333`}}, {"G2", {18.328226949999998`, 
      18.417779149999998`}}, {"C3", {18.62673428333333`, 
      18.708823799999998`}}, {"G2", {18.94016698333333`, 
      19.02971918333333`}}, {"C3", {19.290913099999997`, 
      19.484942866666664`}}}
   ]]

mixMIDI@{"Bassoon.mid", "BrassSection.mid", "FrenchHorn.mid", "CrashCymbal.mid", "Timpani.mid"}
$\endgroup$
  • $\begingroup$ Exactly what I was looking for. +1 $\endgroup$ – TreFox May 30 '17 at 20:23
8
$\begingroup$

Here is an answer using Audio and AudioData.

dirName = "~/some/dir/name/";
Export[dirName <> "sequence.mid", 
  Sound[SoundNote[#, 1.75, "Trumpet"] & /@ {0, 7, 12}]];

Export[dirName <> "perc.mid", 
  Sound[SoundNote["RideCymbal", #] & /@ {0.26, 0.1, 0.26, 0.1, 2}]];

mid1 = Import[dirName <> "perc.mid"];
mid2 = Import[dirName <> "sequence.mid"];

fs = 44000;

ams = Audio[#, SampleRate -> fs] & /@ {mid1, mid2}

Map[Dimensions, AudioData /@ ams]
(* {{2, 119872}, {2, 232064}} *)

AudioPlot /@ ams

enter image description here

am2 = MapThread[Join, AudioData /@ ams];
Dimensions[am2]
(* {2, 351936} *)

This does the sequential combination of midi objects:

Audio[am2]

enter image description here

This shows the sequential combination of midi objects with AudioPlot.

AudioPlot[Audio[am2]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.