The first few lines of this code generate audio noise of arbitrary length. What would be the equivalent for generation of uncompressed video noise (analog tv static)?
import AVFoundation
let srcNode = AVAudioSourceNode { _, _, frameCount, bufferList in
for frame in 0..<Int(frameCount) {
let buf: UnsafeMutableBufferPointer<Float> =
UnsafeMutableBufferPointer(bufferList.pointee.mBuffers)
buf[frame] = Float.random(in: -1...1)
}
return noErr
}
let engine = AVAudioEngine()
let output = engine.outputNode
let format = output.inputFormat(forBus: 0)
engine.attach(srcNode)
engine.connect(srcNode, to: output, format: format)
try? engine.start()
CFRunLoopRunInMode(.defaultMode, CFTimeInterval(5.0), false)
engine.stop()
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
In Swift Playgrounds on macOS, when we create a new file in .documentDirectory given by FileManager, we can always search for the results in e.g. ~/Library/Developer/Xcode/DerivedData/[project-specific]/Build/Products/Debug .
I guess this location may change anytime.
I know that Xcode is not Matlab, but is there a folder / a shortcut / any place in the IDE that includes the resulting files, or even presents a live preview of created files (.csv) for easy inspection?
Let’s imagine HStack full of views. You put down your finger on the leftmost view and lift it up on the rightmost one. What modifier to use for the views in-betweeen just to be notified when the finger is sliding over them? With mouse it would be onHover, but with finger is there anything?
Without a modifier, the result can be achieved with this code:
import SwiftUI
import PlaygroundSupport
//
// How to refactor this ugly code
// with a modifier similar to onHover(perform:)?
//
struct ContentView: View{
@State var isHovered = Array(repeating: false, count: 8)
@State private var location = CGPoint.zero
var body: some View{
HStack{
ForEach(0..<8){ i in
GeometryReader{ g in
Rectangle()
.fill(isHovered[i] ? .orange : .gray)
.onChange(of: location) { newValue in
isHovered[i] = g.frame(
in: .named("keyboardSpace")
).contains(location)
}
}
}
}
.gesture(
DragGesture()
.onChanged { gesture in
location = gesture.location
}
.onEnded{ _ in
isHovered = Array(repeating: false, count: 8)
}
)
.frame(width: 500, height: 500)
.coordinateSpace(name: "keyboardSpace")
}
}
PlaygroundPage.current.setLiveView(ContentView())
The autor of Creating a Movie with an Image and Audio on iOS
proposes the way of rechecking for assetwriter readiness.
// lines 52-55
while !adaptor.assetWriterInput.isReadyForMoreMediaData { usleep(10) }
adaptor.append(buffer, withPresentationTime: startFrameTime)
Is this the canonical way of querying AVFoundation's objects, or maybe there's a better way than sleep and try again loop? The only remotely related post I found is How to check if AvAssetWriter has finished writing the last frame, and has four years and no answer.
For a SwiftUI view, I can SET its size and position with modifiers, or GET its frame by being clever with geometry reader. It is not symmetrical, and violates the Ockham's razor IMO.
Do you know a single modifier, from Apple or not, with which caller can set the value of a frame programmatically, BUT ALSO when the view is autopositioned and autosized the binding sets the value to the correct CGRect?
Hi,
if values are PUBLISHED rapidly, then ALL are present in the Combine sink, but SOME of them are absent from the async loop. Why the difference?
For example, in the code below, tapping repeatedly 4 times gives the output:
INPUT 24, INPUT 9, INPUT 31, INPUT 45, SINK 24, SINK 9, LOOP 24, SINK 31, SINK 45, LOOP 31.
import SwiftUI
import Combine
import PlaygroundSupport
var subject = PassthroughSubject<Int, Never>()
struct ContentView: View {
@State var bag = [AnyCancellable]()
@State var a = [String]()
var body: some View {
Text("TAP A FEW TIMES RAPIDLY")
.frame(width: 160, height: 160)
.onTapGesture {
Task {
let anyInt = Int.random(in: 1..<100)
print("INPUT \(anyInt)")
try await Task.sleep(nanoseconds: 3_000_000_000)
subject.send(anyInt)
}
}
.task {
for await anyInt in subject.values {
print(" LOOP \(anyInt)")
}
}
.onAppear{
subject.sink{ anyInt in
print(" SINK \(anyInt)")
}.store(in: &bag)
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Thank you.