# PlaySound.py

    ############################################################################
    #
    #  Program: GDCM (Grassroots DICOM). A DICOM library
    #  Module:  $URL$
    #
    #  Copyright (c) 2006-2009 Mathieu Malaterre
    #  All rights reserved.
    #  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
    #
    #     This software is distributed WITHOUT ANY WARRANTY; without even
    #     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    #     PURPOSE.  See the above copyright notice for more information.
    #
    ############################################################################

    """
    Usage:

     python PlaySound.py input.dcm
    """

    import gdcm
    import sys

    #filename = "/home/mmalaterre/Creatis/gdcmDataExtra/gdcmNonImageData/audio_from_rafael_sanguinetti.dcm"
    filename = sys.argv[1]
    print filename

    r = gdcm.Reader()
    r.SetFileName( filename )
    if not r.Read():
      sys.exit(1)

    ds = r.GetFile().GetDataSet()

    waveformtag = gdcm.Tag(0x5400,0x0100)
    waveformsq = ds.GetDataElement( waveformtag )
    #print waveformsq

    #print dir(waveformsq)

    items = waveformsq.GetSequenceOfItems()

    if not items.GetNumberOfItems():
      sys.exit(1)

    item = items.GetItem(1)
    #print item

    waveformds = item.GetNestedDataSet()
    #print waveformds

    waveformdatatag = gdcm.Tag(0x5400,0x1010)
    waveformdata = waveformds.GetDataElement( waveformdatatag )

    #print waveformdata.GetPointer()
    bv = waveformdata.GetByteValue()
    print dir(bv)

    #print bv.GetPointer()
    print bv.GetLength()
    l = 116838

    file='test.wav'
    myfile = open(file, "wb")
    s = bv.GetPointer()
    for i in range(0, l):
      myfile.write(s[i])
    myfile.close()

    # http://mail.python.org/pipermail/python-list/2004-October/288905.html
    if sys.platform.startswith('win'):
       from winsound import PlaySound, SND_FILENAME, SND_ASYNC
       PlaySound(file, SND_FILENAME|SND_ASYNC)
    elif sys.platform.find('linux')>-1:
       from wave import open as waveOpen
       from ossaudiodev import open as ossOpen
       s = waveOpen(file,'rb')
       (nc,sw,fr,nf,comptype, compname) = s.getparams( )
       dsp = ossOpen('/dev/dsp','w')
       try:
         from ossaudiodev import AFMT_S16_NE
       except ImportError:
         if byteorder == "little":
           AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
         else:
           AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
       dsp.setparameters(AFMT_S16_NE, nc, fr)
       data = s.readframes(nf)
       s.close()
       dsp.write(data)
       dsp.close()