我正在尝试在Metal中实现代码,该代码在两个具有长度的向量之间执行一维卷积.我已经实现了以下正常工作
kernel void convolve(const device float *dataVector [[ buffer(0) ]],
const device int& dataSize [[ buffer(1) ]],
const device float *filterVector [[ buffer(2) ]],
const device int& filterSize [[ buffer(3) ]],
device float *outVector [[ buffer(4) ]],
uint id [[ thread_position_in_grid ]]) {
int outputSize = dataSize - filterSize + 1;
for (int i=0;i<outputSize;i++) {
float sum = 0.0;
for (int j=0;j<filterSize;j++) {
sum += dataVector[i+j] * filterVector[j];
}
outVector[i] = sum;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是使用Metal处理(计算+与GPU之间的数据传输)相同的数据需要大约10倍的时间,而不是CPU上的Swift.我的问题是如何用单个向量操作替换内部循环还是有另一种方法来加速上面的代码?
我正在尝试删除我添加到视图中的所有子视图,因此我实现了一个循环来迭代子视图,其中包含以下内容:
for subview in view.subviews {
println(subview)
//subview.removeFromSuperview()
}
Run Code Online (Sandbox Code Playgroud)
我通过在我的视图中添加UILabel来测试它,然后运行此代码.输出包含我的UILabel,但也包含_UILayoutGuide.所以,我的问题是我如何确定子视图是我添加的还是系统添加的子视图?
我似乎无法弄清楚如何使用 来约束节点的 Z 值SCNTransformConstraint。这是我到目前为止所拥有的。
let constraint = SCNTransformConstraint(inWorldSpace: true, withBlock:{
node, matrix in
var newMatrix = matrix
let currentNode = node as SCNNode
if (currentNode.presentationNode().position.z > 0.0) {
newMatrix.m43 = 0.0
}
return newMatrix
})
ship.constraints = [constraint]
Run Code Online (Sandbox Code Playgroud)
有了上述约束,ship当我对其施加力时,它不会移动physicsBody。任何帮助将不胜感激。
我正在编写一个应用程序,用于在iPhone 6上使用内置麦克风录制单声道音频.当配置为以8000 Hz录制时,应用程序按预期工作.这是代码
// Set up audio session
let session = AVAudioSession.sharedInstance()
// Configure audio session
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
var recordSettings = [String:AnyObject]()
recordSettings[AVFormatIDKey] = Int(kAudioFormatLinearPCM) as AnyObject
// Set the sampling rate
recordSettings[AVSampleRateKey] = 8000.0 as AnyObject
recordSettings[AVNumberOfChannelsKey] = 1 as AnyObject
recorder = try AVAudioRecorder(url: outputFileURL, settings: recordSettings)
recorder?.delegate = self
recorder?.isMeteringEnabled = true
recorder?.prepareToRecord()
return true
}
catch {
throw Error.AVConfiguration
}
Run Code Online (Sandbox Code Playgroud)
为了降低存储要求,我想以更低的采样率(理想情况下小于1000 Hz)进行记录.如果我将采样率设置为1000 Hz,应用程序记录为8000 Hz.
根据Apple的文档,
硬件采样率的可用范围取决于设备.它通常在8000到48000赫兹之间.
问题...是否可以使用AVAudioSession(或其他框架)以低采样率录制音频?
将 SceneKit 场景的内容导出到 Collada (.dae) 文件时,我得到了意外的结果。这是我到目前为止所拥有的。
我创建了一个沿 x 轴有 5 个球体的简单场景
var x:CGFloat = 0
for i in 0...4 {
let sphere = SCNNode(geometry: SCNSphere(radius: 1))
sphere.name = "sphere\(i+1)"
sphere.position = SCNVector3(x: x, y: 0, z: 0)
exportScene.rootNode.addChildNode(sphere)
x += 2
}
Run Code Online (Sandbox Code Playgroud)
并导出内容
let url = URL(fileURLWithPath: pathName)
exportScene.write(to: url, options: nil, delegate: nil) { totalProgress, error, stop in
print("Export progress: \(totalProgress * 100.0)%")
}
Run Code Online (Sandbox Code Playgroud)
当我将 .dae 文件加载到 3D 程序 (Cheetah 3D) 中时,我期望沿 x 轴有 5 个相同的球体,但出现以下内容。我在导出到 .obj 文件时遇到了类似的问题。
下面的答案是“请记住,DAE …
为了更好地理解 Swift,我扩展SequenceType了添加我自己版本的map,reduce和forEach函数。但是,该filter函数的不同之处在于它返回一个Self.Generator.Element. 这是我到目前为止所拥有的:
extension SequenceType {
public func myFilter(@noescape includeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element] {
var array = [Self.Generator.Element]()
for element in self {
do {
if try includeElement (element) {
array.append(element)
}
}
}
return array
}
}
Run Code Online (Sandbox Code Playgroud)
该语句var array = [Self.Generator.Element]()产生“Invalid use of () to call a value of non-function type [Self.Generator.Element.Type]”错误。我的问题是如何创建/添加到/返回一个数组Self.Generator.Element?
我正在尝试编写一个着色器来为 SceneKit 中的自定义几何图形着色。我想设置颜色,以便朝上的水平表面是白色,朝下的水平表面是黑色,而中间的所有其他表面都是基于其方向的灰色阴影。我使用材质的表面入口点来调用以下着色器
vec4 normal = vec4(_surface.normal, 1.0);
// Assume value is [-1, 1], scale to [0, 1]
float color = normal.z * 0.5 + 0.5;
_surface.diffuse = vec4(color, color, color, 1.0);
Run Code Online (Sandbox Code Playgroud)
似乎normal.z是相对于相机(或视图)。我假设我需要转换该值,以便它位于另一个空间中。我尝试了多种变换(和变换的组合),例如u_inverseViewTransform,但结果似乎都在视图空间中。有谁知道如何根据其表面的方向为几何体着色?
我在记录中添加了一个数组,但是我无法访问数组的元素.这是我的代码.
# Create and initialize an array
@array = (1, 2, 3);
# Add it to a record
$rec = {
field1 => 'foo',
field2 => @array
};
Run Code Online (Sandbox Code Playgroud)
我试图打印数组的内容,但它不起作用.
print $rec->{field2}[0] . "\n";
Run Code Online (Sandbox Code Playgroud)
我的问题是......将数组添加到记录然后访问数组元素的正确方法是什么?