Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

r""" 

Work with WAV files 

 

A WAV file is a header specifying format information, followed by a 

sequence of bytes, representing the state of some audio signal over a 

length of time. 

 

A WAV file may have any number of channels. Typically, they have 1 

(mono) or 2 (for stereo). The data of a WAV file is given as a 

sequence of frames. A frame consists of samples. There is one sample 

per channel, per frame. Every wav file has a sample width, or, the 

number of bytes per sample. Typically this is either 1 or 2 bytes. 

 

The wav module supplies more convenient access to this data. In 

particular, see the docstring for ``Wave.channel_data()``. 

 

The header contains information necessary for playing the WAV file, 

including the number of frames per second, the number of bytes per 

sample, and the number of channels in the file. 

 

AUTHORS: 

 

- Bobby Moretti and Gonzalo Tornaria (2007-07-01): First version 

- William Stein (2007-07-03): add more 

- Bobby Moretti (2007-07-03): add doctests 

""" 

from __future__ import print_function 

from __future__ import absolute_import 

from six.moves import range 

 

import math 

import os 

import wave 

 

from sage.plot.plot import list_plot 

from sage.structure.sage_object import SageObject 

from sage.arith.srange import srange 

from sage.misc.html import html 

from sage.rings.all import RDF 

 

class Wave(SageObject): 

""" 

A class wrapping a wave audio file. 

 

INPUT: 

 

You must call Wave() with either data = filename, where 

filename is the name of a wave file, or with each of the 

following options: 

 

- channels -- the number of channels in the wave file (1 for mono, 2 for 

stereo, etc... 

- width -- the number of bytes per sample 

- framerate -- the number of frames per second 

- nframes -- the number of frames in the data stream 

- bytes -- a string object containing the bytes of the data stream 

 

Slicing: 

 

Slicing a Wave object returns a new wave object that has been 

trimmed to the bytes that you have given it. 

 

Indexing: 

 

Getting the $n$th item in a Wave object will give you the value 

of the $n$th frame. 

""" 

def __init__(self, data=None, **kwds): 

if data is not None: 

self._filename = data 

self._name = os.path.split(data)[1] 

wv = wave.open(data, "rb") 

self._nchannels = wv.getnchannels() 

self._width = wv.getsampwidth() 

self._framerate = wv.getframerate() 

self._nframes = wv.getnframes() 

self._bytes = wv.readframes(self._nframes) 

from .channels import _separate_channels 

self._channel_data = _separate_channels(self._bytes, 

self._width, 

self._nchannels) 

wv.close() 

elif kwds: 

try: 

self._name = kwds['name'] 

self._nchannels = kwds['nchannels'] 

self._width = kwds['width'] 

self._framerate = kwds['framerate'] 

self._nframes = kwds['nframes'] 

self._bytes = kwds['bytes'] 

self._channel_data = kwds['channel_data'] 

except KeyError as msg: 

raise KeyError(msg + " invalid input to Wave initializer") 

else: 

raise ValueError("Must give a filename") 

 

 

def save(self, filename='sage.wav'): 

r""" 

Save this wave file to disk, either as a Sage sobj or as a .wav file. 

 

INPUT: 

filename -- the path of the file to save. If filename ends 

with 'wav', then save as a wave file, 

otherwise, save a Sage object. 

 

If no input is given, save the file as 'sage.wav'. 

 

""" 

if not filename.endswith('.wav'): 

SageObject.save(self, filename) 

return 

wv = wave.open(filename, 'wb') 

wv.setnchannels(self._nchannels) 

wv.setsampwidth(self._width) 

wv.setframerate(self._framerate) 

wv.setnframes(self._nframes) 

wv.writeframes(self._bytes) 

wv.close() 

 

def listen(self): 

""" 

Listen to (or download) this wave file. 

 

Creates a link to this wave file in the notebook. 

""" 

from sage.misc.html import html 

i = 0 

fname = 'sage%s.wav'%i 

while os.path.exists(fname): 

i += 1 

fname = 'sage%s.wav'%i 

 

self.save(fname) 

return html('<a href="cell://%s">Click to listen to %s</a>'%(fname, self._name)) 

 

def channel_data(self, n): 

""" 

Get the data from a given channel. 

 

INPUT: 

n -- the channel number to get 

 

OUTPUT: 

A list of signed ints, each containing the value of a frame. 

""" 

return self._channel_data[n] 

 

 

def getnchannels(self): 

""" 

Returns the number of channels in this wave object. 

 

OUTPUT: 

The number of channels in this wave file. 

""" 

return self._nchannels 

 

def getsampwidth(self): 

""" 

Returns the number of bytes per sample in this wave object. 

 

OUTPUT: 

The number of bytes in each sample. 

""" 

return self._width 

 

def getframerate(self): 

""" 

Returns the number of frames per second in this wave object. 

 

OUTPUT: 

The frame rate of this sound file. 

""" 

return self._framerate 

 

def getnframes(self): 

""" 

The total number of frames in this wave object. 

 

OUTPUT: 

The number of frames in this WAV. 

""" 

return self._nframes 

 

def readframes(self, n): 

""" 

Reads out the raw data for the first $n$ frames of this wave 

object. 

 

INPUT: 

n -- the number of frames to return 

 

OUTPUT: 

A list of bytes (in string form) representing the raw wav data. 

""" 

return self._bytes[:nframes*self._width] 

 

def getlength(self): 

""" 

Returns the length of this file (in seconds). 

 

OUTPUT: 

The running time of the entire WAV object. 

""" 

return float(self._nframes) / (self._nchannels * float(self._framerate)) 

 

def _repr_(self): 

nc = self.getnchannels() 

