- int index = cache_index + (end - begin);
-
- last_pymax = (int) rint ((item->y1 + origin - MIN(waveview->cache->data[index].max, 1.0) * half_height) * item->canvas->pixels_per_unit);
- last_pymin = (int) rint ((item->y1 + origin - MAX(waveview->cache->data[index].min, -1.0) * half_height) * item->canvas->pixels_per_unit);
+ guint index = cache_index + (end - begin);
+
+ if (index >= waveview->cache->data_size) {
+
+ /* the data we want is off the end of the cache, which must mean its beyond
+ the end of the region's source; hence the peak values are 0 */
+ last_pymax = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
+ last_pymin = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
+
+ } else {
+
+ last_pymax = (int) rint ((item->y1 + origin - MIN(waveview->cache->data[index].max, 1.0) * half_height) * item->canvas->pixels_per_unit);
+ last_pymin = (int) rint ((item->y1 + origin - MAX(waveview->cache->data[index].min, -1.0) * half_height) * item->canvas->pixels_per_unit);
+
+ }
+