Saving buffer as wav file

simon.c.hendry's icon

Hello! I'm a fairly experienced Max programmer but struggling with this simple task. I am trying to save the contents of a buffer to disk without having a popup. The reference suggests writewave followed by a filename should do this but I'm not having much luck. Bonus points to save the audio to a folder in the directory with the patch in it... Here is an example which just records a sine wave into a buffer.

{
    "boxes" : [         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "TRY AND SAVE",
                "id" : "obj-14",
                "patching_rect" : [ 127.0, 192.0, 150.0, 20.0 ],
                "numinlets" : 1,
                "numoutlets" : 0
            }

        }
,         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "RECORD SINE WAVE",
                "id" : "obj-12",
                "patching_rect" : [ 150.0, 64.0, 150.0, 20.0 ],
                "numinlets" : 1,
                "numoutlets" : 0
            }

        }
,         {
            "box" :             {
                "maxclass" : "message",
                "text" : "writewave test.wav",
                "id" : "obj-10",
                "outlettype" : [ "" ],
                "patching_rect" : [ 127.0, 214.0, 109.0, 22.0 ],
                "numinlets" : 2,
                "numoutlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "message",
                "text" : "clear",
                "id" : "obj-8",
                "outlettype" : [ "" ],
                "patching_rect" : [ 62.0, 248.0, 35.0, 22.0 ],
                "numinlets" : 2,
                "numoutlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "waveform~",
                "id" : "obj-6",
                "outlettype" : [ "float", "float", "float", "float", "list", "" ],
                "patching_rect" : [ 259.0, 248.0, 256.0, 64.0 ],
                "buffername" : "test",
                "numinlets" : 5,
                "numoutlets" : 6
            }

        }
,         {
            "box" :             {
                "maxclass" : "toggle",
                "id" : "obj-5",
                "parameter_enable" : 0,
                "outlettype" : [ "int" ],
                "patching_rect" : [ 192.0, 102.0, 24.0, 24.0 ],
                "numinlets" : 1,
                "numoutlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "buffer~ test 1000",
                "id" : "obj-3",
                "outlettype" : [ "float", "bang" ],
                "patching_rect" : [ 127.0, 253.0, 99.0, 22.0 ],
                "numinlets" : 1,
                "numoutlets" : 2
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "record~ test",
                "id" : "obj-2",
                "outlettype" : [ "signal" ],
                "patching_rect" : [ 221.0, 157.0, 72.0, 22.0 ],
                "numinlets" : 3,
                "numoutlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "cycle~ 200",
                "id" : "obj-1",
                "outlettype" : [ "signal" ],
                "patching_rect" : [ 224.0, 103.0, 66.0, 22.0 ],
                "numinlets" : 2,
                "numoutlets" : 1
            }

        }
 ],
    "lines" : [         {
            "patchline" :             {
                "source" : [ "obj-10", 0 ],
                "destination" : [ "obj-3", 0 ]
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-8", 0 ],
                "destination" : [ "obj-3", 0 ]
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-5", 0 ],
                "destination" : [ "obj-2", 0 ]
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-1", 0 ],
                "destination" : [ "obj-2", 0 ]
            }

        }
 ],
    "appversion" :     {
        "major" : 8,
        "minor" : 2,
        "revision" : 2,
        "architecture" : "x64",
        "modernui" : 1
    }
,
    "classnamespace" : "box"
}
Source Audio's icon

you can fill the buffer without record~

fill sin ...

buffer should write it next to the patch, if only file name is set but ...
you don't need writewave, at least max 8 on mac does write
file respecting file extension
write aaa.aif or write bbb.wav writes set format properly.

if you have problems writing files like that, use absolute path,
otherwise file might land where you don't expect it, for whatever reason.

Roman Thilenius's icon


i prefer specific locations and i either set them at the beginning of the session (user has to chooses an output folder) but i also have fixed custom locations in the max search path (such as desktop or "beside max/mysamples")

simon.c.hendry's icon

Thanks both. Went down the specific path route and all working now. Not sure why it wasn't working before as even wrote aaa.wav wasn't putting an audio file anywhere, but all working now!