1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
############################################################################
#
# Program: GDCM (Grassroots DICOM). A DICOM library
#
# Copyright (c) 2006-2011 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()
|