TweetyPy

Check-in [8550c6eb3d]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:logging revisions; adjustable eraser size
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256: 8550c6eb3d1229c1ba413dbbf3fbf99968a3e174f9a88216ec837bb2dde43da8
User & Date: jmcclure 2019-09-09 01:32:00
Context
2019-09-09
01:32
logging revisions; adjustable eraser size Leaf check-in: 8550c6eb3d user: jmcclure tags: trunk
2019-09-08
18:56
log warning/error in status bar check-in: 88afc776d3 user: jmcclure tags: trunk
Changes

Changes to tweetypy/__init__.py.

18
19
20
21
22
23
24

25
26
27
28
29
30
31


32
33
34
35
36
37
38
39
   log = logging.getLogger()
   log.info('---------------------------------------------')
   log.info(f'TweetyPy started on {time.asctime(time.localtime())}')
   log.info('Version 1.0.0 "Ain\'t She Tweet"')
   log.info('Copyright 2017 Jesse McClure')
   log.info('License: MIT')
   log.info('---------------------------------------------')

   config = configure()
   app = QApplication(sys.argv)

   args = sys.argv[1:]
   files = [arg for arg in args if os.path.isfile(arg)]
   flags = [arg for arg in args if arg[0] == '-']
   other = [arg for arg in args if arg not in files + flags]



   if flags:
      log.warning(f'Ignoring unrecognized flags: {flags}')
   if other:
      log.warning(f'Ignoring unrecognized arguments: {other}')
   win = MainWin(files, config)
   return app.exec_()








>







>
>








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
   log = logging.getLogger()
   log.info('---------------------------------------------')
   log.info(f'TweetyPy started on {time.asctime(time.localtime())}')
   log.info('Version 1.0.0 "Ain\'t She Tweet"')
   log.info('Copyright 2017 Jesse McClure')
   log.info('License: MIT')
   log.info('---------------------------------------------')

   config = configure()
   app = QApplication(sys.argv)

   args = sys.argv[1:]
   files = [arg for arg in args if os.path.isfile(arg)]
   flags = [arg for arg in args if arg[0] == '-']
   other = [arg for arg in args if arg not in files + flags]
   # TODO log.set
   # log.handlers[0].setLevel('DEBUG')

   if flags:
      log.warning(f'Ignoring unrecognized flags: {flags}')
   if other:
      log.warning(f'Ignoring unrecognized arguments: {other}')
   win = MainWin(files, config)
   return app.exec_()

Changes to tweetypy/mainwin.py.

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
..
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74
   def emit(self, record):
      if record.levelno > 25:
         self.sbar.showMessage(self.format(record), 5000)

class MainWin(QMainWindow):
   def __init__(self, files, config):
      super().__init__()

      self.files = files
      self.data = []
      self.saved = True
      self.config = config
      #self.setGeometry(0, 0, 800, 600)
      self.setWindowTitle("TweetyPy")
      self.setWindowIcon(QIcon('web.png'))   # TODO FIXME
................................................................................
      if self.config['main-window']['maximized']:
         self.setWindowState(Qt.WindowMaximized)
      self.init_calls()
      self.init_actions()
      self.init_menubar()
      self.init_toolbar()
      self.init_statusbar()

      self.slog = RequestHandler(self.statusbar)
      log.addHandler(self.slog)

      self.init_body()

      self.show()
      self.spect = None
      # Spectrograms are sized relative to parent window.  The timer allows
      # the window manager to adjust the parent window prior to this sizing.
      if self.files:
         QTimer.singleShot(20,self.next_spectrogram)

................................................................................
   # and update window title

   def action_unknown(self):
      log.warning('Ignoring unknown action binding')

   def action_add_songs(self):
      fnames, kind = QFileDialog.getOpenFileNames(self, 'Select Audio Files', '', "Audio (*.wav)")

      if not fnames:
         return
      self.files += fnames
      if self.spect:
         self.setWindowTitle(f'TweetyPy: {self.spect.data["name"]} [+{len(self.files)} more]')
      else:
         self.next_spectrogram()

   def action_save(self):
      fname, kind = QFileDialog.getSaveFileName(self, 'Save data file', '', 'Data files (*.txt *.csv)')

      if not fname:
         return
      with open(fname, 'w') as f:
         f.write(self.config['data-files']['header'])
         for data in self.data:
            f.write(self.config['data-files']['format'].format(**data)) # TODO needs testing
      self.saved = True







<







 







<
<
<
<

>







 







>










>







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
..
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
   def emit(self, record):
      if record.levelno > 25:
         self.sbar.showMessage(self.format(record), 5000)

