MaxMSP オーディオシグナルを数値として使いたい時

#Maxでオーディオシグナルを数値として取得したい時

Mspによるオーディオシグナル処理をMaxオブジェクトで使いたいシーンがあると思います。
その時の実装例の1つです。

以下のコードは、[snapshot~]オブジェクトを使って44100サンプル間隔で数値に変換しています。

[thresh~]オブジェクトによって、シグナルレベルが0.8以上の時だけ、キャプチャするようにしています。

![スクリーンショット 2021-04-26 17.55.37.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/152481/f8fbf892-4bd4-c6cd-a0c1-c87275fbdf50.png)



```Max
{
	"boxes" : [ 		{
			"box" : 			{
				"maxclass" : "number",
				"numinlets" : 1,
				"numoutlets" : 2,
				"patching_rect" : [ 1309.0, 774.0, 50.0, 20.0 ],
				"outlettype" : [ "", "bang" ],
				"id" : "obj-123",
				"parameter_enable" : 0
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "thresh~ 0.8",
				"numinlets" : 3,
				"numoutlets" : 1,
				"patching_rect" : [ 1309.0, 614.0, 62.0, 20.0 ],
				"outlettype" : [ "signal" ],
				"id" : "obj-119"
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "number",
				"numinlets" : 1,
				"numoutlets" : 2,
				"patching_rect" : [ 1309.0, 740.0, 50.0, 20.0 ],
				"outlettype" : [ "", "bang" ],
				"id" : "obj-117",
				"parameter_enable" : 0
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "* 127.",
				"numinlets" : 2,
				"numoutlets" : 1,
				"patching_rect" : [ 1309.0, 710.0, 35.0, 20.0 ],
				"outlettype" : [ "float" ],
				"id" : "obj-94"
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "4410 samples",
				"numinlets" : 2,
				"numoutlets" : 1,
				"patching_rect" : [ 1386.0, 614.0, 89.0, 23.0 ],
				"outlettype" : [ "" ],
				"fontsize" : 13.0,
				"id" : "obj-47",
				"fontname" : "Arial"
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "snapshot~",
				"numinlets" : 2,
				"numoutlets" : 1,
				"patching_rect" : [ 1309.0, 647.5, 59.0, 20.0 ],
				"outlettype" : [ "float" ],
				"id" : "obj-41"
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "flonum",
				"numinlets" : 1,
				"numoutlets" : 2,
				"patching_rect" : [ 1309.0, 680.0, 50.0, 20.0 ],
				"outlettype" : [ "", "bang" ],
				"format" : 6,
				"id" : "obj-20",
				"parameter_enable" : 0
			}

		}
 ],
	"lines" : [ 		{
			"patchline" : 			{
				"source" : [ "obj-117", 0 ],
				"destination" : [ "obj-123", 0 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-119", 0 ],
				"destination" : [ "obj-41", 0 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-94", 0 ],
				"destination" : [ "obj-117", 0 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-20", 0 ],
				"destination" : [ "obj-94", 0 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-41", 0 ],
				"destination" : [ "obj-20", 0 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-47", 0 ],
				"destination" : [ "obj-41", 0 ]
			}

		}
 ],
	"appversion" : 	{
		"major" : 8,
		"minor" : 1,
		"revision" : 10,
		"architecture" : "x64",
		"modernui" : 1
	}
,
	"styles" : [ 		{
			"name" : "AudioStatus_Menu",
			"default" : 			{
				"bgfillcolor" : 				{
					"type" : "color",
					"color" : [ 0.294118, 0.313726, 0.337255, 1 ],
					"color1" : [ 0.454902, 0.462745, 0.482353, 0.0 ],
					"color2" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"angle" : 270.0,
					"proportion" : 0.39,
					"autogradient" : 0
				}

			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "BEAP-Audio",
			"default" : 			{
				"textcolor_inverse" : [ 0.862745, 0.870588, 0.878431, 0.698039 ],
				"fontsize" : [ 10.0 ],
				"accentcolor" : [ 0.442178, 0.455072, 0.451475, 1.0 ],
				"textcolor" : [ 0.862745, 0.870588, 0.878431, 0.698039 ],
				"color" : [ 0.278431, 0.839216, 1.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "Luca",
			"default" : 			{
				"textcolor_inverse" : [ 0.239216, 0.254902, 0.278431, 1.0 ],
				"selectioncolor" : [ 0.720698, 0.16723, 0.080014, 1.0 ],
				"bgfillcolor" : 				{
					"type" : "gradient",
					"color" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"color1" : [ 0.862745, 0.870588, 0.878431, 1.0 ],
					"color2" : [ 0.65098, 0.666667, 0.662745, 1.0 ],
					"angle" : 270.0,
					"proportion" : 0.39,
					"autogradient" : 0
				}
,
				"bgcolor" : [ 0.904179, 0.895477, 0.842975, 0.56 ],
				"accentcolor" : [ 0.32549, 0.345098, 0.372549, 1.0 ],
				"color" : [ 0.475135, 0.293895, 0.251069, 1.0 ],
				"elementcolor" : [ 0.786675, 0.801885, 0.845022, 1.0 ],
				"fontname" : [ "Open Sans Semibold" ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "Matt",
			"default" : 			{
				"fontface" : [ 1 ],
				"fontsize" : [ 10.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "WTF",
			"default" : 			{
				"bgfillcolor" : 				{
					"type" : "gradient",
					"color" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"color1" : [ 0.32549, 0.345098, 0.372549, 1.0 ],
					"color2" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"angle" : 270.0,
					"proportion" : 0.39,
					"autogradient" : 0
				}
,
				"fontsize" : [ 18.0 ],
				"bgcolor" : [ 0.163647, 0.174699, 0.17409, 1.0 ],
				"accentcolor" : [ 0.50764, 0.065317, 0.112129, 1.0 ],
				"patchlinecolor" : [ 0.231373, 0.121569, 0.305882, 0.9 ],
				"color" : [ 0.113725, 0.580392, 0.737255, 1.0 ],
				"elementcolor" : [ 0.461105, 0.492646, 0.591878, 1.0 ],
				"fontname" : [ "HydrogenType" ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classic",
			"default" : 			{
				"textcolor_inverse" : [ 0.0, 0.0, 0.0, 1.0 ],
				"bgfillcolor" : 				{
					"type" : "color",
					"color1" : [ 0.83978, 0.839941, 0.839753, 1.0 ],
					"color2" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"color" : [ 0.839216, 0.839216, 0.839216, 1.0 ],
					"angle" : 270.0,
					"proportion" : 0.39
				}
,
				"fontsize" : [ 9.0 ],
				"bgcolor" : [ 0.83978, 0.839941, 0.839753, 1.0 ],
				"accentcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
				"patchlinecolor" : [ 0.0, 0.0, 0.0, 1.0 ],
				"color" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
				"fontname" : [ "Geneva" ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicButton",
			"default" : 			{
				"color" : [ 1.0, 0.890196, 0.090196, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicDial",
			"default" : 			{
				"color" : [ 1.0, 0.890196, 0.090196, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicGain~",
			"default" : 			{
				"color" : [ 0.380392, 0.380392, 0.380392, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicGswitch",
			"default" : 			{
				"accentcolor" : [ 1.0, 1.0, 1.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicGswitch2",
			"default" : 			{
				"accentcolor" : [ 1.0, 1.0, 1.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicKslider",
			"default" : 			{
				"selectioncolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
				"bgcolor" : [ 0.0, 0.0, 0.0, 1.0 ],
				"color" : [ 1.0, 1.0, 1.0, 1.0 ],
				"elementcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicLed",
			"default" : 			{
				"color" : [ 1.0, 0.0, 0.0, 1.0 ],
				"elementcolor" : [ 0.6, 0.0, 0.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicMatrixctrl",
			"default" : 			{
				"color" : [ 1.0, 0.0, 0.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicMeter~",
			"default" : 			{
				"bgcolor" : [ 0.380392, 0.380392, 0.380392, 1.0 ],
				"elementcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicNodes",
			"default" : 			{
				"fontsize" : [ 9.0 ],
				"color" : [ 0.839216, 0.839216, 0.839216, 1.0 ],
				"elementcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicNslider",
			"default" : 			{
				"color" : [ 0.0, 0.0, 0.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicNumber",
			"default" : 			{
				"selectioncolor" : [ 1.0, 0.890196, 0.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicPictslider",
			"default" : 			{
				"elementcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicPreset",
			"default" : 			{
				"color" : [ 1.0, 0.890196, 0.090196, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicScope~",
			"default" : 			{
				"bgcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
				"color" : [ 0.462745, 0.933333, 0.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicTab",
			"default" : 			{
				"color" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
				"elementcolor" : [ 0.839216, 0.839216, 0.839216, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicTextbutton",
			"default" : 			{
				"accentcolor" : [ 0.0, 0.0, 0.0, 1.0 ],
				"color" : [ 1.0, 1.0, 1.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicToggle",
			"default" : 			{
				"color" : [ 0.380392, 0.380392, 0.380392, 1.0 ],
				"elementcolor" : [ 0.376471, 0.384314, 0.4, 0.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicUmenu",
			"default" : 			{
				"color" : [ 1.0, 1.0, 1.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "classicWaveform~",
			"default" : 			{
				"selectioncolor" : [ 0.498039, 0.498039, 0.498039, 0.5 ],
				"color" : [ 0.380392, 0.380392, 0.380392, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "dark-night-patch",
			"default" : 			{
				"bgfillcolor" : 				{
					"type" : "gradient",
					"color1" : [ 0.376471, 0.384314, 0.4, 1.0 ],
					"color2" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"color" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"angle" : 270.0,
					"proportion" : 0.39
				}
,
				"accentcolor" : [ 0.952941, 0.564706, 0.098039, 1.0 ],
				"patchlinecolor" : [ 0.439216, 0.74902, 0.254902, 0.898039 ],
				"textcolor" : [ 0.862745, 0.870588, 0.878431, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "drab",
			"default" : 			{
				"selectioncolor" : [ 0.815686, 0.858824, 0.34902, 0.25 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "jpatcher001",
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "jpatcher002",
			"default" : 			{
				"clearcolor" : [ 0.32549, 0.345098, 0.372549, 0.0 ],
				"bgfillcolor" : 				{
					"type" : "color",
					"color1" : [ 0.32549, 0.345098, 0.372549, 0.0 ],
					"color2" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"color" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"angle" : 270.0,
					"proportion" : 0.39
				}
,
				"fontsize" : [ 9.5 ],
				"patchlinecolor" : [ 0.65098, 0.65098, 0.65098, 0.0 ],
				"fontname" : [ "Ableton Sans Book" ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "jpink",
			"default" : 			{
				"selectioncolor" : [ 0.619608, 0.0, 0.360784, 1.0 ],
				"clearcolor" : [ 0.113725, 0.607843, 0.607843, 1.0 ],
				"bgfillcolor" : 				{
					"type" : "color",
					"color" : [ 0.619608, 0.0, 0.360784, 1.0 ],
					"color1" : [ 0.376471, 0.384314, 0.4, 1.0 ],
					"color2" : [ 0.290196, 0.309804, 0.301961, 1.0 ],
					"angle" : 270.0,
					"proportion" : 0.39,
					"autogradient" : 0
				}
,
				"bgcolor" : [ 0.862745, 0.870588, 0.878431, 1.0 ],
				"accentcolor" : [ 0.619608, 0.0, 0.360784, 1.0 ],
				"patchlinecolor" : [ 0.65, 0.65, 0.65, 1.0 ],
				"textcolor" : [ 0.619608, 0.0, 0.360784, 1.0 ],
				"color" : [ 0.619608, 0.0, 0.360784, 1.0 ],
				"elementcolor" : [ 0.619608, 0.0, 0.360784, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "ksliderWhite",
			"default" : 			{
				"color" : [ 1.0, 1.0, 1.0, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "lightbutton",
			"default" : 			{
				"bgcolor" : [ 0.309495, 0.299387, 0.299789, 1.0 ],
				"elementcolor" : [ 0.654902, 0.572549, 0.376471, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "newobjBlue-1",
			"default" : 			{
				"accentcolor" : [ 0.317647, 0.654902, 0.976471, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "newobjBrown-1",
			"default" : 			{
				"accentcolor" : [ 0.654902, 0.572549, 0.376471, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "newobjCyan-1",
			"default" : 			{
				"accentcolor" : [ 0.029546, 0.773327, 0.821113, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "newobjGreen-1",
			"default" : 			{
				"accentcolor" : [ 0.0, 0.533333, 0.168627, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "newobjMagenta-1",
			"default" : 			{
				"accentcolor" : [ 0.840663, 0.060168, 0.769195, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "newobjRed-1",
			"default" : 			{
				"accentcolor" : [ 0.784314, 0.145098, 0.023529, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "newobjYellow-1",
			"default" : 			{
				"fontsize" : [ 12.059008 ],
				"accentcolor" : [ 0.82517, 0.78181, 0.059545, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "newobjYellow-2",
			"default" : 			{
				"fontsize" : [ 12.059008 ],
				"accentcolor" : [ 0.82517, 0.78181, 0.059545, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "numberGold-1",
			"default" : 			{
				"accentcolor" : [ 0.764706, 0.592157, 0.101961, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "purple",
			"default" : 			{
				"textcolor_inverse" : [ 0.701961, 0.415686, 0.886275, 1.0 ],
				"bgcolor" : [ 0.304029, 0.250694, 0.285628, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "receives",
			"default" : 			{
				"accentcolor" : [ 0.870588, 0.415686, 0.062745, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "rsliderGold",
			"default" : 			{
				"bgcolor" : [ 0.764706, 0.592157, 0.101961, 1.0 ],
				"color" : [ 0.646639, 0.821777, 0.854593, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "sends",
			"default" : 			{
				"accentcolor" : [ 0.0, 0.533333, 0.168627, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "tap",
			"default" : 			{
				"fontname" : [ "Lato Light" ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "tastefulltoggle",
			"default" : 			{
				"bgcolor" : [ 0.185512, 0.263736, 0.260626, 1.0 ],
				"color" : [ 0.941176, 0.690196, 0.196078, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "tastefultoggle",
			"default" : 			{
				"bgcolor" : [ 0.287863, 0.333333, 0.329398, 1.0 ],
				"color" : [ 0.941176, 0.690196, 0.196078, 1.0 ],
				"elementcolor" : [ 0.654902, 0.572549, 0.376471, 1.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "test",
			"default" : 			{
				"fontface" : [ 1 ],
				"fontsize" : [ 10.0 ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
, 		{
			"name" : "whitey",
			"default" : 			{
				"textcolor_inverse" : [ 0.65098, 0.666667, 0.662745, 1.0 ],
				"selectioncolor" : [ 0.011765, 0.396078, 0.752941, 1.0 ],
				"fontsize" : [ 36.0 ],
				"patchlinecolor" : [ 0.199068, 0.062496, 0.060031, 0.9 ],
				"fontname" : [ "Dirty Ego" ]
			}
,
			"parentstyle" : "",
			"multi" : 0
		}
 ],
	"classnamespace" : "box"
}

````