public static GbAudio rewriteTo4bitFancy(short[] samples) {
    int len = samples.length / 2;
    int[] outSamples = new int[len * 2];
    int[] soValues = new int[len];

    for (int pos = 0; pos < len; pos++) {
      int minSoError = Integer.MAX_VALUE;
      int minSoErrorSo = 0;
      for (int so = 0; so < 8; so++) {
        int curSoError = 0;
        for (int offset = 0; offset < 2; offset++) {
          int minError = Integer.MAX_VALUE;
          for (int level = 0; level < 16; level++) {
            int error = Math.abs(samples[2 * pos + offset] - getSample(level, so));
            error *= error;
            minError = Math.min(minError, error);
          }
          curSoError += minError;
        }
        if (curSoError < minSoError) {
          minSoError = curSoError;
          minSoErrorSo = so;
        }
      }

      for (int offset = 0; offset < 2; offset++) {
        int minError = Integer.MAX_VALUE;
        int minErrorLevel = 0;
        for (int level = 0; level < 16; level++) {
          int error = Math.abs(samples[2 * pos + offset] - getSample(level, minSoErrorSo));
          if (error < minError) {
            minError = error;
            minErrorLevel = level;
          }
        }
        outSamples[2 * pos + offset] = minErrorLevel;
      }
      soValues[pos] = minSoErrorSo;
    }

    return new GbAudio(outSamples, soValues);
  }