class MainWin(QMainWindow):
   def __init__(self, files, config):
      super().__init__()

      self.files = files
      self.data = []
      self.saved = True
      self.config = config
      #self.setGeometry(0, 0, 800, 600)
      self.setWindowTitle("TweetyPy")
      self.setWindowIcon(QIcon('web.png'))   # TODO FIXME
................................................................................
      if self.config['main-window']['maximized']:
         self.setWindowState(Qt.WindowMaximized)
      self.init_calls()
      self.init_actions()
      self.init_menubar()
      self.init_toolbar()
      self.init_statusbar()




      self.init_body()
      log.addHandler(RequestHandler(self.statusbar))
      self.show()
      self.spect = None
      # Spectrograms are sized relative to parent window.  The timer allows
      # the window manager to adjust the parent window prior to this sizing.
      if self.files:
         QTimer.singleShot(20,self.next_spectrogram)

................................................................................
   # and update window title

   def action_unknown(self):
      log.warning('Ignoring unknown action binding')

   def action_add_songs(self):
      fnames, kind = QFileDialog.getOpenFileNames(self, 'Select Audio Files', '', "Audio (*.wav)")
      log.debug(f'Adding song files: {fnames}')
      if not fnames:
         return
      self.files += fnames
      if self.spect:
         self.setWindowTitle(f'TweetyPy: {self.spect.data["name"]} [+{len(self.files)} more]')
      else:
         self.next_spectrogram()

   def action_save(self):
      fname, kind = QFileDialog.getSaveFileName(self, 'Save data file', '', 'Data files (*.txt *.csv)')
      log.debug(f'Saving data to: {fname}')
      if not fname:
         return
      with open(fname, 'w') as f:
         f.write(self.config['data-files']['header'])
         for data in self.data:
            f.write(self.config['data-files']['format'].format(**data)) # TODO needs testing
      self.saved = True

Changes to tweetypy/spectrogram.py.

1
2
3
4
5
6
7
8
9
10
11
12
..
31
32
33
34
35
36
37
38
39

40
41
42
43
44
45
46
...
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
...
235
236
237
238
239
240
241


242
243
244
245
246
247
248
...
259
260
261
262
263
264
265
266














import logging, os, wave
from numpy import *
from scipy.signal import spectrogram
from PyQt5.QtCore import pyqtSignal, Qt, QPoint, QRect
from PyQt5.QtGui import QColor, QImage, QPainter, QPainterPath
from PyQt5.QtWidgets import QGraphicsOpacityEffect, QWidget

log = logging.getLogger()

class Spectrogram(QWidget):

................................................................................
      if self.amp is None:
         parent.next_spectrogram()
         self.close()
         return
      self.init_calculations()
      self.init_image()
      self.fit()
      self.eraser = self.init_eraser(10, 200)
      self.eraser.maxheight = 200

      self.cursorX = self.init_cursor(1, self.height() * 4)
      self.cursorY = self.init_cursor(self.width() * 4, 1)
      self.show()

   def init_fft(self):
      fft = self.conf['fft']
      try:
................................................................................

   def resizeEvent(self, e):
      self.sx = self.width() / self.image.width()
      self.sy = self.height() / self.image.height()

   def mousePressEvent(self, e):
      if self.cursorX.isVisible():
         pass


      elif self.eraser.isVisible():

         copyto(self.amp_back, self.amp)
         pass

      elif e.button() == Qt.MiddleButton:
         self.fit()
      else:

         self.__pos = self.pos()
         self.__size = self.size()
         self.__epos = self.mapToParent(e.pos())

   def mouseMoveEvent(self, e):
      # TODO refactor this rats nest
      # TODO add eraser size changes
      y = e.pos().y()
      if y < self.eraser.maxheight / 2.0:
         fixed = y * 2.0
      elif y > self.height() - self.eraser.maxheight / 2.0:
         fixed = (self.height() - y) * 2.0
      else:
         fixed = self.eraser.maxheight
      self.eraser.setFixedHeight(int(clip(fixed,0,self.eraser.maxheight)))
      x = e.pos().x() - self.eraser.width() / 2.0
      y = e.pos().y() - self.eraser.height() / 2.0
      self.eraser.move(x, y)

      x = e.pos().x()
      y = e.pos().y()
      self.cursorX.move(x, 0)
      self.cursorY.move(0, y)
      s = self.imgdata.shape
      x = clip(int(s[1] * e.pos().x() / self.width()), 0, self.time.size)
      y = clip(int(s[0] * e.pos().y() / self.height()), 0, self.freq.size)
