static ClosedContour ClosedContour_create (int numberOfPoints) { ClosedContour result = Melder_calloc (structClosedContour, 1); result -> numberOfPoints = numberOfPoints; result -> x = NUMvector <double> (1, 2 * numberOfPoints); result -> y = result -> x + numberOfPoints; return result; // LEAK }
void SpellingChecker_addNewWord (SpellingChecker me, const char32 *word) { try { autostring32 generic = Melder_calloc (char32, 3 * str32len (word) + 1); Longchar_genericize32 (word, generic.peek()); my userDictionary -> addString_copy (generic.transfer()); } catch (MelderError) { Melder_throw (me, U": word \"", word, U"\" not added."); } }
void SpellingChecker_addNewWord (SpellingChecker me, const char32 *word) { try { if (! my userDictionary) my userDictionary = SortedSetOfString_create (); autostring32 generic = Melder_calloc (char32, 3 * str32len (word) + 1); Longchar_genericize32 (word, generic.peek()); SortedSetOfString_addString (my userDictionary, generic.transfer()); } catch (MelderError) { Melder_throw (me, U": word \"", word, U"\" not added."); } }
void Graphics_grey (Graphics me, double **z, long ix1, long ix2, double x1WC, double x2WC, long iy1, long iy2, double y1WC, double y2WC, int _numberOfBorders, double borders []) { if (ix2 <= ix1 || iy2 <= iy1) return; /* Static variables. */ theGraphics = me; numberOfBorders = _numberOfBorders; data = z; border = borders; dx = (x2WC - x1WC) / (ix2 - ix1); dy = (y2WC - y1WC) / (iy2 - iy1); xoff = x1WC - ix1 * dx; yoff = y1WC - iy1 * dy; if (! right) { right = NUMmatrix <int> (0, MAXGREYSIDE - 1, 0, MAXGREYSIDE - 1); // BUG memory below = NUMmatrix <int> (0, MAXGREYSIDE - 1, 0, MAXGREYSIDE - 1); x = NUMvector <double> (1, MAXGREYPATH); y = NUMvector <double> (1, MAXGREYPATH); edgeContours = Melder_calloc (EdgeContour, MAXGREYEDGECONTOURS * numberOfBorders) - 1; closedContours = Melder_calloc (ClosedContour, MAXGREYCLOSEDCONTOURS * numberOfBorders) - 1; edgePoints = Melder_calloc (structEdgePoint, MAXGREYEDGEPOINTS * numberOfBorders); } /* The matrix is subdivided into matrices with side MAXGREYSIDE, so that: * 1. All the paths will fit into our memory (we have to remember them all). * 2. The path for filling fits into the PostScript path, which may be max. 1500 points long. */ for (row1 = iy1; row1 < iy2; row1 += MAXGREYSIDE - 1) { row2 = row1 + (MAXGREYSIDE - 1); if (row2 > iy2) row2 = iy2; for (col1 = ix1; col1 < ix2; col1 += MAXGREYSIDE - 1) { col2 = col1 + (MAXGREYSIDE - 1); if (col2 > ix2) col2 = ix2; smallGrey (); } } }
void Strings_nativize (Strings me) { autostring32 buffer = Melder_calloc (char32, Strings_maximumLength (me) + 1); for (long i = 1; i <= my numberOfStrings; i ++) { Longchar_nativize32 (my strings [i], buffer.peek(), false); autostring32 newString = Melder_dup (buffer.peek()); /* * Replace string only if copying was OK. */ Melder_free (my strings [i]); my strings [i] = newString.transfer(); } }
NUMlinprog NUMlinprog_new (bool maximize) { NUMlinprog me = nullptr; try { me = Melder_calloc (structNUMlinprog, 1); my linearProgram = glp_create_prob (); // TODO: check glp_set_obj_dir (my linearProgram, maximize ? GLP_MAX : GLP_MIN); } catch (MelderError) { if (me) NUMlinprog_delete (me); return NULL; } return me; }
void structWordList :: v_readBinary (FILE *f) { wchar *current, *p; int kar = 0; length = bingeti4 (f); if (length < 0) Melder_throw ("Wrong length ", length, "."); string = Melder_calloc (wchar, length + 1); p = current = string; if (length > 0) { /* * Read first word. */ for (;;) { if (p - string >= length - 1) break; kar = fgetc (f); if (kar == EOF) Melder_throw (L"Early end of file."); if (kar >= 128) break; *p ++ = kar; } *p ++ = '\n'; /* * Read following words. */ for (;;) { wchar *previous = current; int numberOfSame = kar - 128; current = p; wcsncpy (current, previous, numberOfSame); p += numberOfSame; for (;;) { if (p - string >= length - 1) break; kar = fgetc (f); if (kar == EOF) Melder_throw (L"Early end of file."); if (kar >= 128) break; *p ++ = kar; } *p ++ = '\n'; if (p - string >= length) break; } } *p = '\0'; if (p - string != length) Melder_throw ("Length in header (", length, ") does not match lenth of string (", (long) (p - string), ")."); }
void Strings_genericize (Strings me) { autostring32 buffer = Melder_calloc (char32, Strings_maximumLength (me) * 3 + 1); for (long i = 1; i <= my numberOfStrings; i ++) { const char32 *p = (const char32 *) my strings [i]; while (*p) { if (*p > 126) { // backslashes are not converted, i.e. genericize^2 == genericize Longchar_genericize32 (my strings [i], buffer.peek()); autostring32 newString = Melder_dup (buffer.peek()); /* * Replace string only if copying was OK. */ Melder_free (my strings [i]); my strings [i] = newString.transfer(); break; } p ++; } } }
Strings WordList_to_Strings (WordList me) { try { unsigned char *word = (unsigned char *) my string; autoStrings thee = Thing_new (Strings); thy numberOfStrings = WordList_count (me); if (thy numberOfStrings > 0) { thy strings = NUMvector <wchar *> (1, thy numberOfStrings); } for (long i = 1; i <= thy numberOfStrings; i ++) { unsigned char *kar = word; for (; *kar != '\n'; kar ++) { } long length = kar - word; thy strings [i] = Melder_calloc (wchar, length + 1); wcsncpy (thy strings [i], Melder_peekUtf8ToWcs ((const char *) word), length); thy strings [i] [length] = '\0'; word += length + 1; } return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": not converted to Strings."); } }
void _CollectionOfDaata_v_copy (_CollectionOfDaata* me, _CollectionOfDaata* thee) { thy at._elements = nullptr; // set to null in case the inherited v_copy crashes my structDaata :: v_copy (thee); thy _ownershipInitialized = my _ownershipInitialized; thy _ownItems = my _ownItems; thy _capacity = my _capacity; thy size = my size; if (my _capacity > 0) { thy at._elements = Melder_calloc (Daata, my _capacity); // filled with null pointers thy at._elements --; // immediately turn from base-0 into base-1 // BUG use NUMvector } for (long i = 1; i <= my size; i ++) { Daata itempie = my at [i]; if (my _ownItems) { if (! Thing_isa (itempie, classDaata)) Melder_throw (U"Cannot copy item of class ", Thing_className (itempie), U"."); thy at [i] = Data_copy (itempie).releaseToAmbiguousOwner(); } else { thy at [i] = itempie; // reference copy: if me doesn't own the items, then thee shouldn't either // NOTE: the items don't have to be Daata } } }
WordList Strings_to_WordList (Strings me) { try { long totalLength = 0; /* * Check whether the strings are generic and sorted. */ for (long i = 1; i <= my numberOfStrings; i ++) { wchar *string = my strings [i], *p; for (p = & string [0]; *p; p ++) { if (*p > 126) Melder_throw ("String \"", string, "\" not generic.\nPlease convert to backslash trigraphs first."); } if (i > 1 && wcscmp (my strings [i - 1], string) > 0) { Melder_throw ("String \"", string, L"\" not sorted.\nPlease sort first."); } totalLength += wcslen (string); } autoWordList thee = Thing_new (WordList); thy length = totalLength + my numberOfStrings; thy string = Melder_calloc (wchar, thy length + 1); /* * Concatenate the strings into the word list. */ wchar *q = thy string; for (long i = 1; i <= my numberOfStrings; i ++) { long length = wcslen (my strings [i]); wcscpy (q, my strings [i]); q += length; *q ++ = '\n'; } *q = '\0'; Melder_assert (q - thy string == thy length); return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": not converted to WordList."); } }
void LongSound_playPart (LongSound me, double tmin, double tmax, Sound_PlayCallback callback, Thing boss) { struct LongSoundPlay *thee = (struct LongSoundPlay *) & thePlayingLongSound; MelderAudio_stopPlaying (MelderAudio_IMPLICIT); Melder_free (thy resampledBuffer); // just in case, and after playing has stopped try { int fits = LongSound_haveWindow (me, tmin, tmax); long bestSampleRate = MelderAudio_getOutputBestSampleRate (my sampleRate), n, i1, i2; if (! fits) Melder_throw (U"Sound too long (", tmax - tmin, U" seconds)."); /* * Assign to *thee only after stopping the playing sound. */ thy tmin = tmin; thy tmax = tmax; thy callback = callback; thy boss = boss; if ((n = Sampled_getWindowSamples (me, tmin, tmax, & i1, & i2)) < 2) return; if (bestSampleRate == my sampleRate) { thy numberOfSamples = n; thy dt = 1 / my sampleRate; thy t1 = my x1; thy i1 = i1; thy i2 = i2; thy silenceBefore = (long) (my sampleRate * MelderAudio_getOutputSilenceBefore ()); thy silenceAfter = (long) (my sampleRate * MelderAudio_getOutputSilenceAfter ()); if (thy callback) thy callback (thy boss, 1, tmin, tmax, tmin); if (thy silenceBefore > 0 || thy silenceAfter > 0 || 1) { thy resampledBuffer = Melder_calloc (int16, (thy silenceBefore + thy numberOfSamples + thy silenceAfter) * my numberOfChannels); memcpy (& thy resampledBuffer [thy silenceBefore * my numberOfChannels], & my buffer [(i1 - my imin) * my numberOfChannels], thy numberOfSamples * sizeof (int16) * my numberOfChannels); MelderAudio_play16 (thy resampledBuffer, my sampleRate, thy silenceBefore + thy numberOfSamples + thy silenceAfter, my numberOfChannels, melderPlayCallback, thee); } else { MelderAudio_play16 (my buffer + (i1 - my imin) * my numberOfChannels, my sampleRate, thy numberOfSamples, my numberOfChannels, melderPlayCallback, thee); } } else { long newSampleRate = bestSampleRate; long newN = ((double) n * newSampleRate) / my sampleRate - 1; long silenceBefore = (long) (newSampleRate * MelderAudio_getOutputSilenceBefore ()); long silenceAfter = (long) (newSampleRate * MelderAudio_getOutputSilenceAfter ()); int16 *resampledBuffer = Melder_calloc (int16, (silenceBefore + newN + silenceAfter) * my numberOfChannels); int16 *from = my buffer + (i1 - my imin) * my numberOfChannels; // guaranteed: from [0 .. (my imax - my imin + 1) * nchan] double t1 = my x1, dt = 1.0 / newSampleRate; thy numberOfSamples = newN; thy dt = dt; thy t1 = t1 + i1 / my sampleRate; thy i1 = 0; thy i2 = newN - 1; thy silenceBefore = silenceBefore; thy silenceAfter = silenceAfter; thy resampledBuffer = resampledBuffer; if (my numberOfChannels == 1) { for (long i = 0; i < newN; i ++) { double t = t1 + i * dt; // from t1 to t1 + (newN-1) * dt double index = (t - t1) * my sampleRate; // from 0 long flore = index; // DANGEROUS: Implicitly rounding down... double fraction = index - flore; resampledBuffer [i + silenceBefore] = (1.0 - fraction) * from [flore] + fraction * from [flore + 1]; } } else if (my numberOfChannels == 2) { for (long i = 0; i < newN; i ++) { double t = t1 + i * dt; double index = (t - t1) * my sampleRate; long flore = index; double fraction = index - flore; long ii = i + silenceBefore; resampledBuffer [ii + ii] = (1.0 - fraction) * from [flore + flore] + fraction * from [flore + flore + 2]; resampledBuffer [ii + ii + 1] = (1.0 - fraction) * from [flore + flore + 1] + fraction * from [flore + flore + 3]; } } else { for (long i = 0; i < newN; i ++) { double t = t1 + i * dt; double index = (t - t1) * my sampleRate; long flore = index; double fraction = index - flore; long ii = (i + silenceBefore) * my numberOfChannels; for (long chan = 0; chan < my numberOfChannels; chan ++) { resampledBuffer [ii + chan] = (1.0 - fraction) * from [flore * my numberOfChannels + chan] + fraction * from [(flore + 1) * my numberOfChannels + chan]; } } } if (thy callback) thy callback (thy boss, 1, tmin, tmax, tmin); MelderAudio_play16 (resampledBuffer, newSampleRate, silenceBefore + newN + silenceAfter, my numberOfChannels, melderPlayCallback, thee); } //Melder_free (thy resampledBuffer); // cannot do that, because MelderAudio_play16 isn't necessarily synchronous } catch (MelderError) { Melder_free (thy resampledBuffer); Melder_throw (me, U": not played."); } }