return "Wave file %s with %s channel%s of length %s seconds%s" % \ 

(self._name, nc, "" if nc == 1 else "s", self.getlength(), "" if nc == 1 else " each") 

 

def _normalize_npoints(self, npoints): 

""" 

Used internally while plotting to normalize the number of 

""" 

return npoints if npoints else self._nframes 

 

def domain(self, npoints=None): 

""" 

Used internally for plotting. Get the x-values for the various points to plot. 

""" 

npoints = self._normalize_npoints(npoints) 

# figure out on what intervals to sample the data 

seconds = float(self._nframes) / float(self._width) 

frame_duration = seconds / (float(npoints) * float(self._framerate)) 

 

domain = [n * frame_duration for n in range(npoints)] 

return domain 

 

def values(self, npoints=None, channel=0): 

""" 

Used internally for plotting. Get the y-values for the various points to plot. 

""" 

npoints = self._normalize_npoints(npoints) 

 

# now, how many of the frames do we sample? 

frame_skip = int(self._nframes / npoints) 

# the values of the function at each point in the domain 

cd = self.channel_data(channel) 

 

# now scale the values 

scale = float(1 << (8*self._width -1)) 

values = [cd[frame_skip*i]/scale for i in range(npoints)] 

return values 

 

def set_values(self, values, channel=0): 

""" 

Used internally for plotting. Get the y-values for the various points to plot. 

""" 

c = self.channel_data(channel) 

npoints = len(c) 

if len(values) != npoints: 

raise ValueError("values (of length %s) must have length %s"%(len(values), npoints)) 

 

# unscale the values 

scale = float(1 << (8*self._width -1)) 

values = [float(abs(s)) * scale for s in values] 

 

# the values of the function at each point in the domain 

c = self.channel_data(channel) 

for i in range(npoints): 

c[i] = values[i] 

 

def vector(self, npoints=None, channel=0): 

npoints = self._normalize_npoints(npoints) 

 

V = RDF**npoints 

return V(self.values(npoints=npoints, channel=channel)) 

 

def plot(self, npoints=None, channel=0, plotjoined=True, **kwds): 

""" 

Plots the audio data. 

 

INPUT: 

 

- npoints -- number of sample points to take; if not given, draws all 

known points. 

- channel -- 0 or 1 (if stereo). default: 0 

- plotjoined -- whether to just draw dots or draw lines between sample points 

 

OUTPUT: 

 

a plot object that can be shown. 

""" 

 

domain = self.domain(npoints = npoints) 

values = self.values(npoints=npoints, channel = channel) 

points = zip(domain, values) 

 

L = list_plot(points, plotjoined=plotjoined, **kwds) 

L.xmin(0) 

L.xmax(domain[-1]) 

return L 

 

def plot_fft(self, npoints=None, channel=0, half=True, **kwds): 

v = self.vector(npoints=npoints) 

w = v.fft() 

if half: 

w = w[:len(w)//2] 

z = [abs(x) for x in w] 

if half: 

r = math.pi 

else: 

r = 2*math.pi 

data = zip(srange(0, r, r/len(z)), z) 

L = list_plot(data, plotjoined=True, **kwds) 

L.xmin(0) 

L.xmax(r) 

return L 

 

def plot_raw(self, npoints=None, channel=0, plotjoined=True, **kwds): 

npoints = self._normalize_npoints(npoints) 

seconds = float(self._nframes) / float(self._width) 

sample_step = seconds / float(npoints) 

domain = [float(n*sample_step) / float(self._framerate) for n in range(npoints)] 

frame_skip = self._nframes / npoints 

values = [self.channel_data(channel)[frame_skip*i] for i in range(npoints)] 

points = zip(domain, values) 

 

return list_plot(points, plotjoined=plotjoined, **kwds) 

 

def __getitem__(self, i): 

""" 

Returns the `i`-th frame of data in the wave, in the form of a string, 

if `i` is an integer. 

Returns a slice of self if `i` is a slice. 

""" 

if isinstance(i, slice): 

start, stop, step = i.indices(self._nframes) 

return self._copy(start, stop) 

else: 

n = i*self._width 

return self._bytes[n:n+self._width] 

 

def slice_seconds(self, start, stop): 

""" 

Slices the wave from start to stop. 

 

INPUT: 

start -- the time index from which to begin the slice (in seconds) 

stop -- the time index from which to end the slice (in seconds) 

 

OUTPUT: 

A Wave object whose data is this object's data, 

sliced between the given time indices 

""" 

start = int(start*self.getframerate()) 

stop = int(stop*self.getframerate()) 

return self[start:stop] 

 

# start and stop are frame numbers 

def _copy(self, start, stop): 

start = start * self._width 

stop = stop * self._width 

channels_sliced = [self._channel_data[i][start:stop] for i in range(self._nchannels)] 

print(stop - start) 

 

return Wave(nchannels = self._nchannels, 

width = self._width, 

framerate = self._framerate, 

bytes = self._bytes[start:stop], 

nframes = stop - start, 

channel_data = channels_sliced, 

name = self._name) 

 

def __copy__(self): 

return self._copy(0, self._nframes) 

 

def convolve(self, right, channel=0): 

""" 

NOT DONE! 

 

Convolution of self and other, i.e., add their fft's, then 

inverse fft back. 

""" 

if not isinstance(right, Wave): 

raise TypeError("right must be a wave") 

npoints = self._nframes 

v = self.vector(npoints, channel=channel).fft() 

w = right.vector(npoints, channel=channel).fft() 

k = v + w 

i = k.inv_fft() 

conv = self.__copy__() 

conv.set_values(list(i)) 

conv._name = "convolution of %s and %s"%(self._name, right._name) 

return conv