................................................................................
      x *= s[1] / self.width()
      y *= s[0] / self.height()
      x = int(clip(x, 0, s[1]))
      y = int(clip(y, 0, s[0]))
      return x, y

   def mouseEraseEvent(self, e):


      w = self.eraser.width()
      h = self.eraser.height()
      x = e.pos().x() - w / 2.0
      y = e.pos().y() - h / 2.0
      x1, y1 = self.mapToImgdata(x, y)
      x2, y2 = self.mapToImgdata(x + w, y + h)
      self.amp[y1:y2,x1:x2] = 255.0
................................................................................
         h = self.__size.height() + diff.y()
         self.resize(w, h)

   def mouseReleaseEvent(self, e):
      self.cursorX.resize(1, self.height() * 4)
      self.cursorY.resize(self.width() * 4, 1)
      self.update(self.contentsRect())


















|







 







|
|
>







 







<
>
>

>
|
<
>
|
|
<
>
|
|
|


<
<
<
<
<
<
<
<
<
<



<







 







>
>







 








>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
..
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
...
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
...
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

import logging, os, wave
from numpy import *
from scipy.signal import spectrogram
from PyQt5.QtCore import pyqtSignal, Qt, QPoint, QRect, QSize
from PyQt5.QtGui import QColor, QImage, QPainter, QPainterPath
from PyQt5.QtWidgets import QGraphicsOpacityEffect, QWidget

log = logging.getLogger()

class Spectrogram(QWidget):

................................................................................
      if self.amp is None:
         parent.next_spectrogram()
         self.close()
         return
      self.init_calculations()
      self.init_image()
      self.fit()
      self.eraser = self.init_eraser(20, 200)
      self.eraser.setMinimumSize(4, 40)
      self.eraser.setMaximumSize(100, 1000)
      self.cursorX = self.init_cursor(1, self.height() * 4)
      self.cursorY = self.init_cursor(self.width() * 4, 1)
      self.show()

   def init_fft(self):
      fft = self.conf['fft']
      try:
................................................................................

   def resizeEvent(self, e):
      self.sx = self.width() / self.image.width()
      self.sy = self.height() / self.image.height()

   def mousePressEvent(self, e):
      if self.cursorX.isVisible():

         if e.button() == Qt.MiddleButton:
            self.image.invertPixels()
      elif self.eraser.isVisible():
         if e.button() == Qt.LeftButton:
            copyto(self.amp_back, self.amp)

      else:
         if e.button() == Qt.MiddleButton:
            self.fit()

         elif e.button() == Qt.LeftButton or e.button() == Qt.RightButton:
            self.__pos = self.pos()
            self.__size = self.size()
            self.__epos = self.mapToParent(e.pos())

   def mouseMoveEvent(self, e):










      x = e.pos().x() - self.eraser.width() / 2.0
      y = e.pos().y() - self.eraser.height() / 2.0
      self.eraser.move(x, y)

      x = e.pos().x()
      y = e.pos().y()
      self.cursorX.move(x, 0)
      self.cursorY.move(0, y)
      s = self.imgdata.shape
      x = clip(int(s[1] * e.pos().x() / self.width()), 0, self.time.size)
      y = clip(int(s[0] * e.pos().y() / self.height()), 0, self.freq.size)
................................................................................
      x *= s[1] / self.width()
      y *= s[0] / self.height()
      x = int(clip(x, 0, s[1]))
      y = int(clip(y, 0, s[0]))
      return x, y

   def mouseEraseEvent(self, e):
      if e.buttons() != Qt.LeftButton:
         return
      w = self.eraser.width()
      h = self.eraser.height()
      x = e.pos().x() - w / 2.0
      y = e.pos().y() - h / 2.0
      x1, y1 = self.mapToImgdata(x, y)
      x2, y2 = self.mapToImgdata(x + w, y + h)
      self.amp[y1:y2,x1:x2] = 255.0
................................................................................
         h = self.__size.height() + diff.y()
         self.resize(w, h)

   def mouseReleaseEvent(self, e):
      self.cursorX.resize(1, self.height() * 4)
      self.cursorY.resize(self.width() * 4, 1)
      self.update(self.contentsRect())

   def wheelEvent(self, e):
      p = e.angleDelta()
      if self.cursorX.isVisible():
         # TODO adjust threshold ...
         pass
      elif self.eraser.isVisible():
         w = self.eraser.size().width() - p.x()
         h = self.eraser.size().height() + p.y()
         self.eraser.resize(QSize(w,h))
      else:
         pass