Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #119

Merged
merged 4 commits into from
Jul 21, 2024
Merged

Fix #119

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions FragmentedRecordWriter/AudioResampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class AudioResampler {

try shift()

print("mic", inputSampleRate, outputSampleRate)
let bodyBuffer = underlyingBuffer.advanced(by: numOffsetSamples * 2)
if inputSampleRate == outputSampleRate {
copyMonoInt16(bodyBuffer, monoInt16Buffer, numSamples)
Expand All @@ -118,6 +119,64 @@ public class AudioResampler {
self.currentPTS = pts
}

public func append(
stereoInt16BufferWithSwap: UnsafeMutablePointer<Int16>,
numSamples: Int,
inputSampleRate: Int,
pts: CMTime
) throws {
guard
inputSampleRate == outputSampleRate || inputSampleRate * 2 == outputSampleRate
|| inputSampleRate * 6 == outputSampleRate
else {
throw AudioResamplerError.appendingSampleRateNotSupported
}

try shift()

let bodyBuffer = underlyingBuffer.advanced(by: numOffsetSamples * 2)
if inputSampleRate == outputSampleRate {
copyStereoInt16WithSwap(bodyBuffer, stereoInt16BufferWithSwap, numSamples)
self.numSamples = numSamples
} else if inputSampleRate * 2 == outputSampleRate {
copyStereoInt16UpsamplingBy2WithSwap(bodyBuffer, stereoInt16BufferWithSwap, numSamples)
self.numSamples = numSamples * 2
} else if inputSampleRate * 6 == outputSampleRate {
copyStereoInt16UpsamplingBy6WithSwap(bodyBuffer, stereoInt16BufferWithSwap, numSamples)
self.numSamples = numSamples * 6
}

self.currentPTS = pts
}

public func append(
monoInt16BufferWithSwap: UnsafeMutablePointer<Int16>, numSamples: Int, inputSampleRate: Int,
pts: CMTime
) throws {
guard
inputSampleRate == outputSampleRate || inputSampleRate * 2 == outputSampleRate
|| inputSampleRate * 6 == outputSampleRate
else {
throw AudioResamplerError.appendingSampleRateNotSupported
}

try shift()

let bodyBuffer = underlyingBuffer.advanced(by: numOffsetSamples * 2)
if inputSampleRate == outputSampleRate {
copyMonoInt16WithSwap(bodyBuffer, monoInt16BufferWithSwap, numSamples)
self.numSamples = numSamples
} else if inputSampleRate * 2 == outputSampleRate {
copyMonoInt16UpsamplingBy2WithSwap(bodyBuffer, monoInt16BufferWithSwap, numSamples)
self.numSamples = numSamples * 2
} else if inputSampleRate * 6 == outputSampleRate {
copyMonoInt16UpsamplingBy6WithSwap(bodyBuffer, monoInt16BufferWithSwap, numSamples)
self.numSamples = numSamples * 6
}

self.currentPTS = pts
}

public func getCurrentFrame() -> AudioResamplerFrame {
return AudioResamplerFrame(
numChannels: 2,
Expand Down
4 changes: 4 additions & 0 deletions FragmentedRecordWriter/FragmentedAudioWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ private class FragmentedAudioWriterDelegate: NSObject, AVAssetWriterDelegate {
}

public class FragmentedAudioWriter {
public var playlistURL: URL {
delegate.playlistURL
}

private let outputDirectoryURL: URL
private let outputFilePrefix: String
private let sampleRate: Int
Expand Down
116 changes: 116 additions & 0 deletions FragmentedRecordWriter/FragmentedRecordWriter-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,119 @@ static inline void copyMonoInt16UpsamplingBy6(float *__nonnull dst,
}
}
}

static inline float int16ToFloatWithSwap(int16_t reversedValue) {
uint16_t rawValue = *((uint16_t *)&reversedValue);
uint16_t rawSwappedValue = (rawValue >> 8) | (rawValue << 8);
return INT16_TO_FLOAT(*(int16_t *)&rawSwappedValue);
}

static inline void copyStereoInt16WithSwap(float *__nonnull dst,
int16_t *__nonnull src,
long numSamples) {
for (long i = 0; i < numSamples * 2; i++) {
dst[i] = int16ToFloatWithSwap(src[i]);
}
}

static inline void copyStereoInt16UpsamplingBy2WithSwap(float *__nonnull dst,
int16_t *__nonnull src,
long numSamples) {
{
float x0 = dst[-4];
float y0 = dst[-3];
float x1 = int16ToFloatWithSwap(src[0]);
float y1 = int16ToFloatWithSwap(src[1]);
for (long j = 1; j <= 2; j++) {
dst[j * 2 - 4] = x0 + (x1 - x0) * j / 2.0;
dst[j * 2 - 3] = y0 + (y1 - y0) * j / 2.0;
}
}

for (long i = 0; i < numSamples - 1; i++) {
float x0 = int16ToFloatWithSwap(src[i * 2 + 0]);
float y0 = int16ToFloatWithSwap(src[i * 2 + 1]);
float x1 = int16ToFloatWithSwap(src[i * 2 + 2]);
float y1 = int16ToFloatWithSwap(src[i * 2 + 3]);
for (long j = 1; j <= 2; j++) {
dst[i * 4 + j * 2 + 0] = x0 + (x1 - x0) * j / 2.0;
dst[i * 4 + j * 2 + 1] = y0 + (y1 - y0) * j / 2.0;
}
}
}

static inline void copyStereoInt16UpsamplingBy6WithSwap(float *__nonnull dst,
int16_t *__nonnull src,
long numSamples) {
{
float x0 = dst[-12];
float y0 = dst[-11];
float x1 = int16ToFloatWithSwap(src[0]);
float y1 = int16ToFloatWithSwap(src[1]);
for (long j = 1; j <= 6; j++) {
dst[j * 2 - 12] = x0 + (x1 - x0) * j / 6.0;
dst[j * 2 - 11] = y0 + (y1 - y0) * j / 6.0;
}
}

for (long i = 0; i < numSamples - 1; i++) {
float x0 = int16ToFloatWithSwap(src[i * 2 + 0]);
float y0 = int16ToFloatWithSwap(src[i * 2 + 1]);
float x1 = int16ToFloatWithSwap(src[i * 2 + 2]);
float y1 = int16ToFloatWithSwap(src[i * 2 + 3]);
for (long j = 1; j <= 6; j++) {
dst[i * 12 + j * 2 + 0] = x0 + (x1 - x0) * j / 6.0;
dst[i * 12 + j * 2 + 1] = y0 + (y1 - y0) * j / 6.0;
}
}
}

static inline void copyMonoInt16WithSwap(float *__nonnull dst,
int16_t *__nonnull src,
long numSamples) {
for (long i = 0; i < numSamples; i++) {
float x = int16ToFloatWithSwap(src[i]);
dst[i * 2] = dst[i * 2 + 1] = x;
}
}

static inline void copyMonoInt16UpsamplingBy2WithSwap(float *__nonnull dst,
int16_t *__nonnull src,
long numSamples) {
{
float x0 = dst[-4];
float x1 = int16ToFloatWithSwap(src[0]);
for (long j = 1; j <= 2; j++) {
dst[j * 2 - 4] = dst[j * 2 - 3] = dst[j * 2 - 3] =
x0 + (x1 - x0) * j / 2.0;
}
}

for (long i = 0; i < numSamples - 1; i++) {
float x0 = int16ToFloatWithSwap(src[i + 0]);
float x1 = int16ToFloatWithSwap(src[i + 1]);
for (long j = 1; j <= 2; j++) {
dst[i * 4 + j * 2] = dst[i * 4 + j * 2 + 1] = x0 + (x1 - x0) * j / 2.0;
}
}
}

static inline void copyMonoInt16UpsamplingBy6WithSwap(float *__nonnull dst,
int16_t *__nonnull src,
long numSamples) {
{
float x0 = dst[-12];
float x1 = int16ToFloatWithSwap(src[0]);
for (long j = 1; j <= 6; j++) {
dst[j * 2 - 12] = dst[j * 2 - 11] = x0 + (x1 - x0) * j / 6.0;
}
}

for (long i = 0; i < numSamples - 1; i++) {
float x0 = int16ToFloatWithSwap(src[i * 2 + 0]);
float x1 = int16ToFloatWithSwap(src[i * 2 + 2]);
for (long j = 1; j <= 6; j++) {
dst[i * 12 + j * 2] = dst[i * 12 + j * 2 + 1] = x0 + (x1 - x0) * j / 6.0;
}
}
}
114 changes: 0 additions & 114 deletions FragmentedRecordWriter/FragmentedScreenRecordWriter.swift

This file was deleted.

4 changes: 4 additions & 0 deletions FragmentedRecordWriter/FragmentedVideoWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ private class FragmentedVideoWriterDelegate: NSObject, AVAssetWriterDelegate {
}

public class FragmentedVideoWriter {
public var playlistURL: URL {
delegate.playlistURL
}

private let outputDirectoryURL: URL
private let outputFilePrefix: String
private let frameRate: CMTimeScale
Expand Down
34 changes: 34 additions & 0 deletions FragmentedRecordWriter/MasterPlaylistWriter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// MasterPlaylistWriter.swift
// FragmentedRecordWriter
//
// Created by Kaito Udagawa on 2024/07/21.
//

import Foundation

public struct MasterPlaylistWriter {

public init() {
}

public func write(
outputDirectoryURL: URL,
outputFilePrefix: String,
videoIndexURL: URL,
appAudioIndexURL: URL,
micAudioIndexURL: URL
) throws {
let masterPlaylistURL = outputDirectoryURL.appending(path: "\(outputFilePrefix).m3u8")
let masterPlaylistContent = """
#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=150000,CODECS="avc1.42e00a,mp4a.40.2",AUDIO="audio"
\(videoIndexURL.lastPathComponent)

#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="App",DEFAULT=YES,URI="\(appAudioIndexURL.lastPathComponent)"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="Mic",DEFAULT=NO,URI="\(micAudioIndexURL.lastPathComponent)"
"""

try masterPlaylistContent.write(to: masterPlaylistURL, atomically: true, encoding: .utf8)
}
}
Loading
Loading