|
2 | 2 | /* audioopmodule - Module to detect peak values in arrays */ |
3 | 3 |
|
4 | 4 | #include "Python.h" |
5 | | -#include <math.h> |
6 | 5 |
|
7 | 6 | #if SIZEOF_INT == 4 |
8 | 7 | typedef int Py_Int32; |
@@ -981,46 +980,32 @@ audioop_ratecv(PyObject *self, PyObject *args) |
981 | 980 | /* There are len input frames, so we need (mathematically) |
982 | 981 | ceiling(len*outrate/inrate) output frames, and each frame |
983 | 982 | requires bytes_per_frame bytes. Computing this |
984 | | - without spurious overflow is the challenge. */ |
985 | | - int ceiling; /* the number of output frames, eventually */ |
| 983 | + without spurious overflow is the challenge; we can |
| 984 | + settle for a reasonable upper bound, though. */ |
| 985 | + int ceiling; /* the number of output frames */ |
986 | 986 | int nbytes; /* the number of output bytes needed */ |
987 | 987 | int q = len / inrate; |
988 | | - int r = len - q * inrate; |
989 | | - /* Now len = q * inrate + r exactly, so |
990 | | - len*outrate/inrate = |
991 | | - (q*inrate+r)*outrate/inrate = |
992 | | - (q*inrate*outrate + r*outrate)/inrate = |
993 | | - q*outrate + r*outrate/inrate exactly. |
994 | | - q*outrate is an exact integer, so the ceiling we're after is |
995 | | - q*outrate + ceiling(r*outrate/inrate). */ |
996 | | - ceiling = q * outrate; |
997 | | - if (ceiling / outrate != q) { |
998 | | - PyErr_SetString(PyExc_MemoryError, |
999 | | - "not enough memory for output buffer"); |
1000 | | - goto exit; |
1001 | | - } |
1002 | | - /* Since r = len % inrate, in particular r < inrate. So |
1003 | | - r * outrate / inrate = (r / inrate) * outrate < outrate, |
1004 | | - so ceiling(r * outrate / inrate) <= outrate: the final |
1005 | | - result fits in an int -- it can't overflow. */ |
1006 | | - assert(r < inrate); |
1007 | | - q = (int)ceil((double)r * (double)outrate / (double)inrate); |
1008 | | - assert(q <= outrate); |
1009 | | - ceiling += q; |
1010 | | - if (ceiling < 0) { |
1011 | | - PyErr_SetString(PyExc_MemoryError, |
1012 | | - "not enough memory for output buffer"); |
1013 | | - goto exit; |
1014 | | - } |
| 988 | + /* Now len = q * inrate + r exactly (with r = len % inrate), |
| 989 | + and this is less than q * inrate + inrate = (q+1)*inrate. |
| 990 | + So a reasonable upper bound on len*outrate/inrate is |
| 991 | + ((q+1)*inrate)*outrate/inrate = |
| 992 | + (q+1)*outrate. |
| 993 | + */ |
| 994 | + ceiling = (q+1) * outrate; |
1015 | 995 | nbytes = ceiling * bytes_per_frame; |
1016 | | - if (nbytes / bytes_per_frame != ceiling) { |
| 996 | + /* See whether anything overflowed; if not, get the space. */ |
| 997 | + if (q+1 < 0 || |
| 998 | + ceiling / outrate != q+1 || |
| 999 | + nbytes / bytes_per_frame != ceiling) |
| 1000 | + str = NULL; |
| 1001 | + else |
| 1002 | + str = PyString_FromStringAndSize(NULL, nbytes); |
| 1003 | + |
| 1004 | + if (str == NULL) { |
1017 | 1005 | PyErr_SetString(PyExc_MemoryError, |
1018 | 1006 | "not enough memory for output buffer"); |
1019 | 1007 | goto exit; |
1020 | 1008 | } |
1021 | | - str = PyString_FromStringAndSize(NULL, nbytes); |
1022 | | - if (str == NULL) |
1023 | | - goto exit; |
1024 | 1009 | } |
1025 | 1010 | ncp = PyString_AsString(str); |
1026 | 1011 |
|
|
0 commit comments