GDCM: PlaySound.py

GDCM 3.0.26

PlaySound.py
1
14
15"""
16Usage:
17
18 python PlaySound.py input.dcm
19"""
20
21import gdcm
22import sys
23
24#filename = "/home/mmalaterre/Creatis/gdcmDataExtra/gdcmNonImageData/audio_from_rafael_sanguinetti.dcm"
25filename = sys.argv[1]
26print filename
27
28r = gdcm.Reader()
29r.SetFileName( filename )
30if not r.Read():
31 sys.exit(1)
32
33ds = r.GetFile().GetDataSet()
34
35waveformtag = gdcm.Tag(0x5400,0x0100)
36waveformsq = ds.GetDataElement( waveformtag )
37#print waveformsq
38
39#print dir(waveformsq)
40
41items = waveformsq.GetSequenceOfItems()
42
43if not items.GetNumberOfItems():
44 sys.exit(1)
45
46item = items.GetItem(1)
47#print item
48
49waveformds = item.GetNestedDataSet()
50#print waveformds
51
52waveformdatatag = gdcm.Tag(0x5400,0x1010)
53waveformdata = waveformds.GetDataElement( waveformdatatag )
54
55#print waveformdata.GetPointer()
56bv = waveformdata.GetByteValue()
57print dir(bv)
58
59#print bv.GetPointer()
60print bv.GetLength()
61l = 116838
62
63file='test.wav'
64myfile = open(file, "wb")
65s = bv.GetPointer()
66for i in range(0, l):
67 myfile.write(s[i])
68myfile.close()
69
70# http://mail.python.org/pipermail/python-list/2004-October/288905.html
71if sys.platform.startswith('win'):
72 from winsound import PlaySound, SND_FILENAME, SND_ASYNC
73 PlaySound(file, SND_FILENAME|SND_ASYNC)
74elif sys.platform.find('linux')>-1:
75 from wave import open as waveOpen
76 from ossaudiodev import open as ossOpen
77 s = waveOpen(file,'rb')
78 (nc,sw,fr,nf,comptype, compname) = s.getparams( )
79 dsp = ossOpen('/dev/dsp','w')
80 try:
81 from ossaudiodev import AFMT_S16_NE
82 except ImportError:
83 if byteorder == "little":
84 AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
85 else:
86 AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
87 dsp.setparameters(AFMT_S16_NE, nc, fr)
88 data = s.readframes(nf)
89 s.close()
90 dsp.write(data)
91 dsp.close()
gdcm::Reader
Reader ala DOM (Document Object Model)
Definition gdcmReader.h:54
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39