<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def waveform_stack(mediafiles, xy_size, output=None, label_style=None, center_color=None, outer_color=None, bg_color=None):
 """ Create a stack of waveform images from audio data. Return path to created image file. """

    img_files = []
    output = output or os.path.abspath(os.path.dirname(os.path.commonprefix(mediafiles)))
    if os.path.isdir(output):
        output = os.path.join(output, "waveforms.jpg")
    cmd = [config.CMD_IM_MONTAGE] + shlex.split(label_style or WAVE_LABEL_STYLE)
    cmd += ["-tile", "1x%d" % len(mediafiles), "-geometry", "%dx%d" % xy_size, "-label", "%t"]

    try:
        tempdir = tempfile.mktemp(__name__)
        os.makedirs(tempdir) 

        for mediafile in sorted(mediafiles):
            img_files.append(waveform_image(mediafile, xy_size, tempdir, center_color, outer_color, bg_color))

        cmd.extend(img_files)
        cmd.append(output)
        subprocess.check_call(cmd, stdout=open(os.devnull, "wb"), stderr=subprocess.STDOUT)
    finally:
        if os.path.isdir(tempdir):
            shutil.rmtree(tempdir, ignore_errors=True)

    return output